简体   繁体   English

如何滚动到所选div的底部?

[英]How to scroll to the bottom of the selected div?

How can I scroll to the bottom of the desired div?如何滚动到所需 div 的底部? Right now, it will add the height value to the scrollTop which will made the scroll to scroll to only that specific height.现在,它会将高度值添加到 scrollTop,这将使滚动仅滚动到该特定高度。 I want it to scroll down to the last of the 5th div.我希望它向下滚动到第 5 个 div 的最后一个。 There are 9 h1, so I want it to scroll upto the bottom of the 5th h1有 9 个 h1,所以我希望它滚动到第 5 个 h1 的底部

 const scrollbtn = document.getElementById('scrl'); var objDiv = document.querySelectorAll('.each'); const mycontainer = document.querySelector('.container'); scrollbtn.addEventListener('click', () => { const itemHeight = objDiv[5].scrollHeight; mycontainer.scrollTop += itemHeight; })
 body { background: tomato; scroll-behavior: smooth; }.container { max-height: 200px; overflow-y: scroll; background: white; scroll-behavior: smooth; }.each { padding-top: 10px; background: lightblue; }
 <div class="container"> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> </div> <button id="scrl"> scroll </button>

You can use scrollIntoView您可以使用scrollIntoView

scrollbtn.addEventListener('click', () => {
  objDiv[objDiv.length-1].scrollIntoView(); // change objDiv.length-1 to 4 if you want to scroll to the 5th H
})

You can add options like {behavior: "smooth", block: "end", inline: "nearest"}您可以添加选项,例如{behavior: "smooth", block: "end", inline: "nearest"}

 const scrollbtn = document.getElementById('scrl'); var objDiv = document.querySelectorAll('.each'); const mycontainer = document.querySelector('.container'); scrollbtn.addEventListener('click', () => { objDiv[objDiv.length - 1].scrollIntoView({ behavior: "smooth", block: "end", inline: "nearest" }) })
 body { background: tomato; scroll-behavior: smooth; }.container { max-height: 200px; overflow-y: scroll; background: white; scroll-behavior: smooth; }.each { padding-top: 10px; background: lightblue; }
 <button id="scrl"> scroll </button> <div style="height:500px">Dummy to see the behaviour of the page</div> <div class="container"> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> </div>

Jump to the 5th div immediately立即跳转到第 5 格

 const scrollbtn = document.getElementById('scrl'); var objDiv = document.querySelectorAll('.each'); const mycontainer = document.querySelector('.container'); scrollbtn.addEventListener('click', () => { mycontainer.scrollTop = objDiv[0].scrollHeight * 5 //the first height multiple by 5 })
 body { background: tomato; scroll-behavior: smooth; }.container { max-height: 200px; overflow-y: scroll; background: white; scroll-behavior: smooth; }.each { padding-top: 10px; background: lightblue; }
 <div class="container"> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> </div> <button id="scrl"> scroll </button>

Go through one by one till reaching out the the 5th Go 一个接一个到第5个

 const scrollbtn = document.getElementById('scrl'); var objDiv = document.querySelectorAll('.each'); const mycontainer = document.querySelector('.container'); let interval scrollbtn.addEventListener('click', () => { if (interval) { clearInterval(interval) } let index = 1 interval = setInterval(() => { const itemHeight = objDiv[0].scrollHeight; mycontainer.scrollTop += itemHeight; index++ if (index === 6) { //if it's already gone to the 5th, break the interval clearInterval(interval) } }, 300) })
 body { background: tomato; scroll-behavior: smooth; }.container { max-height: 200px; overflow-y: scroll; background: white; scroll-behavior: smooth; }.each { padding-top: 10px; background: lightblue; }
 <div class="container"> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> <h1 class="each">Test</h1> </div> <button id="scrl"> scroll </button>

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

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