将图像始终放在中心页面

Mel*_*udd 5 css

即使在移动滚动时,也将图像始终放在中心页面(为ajax调用加载Ex图像).如何?

Fla*_*ino 8

对于大多数浏览器,您可以使用 position:fixed

 img.centered {
     position:fixed;
     left: 50%;
     top:  50%;
     /*
         if, for instance, the image is 64x64 pixels,
         then "move" it half its width/height to the
         top/left by using negative margins
     */
     margin-left: -32px;
     margin-top:  -32px;
 }
Run Code Online (Sandbox Code Playgroud)

例如,如果图像是40x30像素,则需要进行设置margin-left:-20px; margin-top:-15px.

这是一个jsfiddle示例:http://jsfiddle.net/WnSnj/1/

请注意,position:fixed在所有浏览器中的工作方式并不完全相同(尽管在所有浏览器中都可以).请参阅:http://www.quirksmode.org/css/position.html