I’m developing a script to scroll pages like in smartphones (touching the screen). I have done in jQuery and jscrollto script, but I want to do it in pure Javascript. Here is my code:
// ==UserScript==
// @name touchNavigation
// @namespace here
// @include http://*
// @version 0.1
// @grant none
// ==/UserScript==
document.body.addEventListener("mousedown", mouseDown);
document.body.addEventListener("mouseup", mouseUp)
var y0=0;
var y1=0;
var diferencia=0;
function mouseDown(event)
{
y0=event.clientY;
}
function mouseUp(event)
{
var i;
y1=event.clientY;
diferencia=y1-y0;
if(diferencia>0){
i=-10;
while(diferencia>0){
setTimeout(desplazamiento, 250, i);
diferencia+=i;
}
}else{
i=10;
while(diferencia<0){
setTimeout(desplazamiento, 250, i);
diferencia+=i;
}
}
}
function desplazamiento(diferencia){
window.scrollBy(0,diferencia);
}
The script works. But the displacement is in one step. It should be more softly.
What could be the trouble of this section?
if(diferencia>0){
i=-10;
while(diferencia>0){
setTimeout(desplazamiento, 250, i);
diferencia+=i;
}
}else{
i=10;
while(diferencia<0){
setTimeout(desplazamiento, 250, i);
diferencia+=i;
}
}
ivan from userscript submited the code to make the scroll in javascript softly. You can see here.
http://userscripts.org/scripts/show/155832
I don’t know how work but work perfectly.