Show/hide div on window scroll

我的未来我决定 提交于 2019-12-11 04:32:50

问题


I have a div element #btns that is hidden by default. It should be displayed on scrolling 200px from top and again hidden after 500px from top.

Here is my (non-working) code:

$(window).scroll(function() {
    if ($(this).scrollTop()>200) {
        $('#btns').fadeIn();
    } 
    elseif ($(this).scrollTop()<500) {
        $('#btns').fadeIn();
    } else {
        $('#btns').fadeOut();
    }
});

回答1:


You can add a class hide in button like this:

$(function() {
    $(window).scroll(function() {
        console.log('scrolling ', $(window).scrollTop(), $(document).height());
        if($(window).scrollTop() >= 200 && $(window).scrollTop() <= ($(document).height() - 500)) {
            $('#btns').removeClass('hide');
        } else {
            $('#btns').addClass('hide');
        }
    });
});

DEMO https://jsfiddle.net/1ks8at6r/5/



来源:https://stackoverflow.com/questions/38552135/show-hide-div-on-window-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!