I have an image that I have given the id #scroller to. What I would like to do is move the scroller horizontally according to the x-coordinate of the mouse but only when it's held down. I'm trying to move scroller against a hroizontal line called #bar. The amount the scroller can move depends on the width of the bar. Here is my code so far.
$(document).ready(function() {
var barWidth = $("#bar").width();
var mouseDown = false;
$(document).mousedown(function() {
mouseDown = true;
});
$(document).mouseup(function() {
mouseDown = false;
});
$(document).on("mousemove", function(e) {
if (mouseDown) {
if (e.pageX >= barWidth) {
$("#debug").html(JSON.stringify(e.pageX, undefined, 4));
$("#scroller").css("left", barWidth);
} else {
$("#debug").html(JSON.stringify(e.pageX, undefined, 4));
$("#scroller").css("left", e.pageX);
}
}
});
});
The #debug just prints something out on to the screen. It helps in debugging. It's not working as expected. I want the scroller to move when the mouse is held down and moving.