views:

62

answers:

2

I'm trying to do something like this:

var mouseY = //mouse y-position relative tot some element

while (mouseY > 0 && mouseY < 200){
    //scroll element -5px
}

I want to scroll a certain element up as long as the mouse is within a certain area of an element.

Getting the mouse position and scrolling is not the problem (using scrollTo plugin),

I just can't figure out how to bind this to an event and keep it repeating whil the mouse is in the required area...

Hope someone can point me in the right direction...

+1  A: 

After doing the scrolling, set a timer to re-check the mouse state again in, say, 50 milliseconds.

Thomas
`setTimeout(your_callback_here, 50);`
Matt Ball
+1  A: 

Thanx for the help so far...

I'm using the scrollTo jquery plugin. It has it's own callback function that i use instead of setTimeout

I managed to make the element scroll when the mouse is in the required area but i can't make the animation stop when the mouse leaves the area (i tried stop(), but no success...

This is what i have so far:

$(document).ready(function() {   
    var subNavL2OffsetTop = $('.subnav.level-2').offset().top;
    var subNavL2Height = $('.subnav.level-2').height();
    var wrapper = $('.subnav.level-2 div.inner');


    $('.subnav.level-2').mousemove(function(e){

        var mouseY = e.pageY-subNavL2OffsetTop;

        if( mouseY > 0 && mouseY < 200){
            //scroll up
        }

        if(mouseY > (subNavL2Height - 200) && mouseY < subNavL2Height){             
            scrollDown(mouseY);         
        }
    });


    function scrollDown(mouseY){        
        wrapper.scrollTo('+=5px',{
            axis:'y',
            onAfter: function(){scrollDown(mouseY)},
            duration: 20             
        });         
    }
});

There's probably better ways to do what i try to achieve...

Joris
Hi Joris. Just so you know, it is better to edit your question rather than provide more information in an answer. Sometimes new folks here don't know they can do that.
patrick dw
Why is the `onAfter:` callback calling `scrollDown` again? This would seem to create an infinite loop.
patrick dw