user1949366
user1949366

Reputation: 465

Scroll on click

I am trying to execute javascript while scrolling in ios. The only way I can think of is like this. However I am trying to get it to stop and start scrolling on click of a div, but will not work when I place it in a onClick function.

$('#pause').click(function(){
    function doScroll(){
       $('body').scrollTop($('body').scrollTop() + 20);
    }

    setInterval(doScroll, 50);
});

Upvotes: 0

Views: 404

Answers (2)

Mesh
Mesh

Reputation: 6422

This toggles the scroll on and off. You may want to add some code to check when the end of the scroll is reached.

http://jsfiddle.net/YNmZh/7/

document.getElementById( 'pause').addEventListener( "click" , function(){
    if( window.scrollTimerId ){
        window.clearInterval(window.scrollTimerId );
        window.scrollTimerId = null
    }
    else{
        doScroll();
         window.scrollTimerId= window.setInterval( doScroll , 50);
    }

});



 function doScroll(){
     window.scrollBy(0,5);        
}

Upvotes: 0

putvande
putvande

Reputation: 15213

Why not just:

var t;
var scrolling = false;

function doScroll() {
    $('body').scrollTop($('body').scrollTop() + 20);
}

$('#pause').on('click',function(){
    scrolling = !scrolling;
    if(!scrolling){ 
        clearInterval(t);
        return;
    }
    t = setInterval(doScroll, 50);
});

Upvotes: 1

Related Questions