简体   繁体   English

使用scrollTop显示/隐藏div

[英]show/hide div using scrollTop

I'm trying to create a script which would show div if 500px < scrollTop < 800px otherwise it would be hidden. 我正在尝试创建一个脚本,如果500px <scrollTop <800px,它将显示div,否则将被隐藏。 So if my scroll is from 0 to 500 and from 800 and more it is hidden and between 500 and 800 it is shown. 因此,如果我的滚动条是从0到500,从800开始,甚至更多,那么它将被隐藏,并且在500到800之间显示。 I'm new to javascript but this is what I have tried: 我是javascript新手,但这是我尝试过的方法:

$(document).ready(function(){ 

$(window).scroll(function(){
    if ($(this).scrollTop() > 500) {
        $('.myDiv').fadeIn();
    } else {
        $('.myDiv').fadeOut();
    }
    if ($(this).scrollTop() > 800) {
        $('.myDiv').fadeOut();
    }
});

However after scrolling to 800 it bugs and starts endlessly hiding and showing. 但是,滚动到800后,它会出错并开始无休止地隐藏和显示。 Any way to fix it please? 有什么办法解决吗?

$(window).scroll(function(){

    if ($(this).scrollTop() > 800) {
        $('.myDiv').fadeOut();
    }
    else {
       if ($(this).scrollTop() > 500) {
           $('.myDiv').fadeIn();
       } else {
           $('.myDiv').fadeOut();
       }
    }

});

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

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