gpt4 book ai didi

javascript - scrollIntoView 在边缘不平滑

转载 作者:行者123 更新时间:2023-11-29 23:18:43 27 4
gpt4 key购买 nike

我正在使用 vanilla JS 让按钮平滑地向下滚动到 div 元素(目标 id)

我的代码是这样的:

this.myButton.addEventListener("click",load_section);
function load_section() {
document.querySelector('#myDiv').scrollIntoView({ block: 'end', behavior: 'smooth' });
}

在所有其他浏览器中,这会平滑地滚动到该部分。在 Edge 中,它只是跳转到它。我需要更改什么才能使其平滑向下滚动到该部分

最佳答案

有关平滑滚动的更全面的方法列表,请参阅我的回答 here .


要在准确的时间内滚动到某个位置,window.requestAnimationFrame可以投入使用,每次计算适当的当前位置。要滚动到某个元素,只需将 y 位置设置为 element.offsetTop

/*
@param pos: the y-position to scroll to (in pixels)
@param time: the exact amount of time the scrolling will take (in milliseconds)
*/
function scrollToSmoothly(pos, time) {
var currentPos = window.pageYOffset;
var start = null;
if(time == null) time = 500;
pos = +pos, time = +time;
window.requestAnimationFrame(function step(currentTime) {
start = !start ? currentTime : start;
var progress = currentTime - start;
if (currentPos < pos) {
window.scrollTo(0, ((pos - currentPos) * progress / time) + currentPos);
} else {
window.scrollTo(0, currentPos - ((currentPos - pos) * progress / time));
}
if (progress < time) {
window.requestAnimationFrame(step);
} else {
window.scrollTo(0, pos);
}
});
}

演示:

function scrollToSmoothly(pos, time) {
var currentPos = window.pageYOffset;
var start = null;
if(time == null) time = 500;
pos = +pos, time = +time;
window.requestAnimationFrame(function step(currentTime) {
start = !start ? currentTime : start;
var progress = currentTime - start;
if (currentPos < pos) {
window.scrollTo(0, ((pos - currentPos) * progress / time) + currentPos);
} else {
window.scrollTo(0, currentPos - ((currentPos - pos) * progress / time));
}
if (progress < time) {
window.requestAnimationFrame(step);
} else {
window.scrollTo(0, pos);
}
});
}

document.getElementById("toElement").addEventListener("click", function(e){
scrollToSmoothly(document.querySelector('div').offsetTop, 500 /* milliseconds */);
});
document.getElementById("backToTop").addEventListener("click", function(e){
scrollToSmoothly(0, 500);
});
<button id="toElement">Scroll To Element</button>
<div style="margin: 1000px 0px; text-align: center;">Div element
<button id="backToTop">Scroll back to top</button>
</div>

SmoothScroll.js library也可以使用,它支持滚动到页面上的元素以及更复杂的功能,例如垂直和水平平滑滚动、在其他容器元素内滚动、不同的缓动行为、从当前位置相对滚动等。

smoothScroll({toElement: document.getElementById('elementId'), duration: 500});

document.getElementById("toElement").addEventListener("click", function(e){
smoothScroll({toElement: document.querySelector('div'), duration: 500});
});
document.getElementById("backToTop").addEventListener("click", function(e){
smoothScroll({yPos: 'start', duration: 500});
});
<script src="https://cdn.jsdelivr.net/gh/LieutenantPeacock/SmoothScroll@1.2.0/src/smoothscroll.min.js" integrity="sha384-UdJHYJK9eDBy7vML0TvJGlCpvrJhCuOPGTc7tHbA+jHEgCgjWpPbmMvmd/2bzdXU" crossorigin="anonymous"></script>
<button id="toElement">Scroll To Element</button>
<div style="margin: 1000px 0px; text-align: center;">Div element
<button id="backToTop">Scroll back to top</button>
</div>

关于javascript - scrollIntoView 在边缘不平滑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51599677/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com