admin管理员组文章数量:1289635
Using jquery, how can you set an event that triggers when there is a vertical scroll visible, and when you change from top half of page to bottom half of page, and vice versa.
So for example, if the vertical scroll bar is there, and then I am looking somewhere on the page which is in the top half of the page, and then move down so I am in the bottom half of the page, the function happens. Then if I move back up so I am in the top half again, the function happens.
Thanks.
Using jquery, how can you set an event that triggers when there is a vertical scroll visible, and when you change from top half of page to bottom half of page, and vice versa.
So for example, if the vertical scroll bar is there, and then I am looking somewhere on the page which is in the top half of the page, and then move down so I am in the bottom half of the page, the function happens. Then if I move back up so I am in the top half again, the function happens.
Thanks.
Share Improve this question asked Aug 2, 2013 at 18:37 omegaomega 43.9k90 gold badges285 silver badges521 bronze badges3 Answers
Reset to default 4var midHeight = jQuery(window).height() / 2 //Splits screen in half
$(window).scroll(function () {
if ($(window).scrollTop() > midHeight) {
//Do something on bottom
} else {
//Do something on top
}
})
well jQuery does have these methods you can use on an element:
.scrollTop() - get top scroll position on an element in the viewport
.scrollLeft() - get left scroll position on an element in the viewport
Also there is the scroll event - that triggers when a viewport/element scrolls within another one (or the window):
.scroll()
$(window).scroll(function () {
if ($(window).scrollTop() > $('body').height() / 2) {
//code goes here...
}
});
Detect Scroll
var s = $('body').scrollTop();
jQuery scrollTop()
Description: Get the current vertical position of the scroll bar for the first element in the set of matched elements or set the vertical position of the scroll bar for every matched element.
You may be interested to know:
$().scrollTop()//how much has been scrolled
$().innerHeight()// inner height of the element
DOMElement.scrollHeight//height of the content of the element
JSFIDDLE DEMO
本文标签: javascriptHow to detect vertical scroll position using jqueryStack Overflow
版权声明:本文标题:javascript - How to detect vertical scroll position using jquery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741392995a2376214.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论