violin demonstration
this is how i will do it ... I would calculate the window width and element width and use them ...
if ($('#left1').is(":hover")) {
var move = left1+20;
if(move > $('#track1').parent().width())
move = $('#track1 .book').length * ($('#track1 .book').width() + 5) * -1;
$('#track1').css('left', move);
}
else if ($('#right1').is(":hover")) {
var move = left1-20;
if(move < $('#track1 .book').length * ($('#track1 .book').width() + 5) * -1 )
move = $('#track1').parent().width();
$('#track1').css('left', move);
}
you can change others in a similar way, also using setIntervalfor moniter is a bad idea if you ask me.
Edit:
I would do it like this: demo
$('.left').hover(function(){
moveLeft($(this));
});
$('.right').hover(function(){
moveRight($(this));
});
function moveLeft(ele){
if(ele.is(":hover")){
var track = ele.next().next(),
move = parseInt(track.css('left'))+10,
books = track.find('.book'),
wid = (books.length * (books.width()+5)) * -1;
if(move > track.parent().width()) move = wid;
track.css('left', move);
setTimeout(moveLeft.bind(null, ele), 50);
}
}
function moveRight(ele){
if(ele.is(":hover")){
var track = ele.next(),
move = parseInt(track.css('left'))-10,
books = track.find('.book'),
wid = (books.length * (books.width()+5)) * -1;
if(move < wid ) move = track.parent().width();
track.css('left', move);
setTimeout(moveRight.bind(null, ele), 50);
}
}
source
share