简体   繁体   中英

html javascript smooth scroll

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> 

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.

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>

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