简体   繁体   中英

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. So now im thinking i should use javascript to solve this.

This is what ive tried but it doesn't actually scroll it still jumps to the section, which means the scrollIntoView isn't working. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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