gpt4 book ai didi

css - 在React中创建平滑滚动(浏览器友好)

转载 作者:行者123 更新时间:2023-11-28 19:16:13 31 4
gpt4 key购买 nike

我目前正在建立我的网站React。但是refs scroll / css与Safari和某些其他浏览器不兼容。在野生动物园中,它只是跳到div而不是滚动到div。

不兼容

html {
scroll-behavior: smooth;
}


什么是创建所有浏览器(最新版本)平滑滚动功能的最佳库(处于活动状态)或方法。我不希望它太重,因为它会经常使用。

这是我当前的滚动基于的内容: ReactJS how to scroll to an element

最佳答案

这篇文章解决了您的问题:Solve smooth scroll on all browser



function SmoothVerticalScrolling(e, time, where) {
var eTop = e.getBoundingClientRect().top;
var eAmt = eTop / 100;
var curTime = 0;
while (curTime <= time) {
window.setTimeout(SVS_B, curTime, eAmt, where);
curTime += time / 100;
}
}

function SVS_B(eAmt, where) {
if(where == "center" || where == "")
window.scrollBy(0, eAmt / 2);
if (where == "top")
window.scrollBy(0, eAmt);
}





并通过以下方式致电:



SmoothVerticalScrolling(myelement, 275, "center");

关于css - 在React中创建平滑滚动(浏览器友好),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58266742/

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