Javascript 在滚动点后显示和隐藏div

Javascript 在滚动点后显示和隐藏div,javascript,jquery,Javascript,Jquery,我使用这个脚本隐藏一个div,当它滚动到页面中的某个点时显示它。这工作正常,但当我向上滚动到顶部时,div将保持可见。有没有人能帮我修改一下代码,在滚动到所需点上方时再次隐藏div 谢谢,T $(document).ready( function() { $("#dvid").hide(); //hide your div initially var topOfOthDiv = $("#othdiv").offset().top; $(window).scroll(function() {

我使用这个脚本隐藏一个div,当它滚动到页面中的某个点时显示它。这工作正常,但当我向上滚动到顶部时,div将保持可见。有没有人能帮我修改一下代码,在滚动到所需点上方时再次隐藏div

谢谢,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
    }
});
});

首先,检查元素可见性:

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)
 );
jQuery(window).bind('DOMContentLoaded load resize scroll', fn);
// fn - is your function to show/hide elements in accordance with previous statement
因此,最终的代码是:

$(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);
});
$(文档).ready(函数(){
var checkVisibility=函数(){
$(“#dvid,#othdiv”)。每个(函数(){
var rect=this.getBoundingClientRect(),
可见=布尔值(
rect.top>=0
&&rect.left>=0

&&rect.bottom Chirag Ravindra非常感谢:)非常欢迎。如果回答了您的问题,请接受答案:)
$(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
    }
});
});