简体   繁体   English

当另一个div滚出屏幕时显示一个div。 产生不必要的闪烁

[英]Show one div when another div scrolls off screen. Getting unwanted flashes

I would like to show one div (.cu5-topbar) when another div (.cu5box-box) scrolls off screen. 我想显示另一个div(.cu5box-box)在屏幕外滚动时显示一个div(.cu5-topbar)。 The problem is that the the .cu5-topbar div is showing up right as the .cu5box-box div is leaving the screen. 问题在于,.cu5-topbar div正在显示,而.cu5box-box div正在离开屏幕。 That's also causing the .cu5-topbar to flashing for a few seconds while the two divs overlap each other. 这也导致.cu5-topbar闪烁几秒钟,而两个div彼此重叠。 Here is my code so far: 到目前为止,这是我的代码:

var scroll_start = 0;
var startchange = jQuery('.cu5box-box');
var offset = startchange.offset();
if (startchange.length) {
    jQuery(document).scroll(function () {
        scroll_start = jQuery(this).scrollTop();
        if (scroll_start > offset.top) {
            jQuery('.cu5box-box').fadeOut(400);
            jQuery('.cu5-topbar').fadeIn(400);
        } else {
            jQuery('.cu5box-box').fadeIn(400);
            jQuery('.cu5-topbar').fadeOut(400);
        }
    });
}

https://jsfiddle.net/zgu70p4m/ https://jsfiddle.net/zgu70p4m/

I would like for the .cu5-topbar div to show up as soon the .cu5box-box div is completely off of the screen and I would like for the .cu5-topbar div to disappear as soon as the .cu5box-box div comes onto the screen. 我希望.cu5-topbar div出现在.cu5box-box div完全不在屏幕上的情况下,我希望.cu5-topbar div一旦.cu5box-box div出现就消失到屏幕上。

Try the below, you had not included jQuery in your jsfiddle example. 请尝试以下操作,您没有在jsfiddle示例中包含jQuery。

You also needed to attach the scroll event to the window. 您还需要将滚动事件附加到窗口。 Finally the offset needed the elements height added to the total to make sure you where getting the correct position of the end of the element. 最后,偏移量需要将元素高度添加到总和中,以确保您在正确位置获得元素末端的位置。

var scroll_start = 0;
var startchange = jQuery('.cu5box-box');
var offset = startchange.offset();
if (startchange.length) {
    $(window).scroll(function () {
        scroll_start = $(this).scrollTop();
        if (scroll_start > offset.top + startchange.height()) {
            $('.cu5-topbar').fadeOut(400);
        } else {
            $('.cu5-topbar').fadeIn(400);
        }
    });
}

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

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