[英]jQuery auto scroll div to div and back to top
I found this code for autoscroll a page. 我找到了用于自动滚动页面的代码。
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(document).ready(function () {
var myInterval = false;
myInterval = setInterval(AutoScroll, 5000);
function AutoScroll() {
var iScroll = $(window).scrollTop();
iScroll = iScroll + 500;
$('html, body').animate({
scrollTop: iScroll
}, 1000);
}
$(window).scroll(function () {
var iScroll = $(window).scrollTop();
if (iScroll == 0) {
myInterval = setInterval(AutoScroll, 5000);
}
if (iScroll + $(window).height() == $(document).height()) {
clearInterval(myInterval);
setTimeout(function(){ window.location.href = "index.php"; },3000)
}
});
});
});//]]>
</script>
My page looks like this: 我的页面如下所示:
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>
<div id="div5"></div>
My goal is to autoscroll from div to div after 20 seconds en after the last div back to top. 我的目标是在最后一个div返回顶部20秒后从div自动滚动到div。
My Question is: 我的问题是:
How to make it work scrolling from div to div? 如何使其从div滚动到div?
I use window.location.href = "index.php" for refreshing the page en start over again. 我使用window.location.href =“ index.php”刷新页面,重新开始。 Is there a different way to achieve the same without a refresh?
是否有另一种无需刷新即可达到相同目的的方法? So go back to the top div and refresh the content of the page?
因此,返回顶部div并刷新页面内容吗?
AutoScroll
function, get the element at the current index, get the offset from the top of the page for that element, and scroll to that. AutoScroll
函数中,获取当前索引处的元素,获取该元素与页面顶部的偏移量,然后滚动到该位置。 Then increment the index value. Something like this should work: 这样的事情应该起作用:
<script type='text/javascript'>
$(window).load(function(){
$(document).ready(function () {
var myInterval = false;
var index = 0;
var elements = ["#div1","#div2","#div3","#div4","#div5"];
myInterval = setInterval(AutoScroll, 5000);
function AutoScroll() {
$('html, body').animate({
scrollTop: $(elements[index]).offset().top
}, 1000);
if(index < (elements.length - 1)){
index++;
} else {
index = 0;
}
}
});
});
</script>
See this JSFiddle: https://jsfiddle.net/3gb5uLgs/ 看到这个JSFiddle: https ://jsfiddle.net/3gb5uLgs/
$(function() { $('a[href*=#]:not([href=#])').click(function() { if (location.pathname.replace(/^\\//,'') == this.pathname.replace(/^\\//,'') && location.hostname == this.hostname) { var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); if (target.length) { $('html,body').animate({ scrollTop: target.offset().top }, 1000); return false; } } }); });
2: You can use location.reload();
2:您可以使用
location.reload();
. 。
You need to add ScrollTop function, and use it when you reach bottom. 您需要添加ScrollTop函数,并在到达底部时使用它。
$(document).ready(function () {
var myInterval = false;
myInterval = setInterval(AutoScroll, 5000);
function AutoScroll() {
var iScroll = $(window).scrollTop();
iScroll = iScroll + 500;
$('html, body').animate({
scrollTop: iScroll
}, 1000);
}
//The function scroll to 0 position.
function scrollTop()
{
$('html, body').animate({
scrollTop: 0
}, 1000);
}
$(window).scroll(function () {
var iScroll = $(window).scrollTop();
if (iScroll == 0) {
clearInterval(myInterval);
myInterval = setInterval(AutoScroll, 5000);
}
if (iScroll + $(window).height() == $(document).height()) {
clearInterval(myInterval);
//Instead refresh, I just scrolled to top.
setTimeout(scrollTop,5000)
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.