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>

6
  • Where is the "Scroll" here? Commented Oct 13, 2015 at 18:10
  • 1
    Possible duplicate of JQuery smooth scrolling when clicking an anchor link Commented Oct 13, 2015 at 18:14
  • @allicarn it is only tagged, but it don't use jQuery ! Commented Oct 13, 2015 at 18:22
  • if jQuery is not part of the question, OP should remove the tag. Commented Oct 13, 2015 at 18:22
  • @allicarn, yes you're right ! Commented Oct 13, 2015 at 18:23

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.