1

我有一个带有jquery.color的简单彩色动画。这是我的代码:

$(document).ready(function(){
    $('.fx_link').bind({
        mouseenter: function(e) {
            $(this).attr('originalColor', $(this).css('color'));
            $(this).animate({ color: "#999999" }, 'fast');
        },
        mouseleave: function(e) {
            $(this).animate({ color: $(this).attr('originalColor') }, 'fast');
            $(this).removeAttr('originalColor');
        }
    });
});

它非常好。但是,动画是针对菜单项的。如果鼠标在某个项目上,则动画开始。然后鼠标点击,页面刷新。鼠标在链接上,但它没有移动。只要鼠标移动一个像素,就会触发 mouseenter 事件(即使鼠标已经在链接上)并且有一个我认为是错误的动画。

我需要一些想法,例如:

$(this).bind({ mouseenter: function(e) {

    if( __ mouse is not already over $(this) __ ) 

        $(this).animate(...); } });

我已经尝试在 mouseenter、mouseover 上设置一些状态,但是......没办法

编辑:

完整的例子。将此另存为h.html

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://code.jquery.com/color/jquery.color-2.1.0.js"></script>
<script type="text/javascript">

$(document).ready(function(){

    $('.fx_link').bind({
        mouseenter: function(e) {
            console.log("enter");
            $(this).attr('originalColor', $(this).css('color'));
            $(this).animate({ color: "#999999" }, 'fast');
        },
        mouseleave: function(e) {
        
            console.log("leave");
            $(this).animate({ color: $(this).attr('originalColor') }, 'fast');
            $(this).removeAttr('originalColor');
        
        }
    });
});


</script>
<body>

<a class="fx_link" href="h.html">this is a link</a>

</body>
</html>
4

2 回答 2

2

对不起,我在手机上,所以代码可能是错误的(未经测试)。

编辑(现在测试)

// fix: bind mousemove to document, not links, sorry!
$(document).bind('mousemove', function(event) {
    $('.fx_link').bind('mouseenter', function(event) {
         //....
    }
    $(this).unbind(event);
});

已编辑

  • 处理 2 种不同鼠标的完整示例如果 [at page refresh] 鼠标已经在链接内(只需设置颜色),则输入一个,当来自外部(动画颜色)时输入一个。

  • 通过在开始时为所有链接设置 originalColors 来修复 originalColors 错误。

  • 使用命名函数而不是匿名函数,因此很容易解除绑定(并且看起来也更清晰)。

这是代码:

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="http://code.jquery.com/color/jquery.color-2.1.0.js"></script>
<script type="text/javascript">

$(document).ready(function(){

    var $links=$('.fx_link');

    // save ALL originalColors so they are fixed forever
    $links.each(function() {$(this).attr('originalColor', $(this).css('color'));});

    $(document).bind('mousemove', handleMove);
    $links.bind('mouseenter', firstMouseEnter);
    $links.bind('mouseleave', anyMouseLeave);

    function handleMove(event) { // When mouse moves in document
        console.log("first move - setting up things..");
        $(document).unbind('mousemove',handleMove); // remove myself (no need anymore)
        $links.bind('mouseenter', otherMouseEnter); // istall second mouseenter
        $links.unbind('mouseenter',firstMouseEnter); // remove first mouseenter
    }

    function firstMouseEnter(event) { // Called before mouse is moved in document
        console.log("first enter");
        $(this).css('color','#999');
    }

    function otherMouseEnter(event) { // Called after mouse is moved in document
        console.log("other enter");
        $(this).animate({ color: "#999" }, 'fast');
    }

    function anyMouseLeave(event) {
        console.log("leave");
        $(this).animate({ color: $(this).attr('originalColor') }, 'fast');
    }

});


</script>
<body>

<a class="fx_link" href="h.html">this is a link</a>

</body>
</html>
于 2012-12-16T23:02:26.020 回答
1

将带有“fx_link”类的菜单项悬停在其他颜色上时,这样做的目的是使其淡化为另一种颜色吗?在这种情况下,您很可能只使用带有 .fx_link:hover 的 css3 转换。然后,您可以根据需要自定义过渡。

a.fx_link{
color:red;
-webkit-transition-property: color;
  -webkit-transition-duration: 2.5s;
  -webkit-transition-timing-function: linear;
  -moz-transition-property: background color;
  -moz-transition-duration: 2.5s;
  -moz-transition-timing-function: linear;
  -o-transition-property: color;
  -o-transition-duration: 2.5s;
  -o-transition-timing-function: linear;
  -ms-transition-property: background color;
  -ms-transition-duration: 2.5s;
  -ms-transition-timing-function: linear;
  transition-property: color;
  transition-duration: 2.5s;
  transition-timing-function: linear;    
}

a.fx_link:hover{
    color:blue;
-webkit-transition-property: color;
  -webkit-transition-duration: 2.5s;
  -webkit-transition-timing-function: linear;
  -moz-transition-property: background color;
  -moz-transition-duration: 2.5s;
  -moz-transition-timing-function: linear;
  -o-transition-property: color;
  -o-transition-duration: 2.5s;
  -o-transition-timing-function: linear;
  -ms-transition-property: background color;
  -ms-transition-duration: 2.5s;
  -ms-transition-timing-function: linear;
  transition-property: color;
  transition-duration: 2.5s;
  transition-timing-function: linear;
}

​</p>

于 2012-12-16T22:49:49.577 回答