简体   繁体   English

滚动时显示/隐藏 div 或图像

[英]Show/hide div or image when scrolling

Is there a way to show an image or a div when scrolling down a web page and hide it when not scrolling and vice versa?有没有办法在向下滚动网页时显示图像或 div 并在不滚动时隐藏它,反之亦然?

So in the code below the red div would be displayed when not scrolling, and the green div would be displayed only when scrolling.所以在下面的代码中,红色的 div 会在不滚动时显示,而绿色的 div 只会在滚动时显示。

 .square { position: fixed; width: 100px; height: 100px; } .green { background: green; display: none; } .red { background: red; } .container { width: 100%; height: 3000px; }
 <div class="container"> <div class="square green"></div> <div class="square red"></div> </div>

The end goal is to achieve something like this: https://mailchimp.com/annual-report/ where the character appears to be walking when the user scrolls, and stands still when the user stops.最终目标是实现这样的目标: https ://mailchimp.com/annual-report/ 当用户滚动时角色似乎正在行走,而当用户停止时则静止不动。 Is this easily achievable?这很容易实现吗?

You just need an eventListener that listen to a scroll event.您只需要一个监听scroll事件的eventListener However this has the issue that it only recoginze when you scroll but not when you stop scrolling.然而,这有一个问题,它只在您滚动时识别,而不是在您停止滚动时识别。 For that you can use this answer that explains how to listen for a "scroll-stop"为此,您可以使用解释如何收听“滚动停止”的答案

To make the code shorter and easier, I removed your display: none from the green box.为了使代码更短更容易,我从绿色框中删除了您的display: none I added a new class d-none that contains this proeprty now instead.我现在添加了一个包含此属性的新类d-none By default it is added to the green box.默认情况下,它被添加到绿色框中。

With classList.toggle('d-none') I can toggle class within both boxes which makes it easier then to address and then add or remove the class for every box on its own.使用classList.toggle('d-none')我可以在两个框中切换类,这样可以更轻松地解决然后为每个框单独添加或删除类。

 var timer = null; var box = document.querySelectorAll('.square'); window.addEventListener('scroll', function() { if (timer !== null) { clearTimeout(timer); } else { box.forEach(el => el.classList.toggle('d-none')); } timer = setTimeout(function() { box.forEach(el => el.classList.toggle('d-none')); }, 150); }, false);
 .d-none { display: none; } .square { position: fixed; width: 100px; height: 100px; } .green { background: green; /* display: none; */ /* removed */ } .red { background: red; } .container { width: 100%; height: 3000px; }
 <div class="container"> <div class="square green d-none"></div> <div class="square red"></div> </div>

You just need a setTimeout function:你只需要一个 setTimeout 函数:

 (function($) { $(function() { $(window).scroll(function() { $('.square.red').show() $('.square.green').hide() clearTimeout($.data(this)); $.data(this, setTimeout(function() { $('.square.red').hide() $('.square.green').show() }, 250)); }); }); })(jQuery);
 .square { position: fixed; width: 100px; height: 100px; } .green { background: green; } .red { background: red; display: none; } .container { width: 100%; height: 3000px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="container"> <div class="square green"></div> <div class="square red"></div> </div>

使用javascript显示/隐藏<div>滚动时</div><div id="text_translate"><p>我想制作一个采用性的粘性导航栏。 我以前从未用 Javascript 编写过自己的代码。</p><p> 我的解决方案是制作两个基于滚动显示/隐藏的粘性导航栏。 我的解决方案是这样的:</p><ul><li> 首先,在完成任何滚动之前,我的第一个导航栏(黄色)将可见并位于距顶部 100px 的位置,即top:100px 。</li><li> 然后,当启动滚动时,我希望这个黄色&lt;div&gt;使用display:none消失。</li><li> 同样在滚动时,在黄色导航栏消失的同时,我会显示带有top:200px的橙色导航栏。</li><li> 现在,橙色条从一开始就(错误地)显示,但它不应该......在任何给定时间只应显示一个条。 我希望栏,即两个&lt;div&gt;元素,在多次上下滚动时也能保持出现/消失。</li></ul><p> 我的 javascript 有问题,codepen 抱怨$未定义。</p><p> <a href="https://codepen.io/G1111/pen/RwBPrPR" rel="nofollow noreferrer">https://codepen.io/G1111/pen/RwBPrPR</a> </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $(window).scroll(function() { Testvariable = $(window).scrollTop(); if (Testvariable == 0) { document.getElementById("stickys").style.display = "normal"; document.getElementById("stickys2").style.display = "none"; } else { document.getElementById("stickys").style.display = "none"; document.getElementById("stickys2").style.display = "normal"; } });</pre><pre class="snippet-code-css lang-css prettyprint-override"> #stickys { top: 100px;important: background-color; yellow: height; 100px: position; fixed: position; fixed:important; box-sizing: border-box; margin: 0 0% 0 0%;important: padding; 0:important; width: calc(60vw - 0%); left: calc(20vw - 0%); right: calc(20vw - 0%); width: calc(100vw - 0%); left: 0px; right: 0px; opacity: 1; } #stickys2 { top: 200px;important: background-color; orange: height; 100px: position; fixed: position; fixed:important; box-sizing: border-box; margin: 0 0% 0 0%;important: padding; 0:important; width: calc(60vw - 0%); left: calc(20vw - 0%); right: calc(20vw - 0%); width: calc(100vw - 0%); left: 0px; right: 0px; opacity: 1; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;div id="stickys"&gt; My first sticky navigation bar. Show initially, then hide when slighest scroll, that is when Testvariable&gt;0. Here, top:100px &lt;/div&gt; &lt;div id="stickys2"&gt; My second sticky navigation bar. Hide initially, then show when slighest scroll, that is when Testvariable&gt;0. Here, top: 200px. &lt;/div&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;</pre></div></div><p></p></div> - Using javascript to show/hide <div> when scrolling

暂无
暂无

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

相关问题 使用javascript显示/隐藏<div>滚动时</div><div id="text_translate"><p>我想制作一个采用性的粘性导航栏。 我以前从未用 Javascript 编写过自己的代码。</p><p> 我的解决方案是制作两个基于滚动显示/隐藏的粘性导航栏。 我的解决方案是这样的:</p><ul><li> 首先,在完成任何滚动之前,我的第一个导航栏(黄色)将可见并位于距顶部 100px 的位置,即top:100px 。</li><li> 然后,当启动滚动时,我希望这个黄色&lt;div&gt;使用display:none消失。</li><li> 同样在滚动时,在黄色导航栏消失的同时,我会显示带有top:200px的橙色导航栏。</li><li> 现在,橙色条从一开始就(错误地)显示,但它不应该......在任何给定时间只应显示一个条。 我希望栏,即两个&lt;div&gt;元素,在多次上下滚动时也能保持出现/消失。</li></ul><p> 我的 javascript 有问题,codepen 抱怨$未定义。</p><p> <a href="https://codepen.io/G1111/pen/RwBPrPR" rel="nofollow noreferrer">https://codepen.io/G1111/pen/RwBPrPR</a> </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-js lang-js prettyprint-override"> $(window).scroll(function() { Testvariable = $(window).scrollTop(); if (Testvariable == 0) { document.getElementById("stickys").style.display = "normal"; document.getElementById("stickys2").style.display = "none"; } else { document.getElementById("stickys").style.display = "none"; document.getElementById("stickys2").style.display = "normal"; } });</pre><pre class="snippet-code-css lang-css prettyprint-override"> #stickys { top: 100px;important: background-color; yellow: height; 100px: position; fixed: position; fixed:important; box-sizing: border-box; margin: 0 0% 0 0%;important: padding; 0:important; width: calc(60vw - 0%); left: calc(20vw - 0%); right: calc(20vw - 0%); width: calc(100vw - 0%); left: 0px; right: 0px; opacity: 1; } #stickys2 { top: 200px;important: background-color; orange: height; 100px: position; fixed: position; fixed:important; box-sizing: border-box; margin: 0 0% 0 0%;important: padding; 0:important; width: calc(60vw - 0%); left: calc(20vw - 0%); right: calc(20vw - 0%); width: calc(100vw - 0%); left: 0px; right: 0px; opacity: 1; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt; &lt;div id="stickys"&gt; My first sticky navigation bar. Show initially, then hide when slighest scroll, that is when Testvariable&gt;0. Here, top:100px &lt;/div&gt; &lt;div id="stickys2"&gt; My second sticky navigation bar. Hide initially, then show when slighest scroll, that is when Testvariable&gt;0. Here, top: 200px. &lt;/div&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;.&lt;br&gt;</pre></div></div><p></p></div> - Using javascript to show/hide <div> when scrolling 用户开始滚动时暂时隐藏Div,然后显示Div - Temporarily Hide Div then Show Div when User starts Scrolling jQuery:当图像悬停时隐藏并显示div - Jquery: Hide and show a div when image is hover 滚动后显示div,向上滚动时隐藏 - Show div after scroll and hide when scrolling back up 如何在向下滚动时隐藏div,然后向上滚动显示 - How to hide div when scrolling down and then show scroll up 滚动“返回顶部”链接时获取div显示/隐藏 - Get div to show/hide when scrolling for a “back to top” link 使用jQuery或JS滚动时如何隐藏和显示div - How to hide and show div when scrolling using jQuery or JS 显示/隐藏图像DIV - Show/Hide Image DIV 滚动时如何使用jQuery显示/隐藏div? - How to show/hide div with jQuery while scrolling? 滚动到div顶部时,隐藏另一个div - When scrolling to top of div hide another div
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM