2012-02-09 61 views
1

我想要一個GIF保持在頁面的中心,而不管用戶在哪裏滾動。我希望那裏有一個錯覺,那就是圖像'漂浮在頁面上方'而不是它上面。在HTML頁面上浮動一個gif圖像

我相信CSS會是這樣做的方式嗎?假設有一個名爲foo.gif的圖像,CSS會做什麼?

+0

你的意思是*浮動*就像一個[陰影](HTTP:// EN .wikipedia.org /維基/ Drop_shadow)?你到目前爲止嘗試了什麼? ['position:fixed'](http://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/),['margin:auto'](http:// bluerobot。 com/web/css/center1.html),...? – 2012-02-09 10:35:15

回答

5

沒有你想要什麼更具體的指導,爲什麼呢,我能提供的最好的是:

img { 
    position: fixed; /* forces the element to stay fixed in relation to the viewport */ 
    top: 50%; /* sets the top of the image 50% of the page height */ 
    left: 50%; /* sets the left side of the image 50% across the page */ 
    margin-left: -100px; /* moves the image half of its own width to the left-side of the page */ 
    margin-top: -93px; /* moves the image half its height 'up' the page */ 
    box-shadow: 0.5em 0.5em 0.7em #333; /* to give the illusion of 'floating' 
    border-radius: 1em; 
} 

JS Fiddle demo

+0

謝謝你David。 :) – 2014-01-28 10:55:11

1

它與iiz的解決方案,如果您更改position:absoluteposition:fixed

我創建了一個jsfiddle供您查看。

我還包括一個陰影(from here),使圖像「浮動」。

這一切都有點粘在一起,但它會工作,你可以在你希望的任何方式改變它...