简体   繁体   English

Javascript 自动滚动到 div

[英]Javascript auto-scroll to div

so ive been looking at different articles on stack overflow and can't seem to find what im looking for.所以我一直在看关于堆栈溢出的不同文章,但似乎找不到我要找的东西。

So I just realized that the css property scroll-behavior doesn't work on safari/ ios browsers.所以我才意识到 css 属性 scroll-behavior 在 safari/ios 浏览器上不起作用。 So now im thinking i should use javascript to solve this.所以现在我想我应该使用javascript来解决这个问题。

This is what ive tried but it doesn't actually scroll it still jumps to the section, which means the scrollIntoView isn't working.这是我尝试过的但实际上并没有滚动它仍然跳转到该部分,这意味着 scrollIntoView 不起作用。 Any suggestions?有什么建议?

function subnav_link_click1(){
    var subnav_link_value = document.getElementById('subnav_link_id1').href;
    subnav_link_value.scrollIntoView(true)
}

 const scrollUp = () => { const anchorEl = document.getElementById('myId'); anchorEl.scrollIntoView({ block: 'end', behavior: 'smooth' }); }
 .container { justify-content: space-between; flex-direction: column; height: 1000px; display: flex; } .container .btn-holder { display: flex; }
 <div class="container"> <a href="https://google.com" id="myId">google</a> <button class="btn-holder" onclick="scrollUp()">scroll up</button> </div>

You have an example of how it should work here.你有一个它应该如何在这里工作的例子。

Scrollintoview - https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView滚动视图 - https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView

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

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