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>
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.