繁体   English   中英

滚动到底部时如何隐藏div并滚动到顶部时再次显示它?

[英]How to hide div when you scroll to bottom and show it again when scroll up to top?

当您进入页面底部时,我希望隐藏滚动条。 我写了这段代码,它工作正常:

<script>
document.onscroll = function() {
        if (window.innerHeight + window.scrollY > document.body.clientHeight) {
            document.getElementById("scroller").style.display='hide';
        }


    }
</script>

但是现在,当您返回顶部时,滚动条也被隐藏了。

当用户返回顶部时,我想再次显示#scroller。

只需添加一个其他案例来显示它:

if (window.innerHeight + window.scrollY > document.body.clientHeight) {
     document.getElementById("scroller").style.display='none';
}
else{
     document.getElementById("scroller").style.display='block';
}
<script>
    document.onscroll = function() {
        if (window.innerHeight + window.scrollY > document.body.clientHeight) {
            document.getElementById("scroller").style.display='hide';
        } else {
            document.getElementById("scroller").style.display='block';
        }
    }
</script>
$(document).ready(function(){
$("div").scroll(function(){
        if($("div").scrollTop()==0)
            $("scroller").show();   
        else
               $("scroller").hide(); 
});

});

在JQuery中,您可以使用它。

尝试以下

$(window).scroll(function() {
   if($(window).scrollTop() + $(window).height() == $(document).height()) {
       $('#scroller').hide('slow');
   } 
   else if($(window).scrollTop()==0)
   {
          $('#scroller').show('slow');
   }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM