I am attempting to create a jQuery thumbnail scroller with buttons that respond to mousedown and mouseup events. I have successfully implemented the scrolling functionality, but I am facing issues when trying to refactor it into a reusable function. Below is the code for my scroller:
var $wrapper = $('.my_ul');
var $div = $('.my_div');
var $ul = $('.my_ul');
function scrollRight() {
var divLeft = $ul.css('marginLeft');
divLeft = Math.abs(parseInt(divLeft)) - 60;
var width = $div.width();
var ulwid = $ul.width();
var ulWidth = $ul.width() - width;
if (divLeft >= ulWidth) {
stopScrolling();
} else {
$wrapper.animate({'margin-left': '-=10'}, 1, scrollRight);
}
}
function scrollLeft() {
var divLeft = $ul.css('marginLeft');
divLeft = parseInt(divLeft);
if (divLeft >= 0) {
stopScrolling();
} else {
$wrapper.animate({'margin-left': '+=10'}, 1, scrollLeft);
}
}
function stopScrolling() {
$wrapper.stop();
}
$('.scroll_right').mousedown(scrollRight).mouseup(stopScrolling);
$('.scroll_left').mousedown(scrollLeft).mouseup(stopScrolling);
I have tried creating a parameterized function without success, and I have noticed that the animation slows down due to recursive calls. If anyone has alternative solutions or suggestions, please share them with me.