设置元素在文档中的坐标
分类:实例代码
本章节分享一段代码实例,它实现了设置一个div元素在document文档中坐标的效果。
使用offset()方法即可实现此功能。
代码实例如下:
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.pipipi.net/" /> <title>前端教程网</title> <style> * { margin:0px; padding:0px; } #box { width:200px; height:100px; background:#ccc; margin:0px auto; margin-top:100px; position:relative; } #antzone { width:50px; height:50px; background:green; } </style> <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> <script type="text/javascript"> $(document).ready(function () { $("#antzone").offset({ top: 100, left: 200 }); }) </script> </head> <body> <div id="box"> <div id="antzone">1</div> </div> </body> </html>
关于offset()方法可以参阅jQuery offset()方法一章节。