简体   繁体   English

在滚动点后显示和隐藏div

[英]Show and hiding div after scroll point

I am using this script to hide a div and show it when have scrolled past a certain point in the page. 我正在使用此脚本隐藏div并在滚动到页面中的特定点时显示它。 This is working fine, but when I scroll back up to the top, the div then stays visible. 这工作正常,但是当我向上滚动到顶部时,div仍然可见。 Could anyone help me with an amendment I can make to the code to hide the div again when scrolling back above the desired point? 有人能帮我修改一下代码,使其在回滚到所需点上方时再次隐藏div吗?

Thanks, T 谢谢,T

$(document).ready( function() {
$("#dvid").hide(); //hide your div initially
var topOfOthDiv = $("#othdiv").offset().top;
$(window).scroll(function() {
    if($(window).scrollTop() > topOfOthDiv) { //scrolled past the other div?
        $("#dvid").show(); //reached the desired point -- show div
    }
});
});
$(document).ready( function() {
    $("#dvid").hide(); //hide your div initially
    var topOfOthDiv = $("#othdiv").offset().top;
    $(window).scroll(function() {
        if($(window).scrollTop() > topOfOthDiv) { //scrolled past the other div?
            $("#dvid").show(); //reached the desired point -- show div
        }
        else{
            $("#dvid").hide(); //else above the desired point -- hide div
        }
    });
});

First, check the element visibility: 首先,检查元素的可见性:

var rect = element.getBoundingClientRect();
var visible = Boolean(
    rect.top >= 0
    && rect.left >= 0
    && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight)
    && rect.right <= (window.innerWidth || document.documentElement.clientWidth)
 );

Then bind event to revalidate visibility: 然后绑定事件以重新验证可见性:

jQuery(window).bind('DOMContentLoaded load resize scroll', fn);
// fn - is your function to show/hide elements in accordance with previous statement

So, the final code would be: 因此,最终代码将是:

$(document).ready( function() {
    var checkVisibility = function () {
        $("#dvid, #othdiv").each(function () {
            var rect = this.getBoundingClientRect(),
                visible = Boolean(
                    rect.top >= 0
                    && rect.left >= 0
                    && rect.bottom <= (window.innerHeight || document.documentElement.clientHeight)
                    && rect.right <= (window.innerWidth || document.documentElement.clientWidth)
                 );
            $(this)[visible ? 'show' : 'hide']();
        });
    };
    //hide your divs initially
    $("#dvid, #othdiv").hide(); 
    jQuery(window).bind('DOMContentLoaded load resize scroll', checkVisibility);
});
$(document).ready( function() {
$("#dvid").hide(); //hide your div initially
var topOfOthDiv = $("#othdiv").offset().top;
$(window).scroll(function() {
    if($(window).scrollTop() > topOfOthDiv) { //scrolled past the other div?
        $("#dvid").show(); //reached the desired point -- show div
    } else {
        $("dvid").show(); //hide div
    }
});
});

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

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