简体   繁体   English

淡入淡出并在鼠标悬停时停止

[英]fade in fade out and stop on mouse hover

I m working on small animation where the link will have fadein fadeout effect with some delay but when when user moveover his mouse on link it should stop and start animate once again once mouse out. 我正在制作小型动画,其中的链接将具有一定程度的淡入淡出效果,但是当用户在链接上移动鼠标时,它应该停止并再次动画,一旦鼠标移出。

Currently When user moveover mouse on link 2 or more link start appering and its stop at last. 当前,当用户将鼠标移到链接2或更多链接上时,鼠标便开始显示并最终停止。

https://jsfiddle.net/e1fye4uy/3/ https://jsfiddle.net/e1fye4uy/3/

function InOut(elem) {
elem.delay()
    .fadeIn(1000)
    .delay(10000)
    .fadeOut(1000,

function () {
    if (elem.next().length > 0) {
        InOut(elem.next());
    } else {
        InOut(elem.siblings(':first'));
    }

}).mouseover(function () {
    //$(this).stop(true, false);
   // $(this).clearQueue();
    elem.stop($(".newsFlash").children('li'), true, false);
}).mouseout(function () {
    if (elem.next().length > 0) {
        elem.clearQueue();
        // elem.finish();
        InOut($(this));
    }
});};$(function () {
$('#content li').hide();
InOut($('#content li:first'));

}); });

This is my solution https://jsfiddle.net/e1fye4uy/8/ 这是我的解决方案https://jsfiddle.net/e1fye4uy/8/

var doAniamtion = false;
var lis = $("#content li");
var time;
function start(){
    time = setInterval(function(){ 
       next(); 
    }, 2000);
    console.log("start" + time);
}

function stop(){
    console.log("stop " + time)
    clearInterval(time);
}



function next(){
    var lisLength = $(lis).length;
    for(var i=0;i<lisLength;i++)
        if($(lis[i]).hasClass("current"))
        {
            fadOut(lis[i],function(){
                fadIn(((i+1) < lisLength) ? lis[i+1] : lis[0]);
            });
            return;
        }
}

function fadIn(obj, callback){
    $(obj).addClass("current");
    $(obj).animate({
        opacity:1
    },500,function(){

        callback && callback();
    });
}

function fadOut(obj, callback){
    $(obj).addClass("current");
    $(obj).animate({
        opacity:0
    },500,function(){
         $(obj).removeClass("current");
        callback && callback();
    });
}

start();

$("#content").mouseover(function(){
    stop();
});

$("#content").mouseout(function(){
    start();
});

So, I decide to use animation instead of use fadeIn and fadeOut, setInterval (+info http://www.w3schools.com/jsref/met_win_setinterval.asp ) and class "current" in the li HTML element to know witch li is current visible. 因此,我决定使用动画而不是使用fadeIn和fadeOut,setInterval(+ info http://www.w3schools.com/jsref/met_win_setinterval.asp )和li HTML元素中的“ current”类来了解li是最新的可见。

PS: PS:

For change the time that li remains visible change the setInterval Time. 要更改li保持可见的时间,请更改setInterval Time。 For make the animations more fast or slow change the time in animation inside of fadIn and fadout functions 为了使动画更快或更慢,可以在fadIn和fadout函数中更改动画的时间

Any doubt about the code, ask! 对代码有任何疑问,请询问!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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