jQuery scrollLeft() 方法
实例
返回 <div> 元素的水平滚动条位置:
<!DOCTYPE html><html><head><script src="https://libs.baidu.com/jquery/2.1.4/jquery.min.js"></script><script>$(document).ready(function(){$("button").click(function(){alert($("div").scrollLeft() + " px");});});</script></head><body><div style="border:1px solid black;width:100px;height:130px;overflow:auto">The longest word in the english dictionary is: pneumonoultramicroscopicsilicovolcanoconiosis.</div><br><button>Return the horizontal position of the scrollbar</button><p>Move the scrollbar to the right and click the button again.</p></body></html>
定义与用法
scrollLeft() 方法设置或返回所选元素的水平滚动条位置。
提示: 当滚动条位于最左侧时,位置为 0。
当用于返回该位置时:
此方法返回第一个匹配元素的滚动条的水平位置。
当用于设置位置时:
此方法为所有匹配的元素设置滚动条的水平位置。
语法
返回水平滚动条位置:
$(selector).scrollLeft()
设置水平滚动条位置:
$(selector).scrollLeft(position)
| 参数 | 描述 |
|---|---|
| position | 以像素为单位指定水平滚动条的位置 |