首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有jQuery的CSS停车灯

带有jQuery的CSS停车灯
EN

Stack Overflow用户
提问于 2012-06-25 03:41:34
回答 2查看 1.3K关注 0票数 0

我正在尝试创建一个停止灯动画(旋转闪光灯以红色>黄色>绿色开始),当我将鼠标悬停在任何一个灯上时,它会暂停动画并照亮所选的灯并显示悬停div。

到目前为止,一切都在工作(尽管我确信代码可以使用一些优化),但是当我将鼠标悬停在任何一个灯光上时,动画并没有立即停止。我试着使用clearTimeout & clearInternal,但是什么都不起作用。

jsfiddle:http://jsfiddle.net/JZ86B/

这是我的jQuery

代码语言:javascript
复制
stopLight();

$('[class^="stopLight"]').mouseenter(function(){
     clearTimeout(window.flashLightTimer);
     clearInterval(window.stopLightInterval);
     $(this).css('background-position-x','-106px');
     $(this).find('span').fadeIn('fast');
}).mouseleave(function() {
     $(this).css('background-position-x','0px');         
     $(this).find('span').hide('fast');
     stopLight();
}); 

function stopLight() {
    window.stopLightInterval = setInterval(function() {
        flashLight('red',1000);     
        flashLight('yellow',2000);
        flashLight('green',3000);
    }, 3000);
};

function flashLight (a, b) {
    window.flashLightTimer = setTimeout(function(){
        $(".stopLight-"+a).animate({
            'background-position-x': '-106px'
        }, 0, 'linear')
        .delay(1000)
        .animate({
            'background-position-x': '0px'
        }, 0, 'linear')
    }, b);
};
EN

回答 2

Stack Overflow用户

发布于 2012-06-25 09:49:10

您是否已尝试在鼠标输入事件被激发时立即重置所有灯光的背景位置?类似于:

代码语言:javascript
复制
$(".light div").css('background-position-x','0px');

你需要做更多的调整,但这至少会立即“取消”所有其他的灯光。

票数 0
EN

Stack Overflow用户

发布于 2012-06-25 14:11:12

这是一个很大的帮助:

clearTimeout on Mouse Exit

这是我的最终代码

代码语言:javascript
复制
var timerID = [];

$('[class^="stopLight"]').mouseenter(function() {
    $(this).css('background-position-x', '-106px');
    $(this).find('span').fadeIn('fast');
    for (var i = 0; i < timerID.length; i++) {
        clearTimeout(timerID[i]);
    };
    clearInterval(window.stopLightInterval);    
}).mouseleave(function() {
    $(this).css('background-position-x', '0px');
    $(this).find('span').hide('fast');
    stopLight();
});

function stopLight() {
    window.stopLightInterval = setInterval(function() {
        flashLight('red', 1000);
        flashLight('yellow', 2000);
        flashLight('green', 3000);
    }, 3000);
};

function flashLight(a, b) {
    timer = setTimeout(function() {
        $(".stopLight-" + a).animate({
            'background-position-x': '-106px'
        }, 0, 'linear').delay(1000).animate({
            'background-position-x': '0px'
        }, 0, 'linear')
    }, b);
    timerID.push(timer);
};

stopLight();?
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11180639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档