Solved (see below)
$(function () {
var textHi = $('#text').height();
var moveIt = textHi / 5;
var margH = parseInt(("#text").css("marginTop"));
margH = Math.abs(margH);
$("#down").mousehold(function(){
if (margH === textHi) {
return false;
} else {
$("#text").animate({marginTop: '-='moveIt}, 500);
}
});
$("#up").mousehold(function(){
if (margH === 0){
return false;
} else {
$("#text").animate({marginTop: '+='moveIt}, 500);
}
});
});
I have two buttons either side of a DIV. It works when I use regular numbers to increment the div up or down and stop/start it based on a simple incrementing variable.
Although, because of the different sizes the DIV can be, varying amounts of content, I wanted to pick up the current div height (textHi), then divide it by an amount, ie 5 (moveIt), and then use this variable to move the div up and down on the event. I then wanted to get the current margin-top value and use that as an indicator of where the div is, ie, when the margin top is '0' the up button doesn't work, as this will push it down too far. Or once the margin top has become equal (negatively, but I convert this to positive as there is no actual 'positive' margin top) it stops going down further as it will make the div scrolling forever.
For some reason, the variables are working and the increment is not happening.
Any ideas would be great, thanks.
With the direction of answers, solved it like this:
$(function () {
var textHi = $('#text').height();
var moveIt = textHi / 8;
var timing = moveIt * 5;
var intVal = 0;
$("#down").mousehold(function(){
if (intVal === 7) {
return false;
} else {
$("#text").animate({marginTop: '-=' + moveIt}, timing);
intVal = intVal + 1;
}
});
$("#up").mousehold(function(){
if (intVal == 0){
return false;
} else {
$("#text").animate({marginTop: '+=' + moveIt}, timing);
intVal = intVal - 1;
}
});
});
As you can see, I used a incrementing variable to initiate the animation. As the div scrolls down it reaches 7/8th of its total height and stops. If the increment is ever 0 it will not scroll up.