简体   繁体   English

如何使用Javascript滚动到下一个div?

[英]How to scroll to next div using Javascript?

So I'm making a website with a lot of Divs that take 100% height.所以我正在制作一个包含大量 100% 高度的 Div 的网站。 And I want to make a button so when it's clicked to smoothly scroll to next div.我想制作一个按钮,以便在单击它时平滑滚动到下一个 div。 I've coded something so when its clicked, it scrolls to specific div.我已经编码了一些东西,所以当它被点击时,它会滚动到特定的 div。

 $(".next").click(function() { $('html,body').animate({ scrollTop: $(".p2").offset().top}, 'slow'); });
 body{ margin: 0; height: 100%; } .p1{ height: 100vh; width: 70%; background-color: #2196F3; } .p2{ height: 100vh; width: 70%; background-color: #E91E63; } .p3{ height: 100vh; width: 70%; background-color: #01579B; } .admin{ background-color: #B71C1C; height: 100vh; position: fixed; right: 0%; top: 0%; width: 30%; float: left; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="p1"> </div> <div class="p2"> </div> <div class="p3"> </div> <div class="admin"> <button class="next">NEXT</button> </div>

To make this work you need to identify the currently displayed div .要完成这项工作,您需要识别当前显示的div For that you can apply a class to the element which is currently shown.为此,您可以将类应用于当前显示的元素。 Then you can use next() to traverse through them all.然后你可以使用next()遍历它们。

Also note in the below example the addition of a common class on all elements, .p , in order to DRY up the CSS and make DOM traversal easier.还要注意在下面的示例中,在所有元素上添加了一个公共类.p ,以便干燥 CSS 并使 DOM 遍历更容易。

 $(".next").click(function() { var $target = $('.p.active').next('.p'); if ($target.length == 0) $target = $('.p:first'); $('html, body').animate({ scrollTop: $target.offset().top }, 'slow'); $('.active').removeClass('active'); $target.addClass('active'); });
 body { margin: 0; height: 100%; } .p { height: 100vh; width: 70%; } .p1 { background-color: #2196F3; } .p2 { background-color: #E91E63; } .p3 { background-color: #01579B; } .admin { background-color: #B71C1C; height: 100vh; position: fixed; right: 0%; top: 0%; width: 30%; float: left; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="p p1 active"></div> <div class="p p2"></div> <div class="p p3"></div> <div class="admin"> <button class="next">NEXT</button> </div>

Use same class name for container.Start with first element.Each time click target the next scroller element为容器使用相同的类名。从第一个元素开始。每次点击目标下一个scroller元素

 var f = $('.p1'); var nxt = f; $(".next").click(function() { if (nxt.next('.scroller').length > 0) { nxt = nxt.next('.scroller'); } else { nxt = f; } $('html,body').animate({ scrollTop: nxt.offset().top }, 'slow'); });
 body { margin: 0; height: 100%; } .p1 { height: 100vh; width: 70%; background-color: #2196F3; } .p2 { height: 100vh; width: 70%; background-color: #E91E63; } .p3 { height: 100vh; width: 70%; background-color: #01579B; } .admin { background-color: #B71C1C; height: 100vh; position: fixed; right: 0%; top: 0%; width: 30%; float: left; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="p1 scroller"> </div> <div class="p2 scroller"> </div> <div class="p3 scroller"> </div> <div class="admin"> <button class="next">NEXT</button> </div>

Here is a basic version that moves forward and wraps around to the beginning when it reaches the last slide.这是一个基本版本,它向前移动并在到达最后一张幻灯片时环绕到开头。 We store currSlide outside of the loop and increment the number internally in the function.我们将currSlide存储在循环之外并在函数内部增加数字。

For convenience, I added a slide class to each slide which allows me to:为方便起见,我为每张幻灯片添加了一个slide类,它允许我:

  • easily count the length of the slides轻松计算幻灯片的长度
  • condense the CSS压缩CSS

 let currSlide = 1; const SLIDE_LENGTH = $('.slide').length; $(".next").click(function() { currSlide = currSlide === SLIDE_LENGTH ? 1 : ++currSlide; $('html,body').animate({ scrollTop: $(`.p${currSlide}`).offset().top }, 'slow'); });
 body { margin: 0; height: 100%; } /* Less repetition */ .slide { height: 100vh; width: 70%; } .p1 { background-color: #2196F3; } .p2 { background-color: #E91E63; } .p3 { background-color: #01579B; } .admin { background-color: #B71C1C; height: 100vh; position: fixed; right: 0%; top: 0%; width: 30%; float: left; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="slide p1"></div> <div class="slide p2"></div> <div class="slide p3"></div> <div class="admin"> <button class="next">NEXT</button> </div>

jsFiddle js小提琴

Bonus edit:奖金编辑:

In case you're interested in adding a previous button at some point…如果您有兴趣在某个时候添加上一个按钮……

 let currSlide = 1; const SLIDE_LENGTH = $('.slide').length; function moveSlide() { currSlide = $(this).hasClass("next") ? ++currSlide : --currSlide; if (currSlide < 1) { currSlide = SLIDE_LENGTH; } if (currSlide > SLIDE_LENGTH) { currSlide = 1; } $('html,body').animate({ scrollTop: $(`.p${currSlide}`).offset().top }, 'slow'); } $(".prev, .next").on("click", moveSlide);
 body { margin: 0; height: 100%; } /* Less repetition */ .slide { height: 100vh; width: 70%; } .p1 { background-color: #2196F3; } .p2 { background-color: #E91E63; } .p3 { background-color: #01579B; } .admin { background-color: #B71C1C; height: 100vh; position: fixed; right: 0%; top: 0%; width: 30%; float: left; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="slide p1"></div> <div class="slide p2"></div> <div class="slide p3"></div> <div class="admin"> <button class="prev">PREVIOUS</button> <button class="next">NEXT</button> </div>

jsFiddle js小提琴

Since your question asks "How-to in Javascript", I will answer the question in a few lines of vanilla JS:由于您的问题是“How-to in Javascript”,我将用几行普通的 JS 来回答这个问题:

var p = 1;
const container = document.getElementById('container');
var nextPage = function() {
  var topPos = document.getElementsByClassName('page')[p++].offsetTop;
  container.scrollTo({top: topPos, behavior: 'smooth'});
}

in the above example, page is the class name you assign to your divs you want to scroll to, such as:在上面的示例中, page是您分配给要滚动到的 div 的类名,例如:

<div id="container">
  <div class="page p1"></div>
  <div class="page p2"></div>
  <div class="page p3"></div>
</div>

Since you want to scroll the entire browser window, you just replace由于要滚动整个浏览器窗口,因此只需替换

container.scrollTo({top: topPos, behavior: 'smooth'});

with

window.scrollTo({top: topPos, behavior: 'smooth'});

like so:像这样:

var p = 1;
var nextPage = function() {
  var topPos = document.getElementsByClassName('page')[p++].offsetTop;
  window.scrollTo({top: topPos, behavior: 'smooth'});
}

You can subtract or add the number of pixels you want to offset the topPos if it's not in the right position如果topPos不在正确位置,您可以减去或添加要偏移的像素数

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

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