Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smooth Scrolling for scrollIntoView();

I have a radio button. When clicked upon, I make the page scroll to a div. That's working perfectly.

Here's the HTML for the Radio Button:

<input type="radio" id="tab" onclick="document.getElementById('mydivID').scrollIntoView();" name="tabs1" class="input">

The only thing I want more is for that scroll to be a smooth scroll. Is that possible?

like image 225
RaajTram Avatar asked Mar 10 '26 05:03

RaajTram


2 Answers

document.getElementById('mydivID')
        .scrollIntoView({
            behavior: "smooth"
        });

Let me know how that goes

like image 128
DogPawHat Avatar answered Mar 11 '26 17:03

DogPawHat


here's a solution that worked for me, first get the element you want to scroll your view to then apply a smooth behavior:

    const elm = document.getElementById(id)
    elm.scrollIntoView({ behavior: 'smooth' })

make sure to install this polyfill (npm package):

    # npm install smoothscroll-polyfill --save
like image 39
HiLuLiT Avatar answered Mar 11 '26 18:03

HiLuLiT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!