简体   繁体   中英

Stop timeOut and animation on mouseenter and continue on mouseleave

I wanted to get rid of a slider-plugin, so i tried to build my own,

everything works nice but i´m stuck to stop the whole thing on hover and restart it again on mouseleave,

here is my js :

function startPslider(val) {
    if (!val) {
        val = 0;
    }
    var holder = 'slideHolder';
    var text = 'slideText';
    startInterval(holder, text, val);
}

function startInterval(holder, text, val) {
    var t;
    var i = val;
    if (i > 2) {
        i = 0
    }

    $('#' + holder + i).animate({
        opacity: 1,
    }, function () {
        $(this).addClass('active')
        $('.' + text + i).animate({
            opacity: 1,
            left: 0
        }, 1200);
        t = setTimeout(function () {
            $('.' + text + i).animate({
                opacity: 0,
                left: '-400px'
            }, 1200);
            $('#' + holder + i).animate({
                opacity: 0,
            }, 2200).removeClass('active');
            startPslider(i + 1);
        }, 4000)
    });

    // Here´s the not working hover-function 
    $('#hpCanvas').hover(function () {
        clearTimeout(t);
    }, function () {
        var id = $('.active').attr('id');
        var slide = id.substring(11, 22);
        console.log(slide)
        startPslider(slide);
    });
}

$(function () {
    startPslider();
});

tryed to solve this with adding class 'active' to the current holder and at hover-out try to catch the current-slide number (val) and restart it again, starting on the correct slide, but it´s not working as I wish,

have a look at this fiddle, http://jsfiddle.net/zDh76/ you will find html and css there, as you see everything works fine as long you do not hover.

Maybe anyone has a helping hint how to stop the animation, clear the timer and go on with the correct slide on mouseleave?

UPDATE

i separated start and end-interval

function startPslider(i) {
if(!i){
    i=0;
}
if(i >2){
    i=0
    }
console.log('started Slider with slide:'+i)
var holder = 'slideHolder';
var text = 'slideText';
startInterval(holder, text, i);
}

function startInterval(holder,text,i) {
var t;
var v;
console.log('started Interval with slide:'+i);

$('#'+holder+i).animate({
        opacity:1,
    }, function(){
        $('.'+text+i).animate({
            opacity:1,
            left:0
        },1200);
        t= setTimeout(function(){endInterval(holder,text,i);    },4000);
});
 }

 function endInterval(holder,text,i,cont){
console.log('end Interval with slide:'+i);
$('.'+text+i).animate({
    opacity:0,
    left:'-400px'
},1200);
$('#'+holder+i).animate({
    opacity:0,
},2200, function(){
    $('.slideHolder').css('opacity',0);
             i = i+1;
    startPslider(i);        
});
 }

I found it out myself,

i needed to unbind the hover event on #hpCanvas inside the mouseleave function like

    $('#hpCanvas').hover(function(){
        $('.slideHolder, .slideText').stop(true,true);
        clearTimeout(t)
    },function(){
      endInterval(holder,text,i);
      $('#hpCanvas').unbind('mouseenter mouseleave') 
    })

as the next call to bind it with hover event is inside the mouseleave-part of the first hover event.

thanks anyway for everyone reading this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM