Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在jQuery中拦截不透明度转换?_Jquery_Hover_Opacity_Fade_Intercept - Fatal编程技术网

如何在jQuery中拦截不透明度转换?

如何在jQuery中拦截不透明度转换?,jquery,hover,opacity,fade,intercept,Jquery,Hover,Opacity,Fade,Intercept,我有以下悬停脚本,可用于导航链接图像: $("nav a").hover( function() { $(".current", this).animate({ opacity: 1 }, { duration: 300, specialEasing: { opacity: 'linear', }, }); }, func

我有以下悬停脚本,可用于导航链接图像:

$("nav a").hover(
    function() {
        $(".current", this).animate({
            opacity: 1
        }, {
            duration: 300,
            specialEasing: {
                opacity: 'linear',
            },
});
    }, function() {
        $(".current", this).animate({
            opacity: 0
        }, {
            duration: 2000,
            specialEasing: {
                opacity: 'linear',
            },

});
});
我想知道,如果用户在原始动画完成之前将光标移出hover div,然后再移回到hover div中,是否有办法拦截动画。如果用户将光标移回div,我希望动画立即将.current div中图像的不透明度衰减为1

我希望这是清楚的,并将很高兴有任何帮助

谢谢


尼克

您可以使用jQuery的
stop()
函数取消动画。将enter函数的第一行更改为:

    $(".current", this).stop().animate({

这将停止动画,并立即将不透明度返回到1。

我过去完成类似操作的方法是将悬停元素的引用存储在对象中,并通过与悬停元素相关联的某种ID设置关键帧

例如:

var hoveredElements = {previousId:null};
$("nav a").bind("mouseover mouseout",function(e) {
           if(e.type == "mouseover")
           {

              if(!hoveredElements.hasOwnProperty(this.id))
              {
                 hoveredElements[this.id] =  $(".current", this).animate({opacity:  1},300);
                 hoveredElements.previousId = this.id;
              }
              else if(hoveredElements.previousId == this.id)
              {
                if(hoveredElements.hasOwnProperty(this.id))
                {
                    hoveredElements[this.id].stop().css({opacity:1});
                } 
                else
                {
                  hoveredElements[this.id] = $(".current", this).css({opacity:1});
                }
              }
           }
           else
           {
             if(hoveredElements.hasOwnProperty(this.id))
             {
                 hoveredElements[this.id].animate({opacity:  0},2000,function(){
                    delete hoveredElements[this.id];
                 });
             }
           }
});
当然,ID不必是HTML ID,只需要一些唯一的标识符(可以是元素本身)

编辑:对不起,没有完全理解原来的问题