0

How to smooth scroll using JavaScript? I want to Smooth Scroll Down To Div Can anyone know how to add smooth scroll in the JavaScript code below

function scroll_to(val) {
 if(val== "Scroll down to div id")  
    document.getElementById('divid11').scrollIntoView();
}
<select onchange="scroll_to(this.value);">
<option>1111111</option>
<option>2222222</option>
<option >Scroll down to div id</option>
<option>4444444</option>
<option>5555555</option>
<option>6666666</option>
</select>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br>
<br><br><br><br><br><br><br><br><br><br><br><br><br><br>
    <div id='divid11'>scroll</div>

2 Answers2

2

This is in general already answered here. So using JQuerys .animate function in combination of ScrollTop will do the trick for you.

For doing this without JQuery you can follow this tutorial.

Community
  • 1
  • 1
nameless
  • 1,483
  • 5
  • 32
  • 78
0

Use this script in your body for smooth scroll.

<script type="text/javascript">
    $(function() {
        $('a[href*=#]:not([href=#])').click(function() {
            if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
                var target = $(this.hash);
                target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
                if (target.length) {
                    $('html,body').animate({
                        scrollTop: target.offset().top
                    }, 1000);
                    return false;
                }
            }
        });
    });
    </script>
Gautham SK
  • 69
  • 2
  • 10