[英]How to automatically go to top after x seconds if user scrolled down
I'm new.我是新来的。 New to JS/CSS and Stack Overflow. JS/CSS 和 Stack Overflow 的新手。
I have a div
box with the CSS property overflow: auto
.我有一个带有 CSS 属性overflow: auto
的div
框。 If a user scrolls down in the DIV box, it should automatically scroll to the top again after x seconds.如果用户在 DIV 框中向下滚动,它应该会在 x 秒后自动再次滚动到顶部。
Is this possible?这可能吗? And how is this possible?这怎么可能?
I think it should detect if the scrollbar is not in the start position to let the countdown count.我认为它应该检测滚动条是否不在开始位置让倒计时。 After x seconds it should go back to top. x 秒后它应该回到顶部。 Right?对?
Thank you.谢谢你。
Here's a non jQuery version:这是一个非 jQuery 版本:
var timeout; function returnToTop() { if (!timeout) { timeout = setTimeout(() => { document.getElementById("scrollable").scroll(0, 0); timeout = null; }, 5000) } }
#scrollable { height: 200px; overflow-y: scroll; } #interior { height: 500px; }
<div id="scrollable" onscroll="returnToTop()"> <div id="interior"> scroll me! </div> </div>
try this with setTimeOut() function
and page idle用setTimeOut() function
和页面空闲试试这个
$(window).scroll(function() { if($(window).scrollTop() + $(window).height() == $(document).height()) { setTimeout(function(){ $('html,body').animate({ scrollTop: 0 }, 700); }, 5000); } }); var idleTime = 0; $(document).ready(function () { //Increment the idle time counter every second. var idleInterval = setInterval(timerIncrement, 1000); // 1 second //Zero the idle timer on mouse movement. $(this).mousemove(function (e) { idleTime = 0; }); $(this).keypress(function (e) { idleTime = 0; }); }); function timerIncrement() { idleTime = idleTime + 1; if (idleTime > 10) { // 10 seconds idleTime = 0; $('html,body').animate({ scrollTop: 0 }, 700); } }
#content { height: 2000px; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="content">Scroll ↓</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.