通过连续的点击+1/-1按钮可以看出,当#pg宽度>#pg背景图片宽度(400px)且为奇数时,右侧才会出现这1px的空隙

当我们纠结于奇偶数的时候,神奇的
body{margin-left:1px}
出现了,详见《CSS IE one pixel image offset hack》。当设置了body的左外边距为1px时,不管奇偶数都不会出现这个1px的空隙了。不过,当body宽度小于背景大图宽度(这里是400px)且为偶数时,左侧交界处却出现了1px的空隙

看来只有用JS解决了,是当body宽度≥背景大图宽度(这里是400px)时,令body margin-left:1px,<时则去除margin-left:1px 全部代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> <title>Sub-Pixel</title> <style type="text/css"> body, div, p{margin:0;padding:0;} body{text-align:center;} button{margin:1em;padding:0 1em;} #pg, body{background-position:center 0;background-repeat:no-repeat;} body{background-image:url('http://webteam.tencent.com/wp-content/uploads/2010/3/1749_003.jpg');} #pg{margin:0 auto;width:200px;height:200px;background-image:url('http://webteam.tencent.com/wp-content/uploads/2010/3/1749_004.jpg');} .fix-bg{margin-left:1px;} </style> </head> <body> <div id="pg"></div> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/ jquery.min.js"></script> <script> $(document).ready(function(){ var body = $('body'); function fixBg() { (body.width() >= 400) ? body.addClass('fix-bg') : body.removeClass('fix-bg'); } fixBg(); $(window).resize(fixBg); }) </script> </body> </html>
后记
眼看就要圆满了,转念一想:“如果大图宽度是奇数,会出现这个问题吗?如果出现了,那怎么搞捏?” 额…介个,介个…
原文:http://webteam.tencent.com/?p=1749
本文链接:http://www.blueidea.com/tech/web/2010/7467.asp
出处:腾讯Webteam
责任编辑:bluehearts
上一页 Sub-Pixel Bug?! [2] 下一页
◎进入论坛网页制作、WEB标准化版块参加讨论,我还想发表评论。
|