i tried to make a custom scrolling script that goes from one block to another in my website but when i click on my next button, the scrolling always jump to the top. I don't know why it always do that.
Here is my javascript code :
function myscroll(i)
{
if (document.body.scrollTop + window.innerHeight > document.body.scrollHeight - 50)
{
document.body.scrollTop += 1;
if (document.body.scrollTop + window.innerHeight < document.body.scrollHeight)
setTimeout(function() { myscroll(i) }, 10);
}
else if (document.body.scrollTop < i - 100)
{
document.body.scrollTop += 10;
if (document.body.scrollTop + window.innerHeight < document.body.scrollHeight)
setTimeout(function() { myscroll(i) }, 10);
}
else if (document.body.scrollTop < i - 50)
{
document.body.scrollTop += 5;
if (document.body.scrollTop + window.innerHeight < document.body.scrollHeight)
setTimeout(function() { myscroll(i) }, 10);
}
else if (document.body.scrollTop < i)
{
document.body.scrollTop += 1;
if (document.body.scrollTop + window.innerHeight < document.body.scrollHeight)
setTimeout(function() { myscroll(i) }, 10);
}
}
here i made an exemple on jsbin : http://jsbin.com/ehIZAya/
Thanks for reading.
Problem: Page jumping to top
The page jumps to the top because
href="#"
causes your browser to look for an element with ID of what comes after the#
. For example:href="#20898954"
(since20898954
is the ID of this answer); will cause your browser to jump to the top of this answerhref="#"
; it will cause your browser to jump to the top of the pageTo fix this, you have two choices:
href
attribute. Unfortunately, this method removes all styling (and it looks like ordinary text), which can be confusing. Instead, I prefer to...onclick
function withpreventDefault()
.Problem:
document.body.scrollTop
always equals 0. In your code, the firstelse if
executes as an infinite loop, since it is the first condition that evaluates to true. The problem is that you add pixels todocument.body.scrollTop
, which remains 0, and thus, the second condition always, in effect, calls itself to run again.Replace
document.body.scrollTop
withdocument.documentElement.scrollTop
for accurate readings of the current top of the viewable window, and to set it correctly.To prevent the automatic jump to the top of the page, you can use
preventDefault()
. Assume each anchor has classlink
:The problem there is
someNumber
. You could enter an integer value (i.e.500
), and each link would scroll to the same place on the page (500px). You could enter an integer * i (i.e.500 * i
), and each link would scroll to a multiple of 500px (0px, 500px, 1000px, etc.). But I chose to make this a little more customizable than that, with a custom attribute calledscroll-target
.Each anchor is defined
<a class="link" href="#" scroll-target="500">
, where 500 might be any value. To get a custom attribute, useelement.getAttribute("attributeName")
. And so, we're left with:To apply this to your code, make sure each anchor is defined
<a class="link" href="#" scroll-target="500">
, replacing 500 with the actual value you want. You can also use a class name other than "link", but if you do, make sure you replace the class name in the second line of my script also.On page load, it alerts a few values. Scroll to random places, and refresh the page. Take note which values change, and which remain the same. Do this a few times until you understand.
Javascript:
You might also want to add equal and opposite conditions for the top of the currently-viewable area being past/below the target, if you ever want to scroll upwards.