Smooth scroll without the use of jQuery
Try this smooth scrolling demo, or an algorithm like:
- Get the current top location using
self.pageYOffset
- Get the position of element till where you want to scroll to:
element.offsetTop
- Do a for loop to reach there, which will be quite fast or use a timer to do smooth scroll till that position using
window.scrollTo
See also the other popular answer to this question.
Andrew Johnson's original code:
function currentYPosition() {
// Firefox, Chrome, Opera, Safari
if (self.pageYOffset) return self.pageYOffset;
// Internet Explorer 6 - standards mode
if (document.documentElement && document.documentElement.scrollTop)
return document.documentElement.scrollTop;
// Internet Explorer 6, 7 and 8
if (document.body.scrollTop) return document.body.scrollTop;
return 0;
}
function elmYPosition(eID) {
var elm = document.getElementById(eID);
var y = elm.offsetTop;
var node = elm;
while (node.offsetParent && node.offsetParent != document.body) {
node = node.offsetParent;
y += node.offsetTop;
} return y;
}
function smoothScroll(eID) {
var startY = currentYPosition();
var stopY = elmYPosition(eID);
var distance = stopY > startY ? stopY - startY : startY - stopY;
if (distance < 100) {
scrollTo(0, stopY); return;
}
var speed = Math.round(distance / 100);
if (speed >= 20) speed = 20;
var step = Math.round(distance / 25);
var leapY = stopY > startY ? startY + step : startY - step;
var timer = 0;
if (stopY > startY) {
for ( var i=startY; i<stopY; i+=step ) {
setTimeout("window.scrollTo(0, "+leapY+")", timer * speed);
leapY += step; if (leapY > stopY) leapY = stopY; timer++;
} return;
}
for ( var i=startY; i>stopY; i-=step ) {
setTimeout("window.scrollTo(0, "+leapY+")", timer * speed);
leapY -= step; if (leapY < stopY) leapY = stopY; timer++;
}
}
Related links:
- https://www.sitepoint.com/smooth-scrolling-vanilla-javascript/
- https://github.com/zengabor/zenscroll/blob/dist/zenscroll.js
- https://github.com/cferdinandi/smooth-scroll/blob/master/src/js/smooth-scroll.js
- https://github.com/alicelieutier/smoothScroll/blob/master/smoothscroll.js
Smooth scroll anchor links WITHOUT jQuery
Using the function from here: JavaScript animation and modifying it to modify a property (not only a style's property), you can try something like this:
DEMO: http://jsfiddle.net/7TAa2/1/
Just saying...
function animate(elem, style, unit, from, to, time, prop) {
if (!elem) {
return;
}
var start = new Date().getTime(),
timer = setInterval(function() {
var step = Math.min(1, (new Date().getTime() - start) / time);
if (prop) {
elem[style] = (from + step * (to - from)) + unit;
} else {
elem.style[style] = (from + step * (to - from)) + unit;
}
if (step === 1) {
clearInterval(timer);
}
}, 25);
if (prop) {
elem[style] = from + unit;
} else {
elem.style[style] = from + unit;
}
}
window.onload = function() {
var target = document.getElementById("div5");
animate(document.scrollingElement || document.documentElement, "scrollTop", "", 0, target.offsetTop, 2000, true);
};
div {
height: 50px;
}
<div id="div1">asdf1</div>
<div id="div2">asdf2</div>
<div id="div3">asdf3</div>
<div id="div4">asdf4</div>
<div id="div5">asdf5</div>
<div id="div6">asdf6</div>
<div id="div7">asdf7</div>
<div id="div8">asdf8</div>
<div id="div9">asdf9</div>
<div id="div10">asdf10</div>
<div id="div10">asdf11</div>
<div id="div10">asdf12</div>
<div id="div10">asdf13</div>
<div id="div10">asdf14</div>
<div id="div10">asdf15</div>
<div id="div10">asdf16</div>
<div id="div10">asdf17</div>
<div id="div10">asdf18</div>
<div id="div10">asdf19</div>
<div id="div10">asdf20</div>
How to control scroll time without jquery when smooth scrolling to element?
use this function :
Smooth scroll anchor links WITHOUT jQuery
Scroll time is used as parameter.
Smoothscroll without anchors
var pixelsToMove = 50;
$('.myButton').click(function () {
var currentScrollTopValue = $('#container').scrollTop();
$('#container').scrollTop(currentScrollTopValue + pixelsToMove);
});
https://api.jquery.com/scrollTop/
hi i want to make this as smooth scroll can anyone help me up
This might be what you are looking for
html {
scroll-behavior: smooth;
}
Do a simple google search before asking a question here...
Why is my smooth scrolling function not working?
I've found the solution to the issue, adding this allows it to work:
html {
scroll-behavior: smooth;
}
JQuery smooth scroll - can this be simplified?
If you are using JQuery only for this feature, use scrollIntoView from JavaScript.
With JQuery, this function is enough:
function elevator(where) {
var the_id = "#" + where;
$('html, body').animate({
scrollTop:$(the_id).offset().top
}, 'slow');
return false;
}
which you can use in html tags like this:
<h1 id="title">Title at the top of the page</h1>
<!-- a lot of space -->
<button onclick="elevator('title')">go up</button>
NOTE: The CSS property is not useful with this function.
Now if you want an explanation of your code:
$(document).ready(function(){
// Add smooth scrolling to all links
// i.e. select all links on your page and add en event for each of them
// event called when there is a click
$("a").on('click', function(event) {
// Make sure this.hash has a value before overriding default behavior
// the hash is the anchor. For example: "website.com/page.html#hash"
// we must check if there is an hash in the link
if (this.hash !== "") {
// Prevent default anchor click behavior
// i.e. the default behavior of a link
event.preventDefault();
// Store hash
var hash = this.hash;
// Using jQuery's animate() method to add smooth page scroll
// The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){
// Add hash (#) to URL when done scrolling (default click behavior)
window.location.hash = hash;
});
} // End if
});
});
Your code is creating the smooth effect for every link automatically.
Related Topics
Execute Function After Complete Page Load
Coordinates of Selected Text in Browser Page
Conditionally Load JavaScript File
Add Content to a New Open Window
How to Get Element in User-Agent Shadow Root With JavaScript
How to Generate a Simple Popup Using Jquery
What Do Curly Braces in JavaScript Mean
Why Pass Parameters to CSS and JavaScript Link Files Like Src="../Cnt.Jsver=4.0"
Using Selenium to Imitate Dragging a File Onto an Upload Element
Difference Between "Let" and "Var"
How to Fix Blurry Text in My Html5 Canvas
Benefits of Loading Js At the Bottom as Opposed to the Top of the Document
Does Schema.Org Markup Work If Markup Is Dynamically Built With JavaScript
How to Get Nth-Child Selector to Skip Hidden Divs
Restart Animation in Css3: Any Better Way Than Removing the Element