Javascript 使div在某个点后出现,在另一个点后消失

Javascript 使div在某个点后出现,在另一个点后消失,javascript,jquery,html,css,Javascript,Jquery,Html,Css,:) 我藏一个DIV有点麻烦 滚动donw,我可以让它在到达某一点时出现,但是,我想让它在到达另一点时消失——然后向下滚动,使它再次出现 我尝试添加另一个变量(名称为第三个点),甚至让它消失,但当再次向上滚动时,它开始闪烁( 怎么了?我该怎么办( $(文档).ready(函数(){ $(“#DIV1”).hide();//最初隐藏您的div var topOfOthDiv=$(“#DIV2”).offset().top; $(窗口)。滚动(函数(){ 如果($(window.scrollTop(

:) 我藏一个DIV有点麻烦

滚动donw,我可以让它在到达某一点时出现,但是,我想让它在到达另一点时消失——然后向下滚动,使它再次出现

我尝试添加另一个变量(名称为第三个点),甚至让它消失,但当再次向上滚动时,它开始闪烁(

怎么了?我该怎么办(

$(文档).ready(函数(){
$(“#DIV1”).hide();//最初隐藏您的div
var topOfOthDiv=$(“#DIV2”).offset().top;
$(窗口)。滚动(函数(){
如果($(window.scrollTop()>topOfOthDiv){//滚动到另一个div之前?
$(“#DIV1”).fadeIn(200);//达到了所需的点--show div
}
});
$(窗口)。滚动(函数(){
如果($(window.scrollTop()
这可以通过媒体查询100%完成

.selector {
    display: none; /*by default*/
}


@media screen and (max-width: 1000px) {
    .selector {
        display: block;
    }
}

它闪烁是因为你的if语句都是真的,所以它试图同时淡入淡出

我更新了你的视频,只有在你处于两个div之间时才会淡入

我更改了它,使它首先检查它是否在第一个div之上。如果在第一个div之上,则隐藏特殊div。如果滚动到第一个div之上,则它会检查相对于第二个div的位置,并显示或隐藏它

因此,您的代码如下所示:

$(document).ready(function() {

    $("#DIV1").hide(); //hide your div initially

    var topOfOthDiv1 = $("#DIV2").offset().top;
    var topOfOthDiv3 = $("#DIV3").offset().top;

    $(window).scroll(function() {
        if($(window).scrollTop() < topOfOthDiv1)
        {
             $("#DIV1").fadeOut(200); //reached the desired point -- show div
        }
        else
        {
            if($(window).scrollTop() < topOfOthDiv3) { //scrolled past the other div?
                $("#DIV1").fadeIn(200); //reached the desired point -- show div
            }
            else if($(window).scrollTop() > topOfOthDiv3) { //scrolled past the other div?
                $("#DIV1").fadeOut(200); //reached the desired point -- show div
            }
        }
    });

});
$(文档).ready(函数(){
$(“#DIV1”).hide();//最初隐藏您的div
var topOfOthDiv1=$(“#DIV2”).offset().top;
var topOfOthDiv3=$(“#DIV3”).offset().top;
$(窗口)。滚动(函数(){
if($(窗口).scrollTop()topOfOthDiv3){//滚动到另一个div之前,是否执行其他操作?
$(“#DIV1”).fadeOut(200);//达到所需的点--show div
}
}
});
});

你能不能也加一把小提琴……这是:是的,但问题是高度不是固定的。所以,更简单的方法(我想)是当一个div出现时触发它(一个在中间,另一个在末尾)。哇,差不多了。当它经过DIV2时,我只需要再次隐藏它(特别是像以前一样):)我遗漏了什么?当您再次向上滚动时,当到达DIV2时,它不会隐藏。在我的原始代码中,它确实隐藏了(仅供参考;D)我已经更新了它。我认为它符合您现在想要的:)是的。谢谢你,波波!就这样D
$(document).ready(function() {

    $("#DIV1").hide(); //hide your div initially

    var topOfOthDiv1 = $("#DIV2").offset().top;
    var topOfOthDiv3 = $("#DIV3").offset().top;

    $(window).scroll(function() {
        if($(window).scrollTop() < topOfOthDiv1)
        {
             $("#DIV1").fadeOut(200); //reached the desired point -- show div
        }
        else
        {
            if($(window).scrollTop() < topOfOthDiv3) { //scrolled past the other div?
                $("#DIV1").fadeIn(200); //reached the desired point -- show div
            }
            else if($(window).scrollTop() > topOfOthDiv3) { //scrolled past the other div?
                $("#DIV1").fadeOut(200); //reached the desired point -- show div
            }
        }
    });

});