Siddharth Sharma
Siddharth Sharma

Reputation: 1711

jquery slider dont stop instantously with mousedown and mouseup

The link below is for copy of a jquery slider , i want to make the sliding smooth but when I reduce the time in setinterval (say 200)the slider dont respond to the mouseup event instantaneously and the slides stop after a second or two after mouseup is triggered . I also tried using stop on jquery animation but that was not helpful . Here is the link . http://jsfiddle.net/WwT54/ For now after every half second the slide shifts by 10px , i want to make it look smooth .

$("#popUpInnerArrowLeft").mousedown(function (event) {

    movetoleft();
});

var into, into2;

function movetoleft() {
    function moveit() {
        $(".thumbsInnerContainer").animate({
            left: '-=10px'
        });
    }

    into = setInterval(function () {
        moveit();
    }, 500);

}

$(document).mouseup(function (event) {
    clearInterval(into);

});



//for the right arrow

$("#popUpInnerArrowRight").mousedown(function (event) {
    movetoright();
});

function movetoright() {

    function moveit2() {
        $(".thumbsInnerContainer").animate({
            left: '+=10px'
        });
    }

    into2 = setInterval(function () {
        moveit2();
    }, 500);

}

$(document).mouseup(function (event) {
    clearInterval(into2);
});

Upvotes: 0

Views: 206

Answers (1)

maverickosama92
maverickosama92

Reputation: 2725

check this (is that what you want):

var into, into2, unit = '';

$("#popUpInnerArrowLeft").click(function (e) {  
    e.preventDefault();
    unit = false;
    movetoleft();
});

//for the right arrow
$("#popUpInnerArrowRight").click(function (e) {
    e.preventDefault();
    unit = true;
    movetoright();
});

function moveit() {    

    $(".thumbsInnerContainer").stop(true,true).animate({
        left: ((unit == true)? '+=':'-=') + '10px'
    },{easing:'linear'});
}

function movetoright() {        
    into = setInterval(function () {
        moveit();
    }, 300);
}

function movetoleft() {    
    into = setInterval(function () {
        moveit();
    }, 300);
}

$(document).mouseup(function (event) {
    clearInterval(into);    
});

working fiddle here: http://jsfiddle.net/WwT54/2/

Upvotes: 1

Related Questions