设置方法:1、用offset()设置元素相对于文档的偏移坐标,语法“元素对象.offset({top:偏移值,left:偏移值})”;2、用scrollTop()设置元素垂直滚动条位置;3、用scrollLeft()设置元素水平滚动条位置。

本教程操作环境:windows7系统、jquery1.10.2版本、Dell G3电脑。
jquery设置元素位置的多种方法
1、使用offset()
offset() 方法可设置被选元素相对于文档的偏移坐标。
$(selector).offset({top:value,left:value})规定以像素为单位的 top 和 left 坐标。
示例:
这是一个段落。

2、使用scrollTop()
scrollTop() 方法可设置被选元素的垂直滚动条位置。
jQuery插件可以实现鼠标滑过图片时在图片上方创建任意的元素,包括分享按钮和图片文字描述等。这些按钮和文字可以悬浮在图片的任意位置,同时你也可以自己定义这些图片上方的元素出现时的动画特效
$(selector).scrollTop(position)
提示:当滚动条位于最顶部时,位置是 0。
示例:
This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text. This is some text.

3、使用使用scrollLeft()
scrollLeft() 可以设置匹配元素相对滚动条左侧的偏移,即水平滚动条的位置。
$(selector).scrollLeft(position)
滚动条的水平位置指的是从其左侧滚动过的像素数。当滚动条位于最左侧时,位置是 0。
示例:
The longest word in the english dictionary is: pneumonoultramicroscopicsilicovolcanoconiosis.

【推荐学习:jQuery视频教程、web前端视频】









