基本代码如下
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>mag3</title> <link rel="stylesheet" type="text/css" href="http://blog.163.com/m13194695967_2/blog/reset.css"> <style> *{margin:0;padding:0;border:0;} .all{width:500px;height:500px;border:1px solid blue;margin:0 auto;/* text-align:center; */} .con{width:200px;height:200px;background:url("img/backpic7.jpg");} </style> </head> <body> <div class="all"> <div class="con" id="con"></div> </div> </body>
<script type="text/javascript"> var con=document.getElementById("con");
con.style.backgroundPosition=600+"px "+800+"px";
</script> </html>
如: con.style.backgroundPosition=x+"px "+y+"px";
可以兼容所有的浏览器,但要注意第一个“px ”中有一个空格。
如:
不兼容FF.
2用百分比指定。
如: con.style.backgroundPosition=x%+y%;
如:
不兼容FF。
代码部分更改如下
var isFF = !!navigator.userAgent.match(/firefox/i);//FF的hack try//异常处理 { if(isFF) { con.style.backgroundPosition="60%"+"80%"; } con.style.backgroundPositionX="60%"; con.style.backgroundPositionY="80%"; } catch (err) { }