简体   繁体   English

如何让jQuery在鼠标滚动时隐藏div?

[英]How to get jQuery to hide a div on mouse scroll?

I have some jQuery code that loads a div with few elements on toggle but I notice that when I scroll the div stays loaded so how do I hide the div on scroll is my question. 我有一些jQuery代码,可在切换时加载包含少量元素的div,但我注意到在滚动div时仍保持加载状态,因此如何在滚动时隐藏div是我的问题。

This is a part of the code - the way it looks normally is like: 这是代码的一部分-正常外观如下:

how it looks normally when loaded 加载后的外观如何正常

The problem: 问题:

notice that it moves down when scrolled and I want to prevent that 注意滚动时它会向下移动,我想防止

$(document).ready(function () {
    // $(".navbar").hide(); //well this works the same for loading pages
    $("#loadindex").load("codeParts/headerDreiHome.html") // loads the second main header part for the navbar
    $("#loadindex").hide();
    $("#loadindex1").load("codeParts/headerDreiHtmlCss.html") // loads the second main header part for the navbar
    $("#loadindex1").hide();
    $("#loadindex2").load("codeParts/headerDreiJava.html") // loads the second main header part for the navbar
    $("#loadindex2").hide();
    $("#loadindex3").load("codeParts/headerDreiJavaScript.html") // loads the second main header part for the navbar
    $("#loadindex3").hide();
    $("#loadindex4").load("codeParts/headerDreiAboutMe.html") // loads the second main header part for the navbar
    $("#loadindex4").hide();
    $("#loadindex5").load("codeParts/headerDreiContactMe.html") // loads the second main header part for the navbar
    $("#loadindex5").hide();
    $("#toggleHome").click(function () {
        $("#loadindex1").hide();
        $("#loadindex2").hide();
        $("#loadindex3").hide();
        $("#loadindex4").hide();
        $("#loadindex5").hide();
        $("#loadindex").toggle("slow", function () {
            setTimeout(function () {
                $("#loadindex").hide();
            }, 8000);
        });
    });
    $("#toggleHtmlCss").click(function () {
        $("#loadindex").hide();
        $("#loadindex2").hide();
        $("#loadindex3").hide();
        $("#loadindex4").hide();
        $("#loadindex5").hide();
        $("#loadindex1").toggle("slow", function () {
            setTimeout(function () {
                $("#loadindex1").hide();
            }, 8000);
        });
    });
    $("#toggleJava").click(function () {
        $("#loadindex1").hide();
        $("#loadindex").hide();
        $("#loadindex3").hide();
        $("#loadindex4").hide();
        $("#loadindex5").hide();
        $("#loadindex2").toggle("slow", function () {
            setTimeout(function () {
                $("#loadindex2").hide();
            }, 8000);
        });
    });
    $("#toggleJavaScript").click(function () {
        $("#loadindex1").hide();
        $("#loadindex2").hide();
        $("#loadindex").hide();
        $("#loadindex4").hide();
        $("#loadindex5").hide();
        $("#loadindex3").toggle("slow", function () {
            setTimeout(function () {
                $("#loadindex3").hide();
            }, 8000);
        });
    });
    $("#toggleAboutMe").click(function () {
        $("#loadindex1").hide();
        $("#loadindex2").hide();
        $("#loadindex").hide();
        $("#loadindex3").hide();
        $("#loadindex5").hide();
        $("#loadindex4").toggle("slow", function () {
            setTimeout(function () {
                $("#loadindex4").hide();
            }, 8000);
        });
    });
    $("#toggleContactMe").click(function () {
        $("#loadindex1").hide();
        $("#loadindex2").hide();
        $("#loadindex3").hide();
        $("#loadindex4").hide();
        $("#loadindex").hide();
        $("#loadindex5").toggle("slow", function () {
            setTimeout(function () {
                $("#loadindex5").hide();
            }, 8000);
        });
    });
});

 $(window).scroll(function() { if ($(this).scrollTop() > 0) { $('.divTo').fadeOut(); } else { $('.divTo').fadeIn(); } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <div class="divTo"> Content </div> </div> 

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

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