scrollbar - How to trigger a JavaScript function when the scroll bar reaches a certain percentage of the page height - Stack Ove

I am looking for a solution written in plain JavaScript that fires a JS function at a certain percentag

I am looking for a solution written in plain JavaScript that fires a JS function at a certain percentage of a page scroll (say 80%). I am looking to acplish something similar to what Waypoints does, without 750 lines of code or using jQuery.

I'm assuming I would figure determine the height of the window then trigger the callback once the scroll point is greater than 80% of the height.

The other requirements here are that I only want it to trigger once. The other concern I have is potential performance issues of constantly polling for the scroll percentage. Is this a valid concern?

I am looking for a solution written in plain JavaScript that fires a JS function at a certain percentage of a page scroll (say 80%). I am looking to acplish something similar to what Waypoints does, without 750 lines of code or using jQuery.

I'm assuming I would figure determine the height of the window then trigger the callback once the scroll point is greater than 80% of the height.

The other requirements here are that I only want it to trigger once. The other concern I have is potential performance issues of constantly polling for the scroll percentage. Is this a valid concern?

Share Improve this question edited Jan 22, 2017 at 1:57 Fady Sadek 1,1601 gold badge13 silver badges23 bronze badges asked Jan 22, 2017 at 1:33 Zach RussellZach Russell 1,1502 gold badges16 silver badges27 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

A way that will check the page scroll once every 500ms while scrolling, and will only run once:

var throttledListener = throttle(scrollListener, 500);
window.addEventListener('scroll', throttledListener);

function throttle(func, delay) { // allows [func] to run once every [delay] ms
    var func = func.bind(func),
        last = Date.now();
    return function() {
        if (Date.now() - last > delay) {
            func();
            last = Date.now();
        }
    }
}
function scrollListener() {
    console.log('scrolled');
    var threshold = document.body.clientHeight * 0.6;
    if (window.pageYOffset >= threshold) {
        alert('user scrolled to threshold; listener removed');
        window.removeEventListener('scroll', throttledListener);
    }
}
<div style="height:2000px;width:100%">tall div</div>

You can use onscroll

function trigerScroll(ev){ if(window.pageYOffset>400)alert('User has scrolled at least 400 px!'); } window.onscroll=trigerScroll ;

Edit:

you can get the document height like this

var body = document.body,
    html = document.documentElement;

var height = Math.max( body.scrollHeight, body.offsetHeight, 
                       html.clientHeight, html.scrollHeight, html.offsetHeight );

source : How to get height of entire document with JavaScript?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745564904a4633334.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信