简体   繁体   English

JQuery 淡入 Hover 效果 - 帮助?

[英]JQuery Fade On Hover Effect - help?

I'm trying to achieve a fade-on-hover effect with JQuery.我正在尝试使用 JQuery 实现悬停时淡入淡出效果。 Currently I have an element with a "hov" class attacked to it, without javascript the css will simply change it's color on:hover.目前我有一个带有“hov”class 攻击的元素,没有 javascript css 将简单地改变它的颜色:ZE013542F5795381。 With JQuery.与 JQuery。

The idea is to clone the element as it's rolled over and place it directly infront, stripping it of the "hov" class so it's just static.想法是在元素滚动时克隆元素并将其直接放在前面,将其从“hov”class 中剥离,因此它只是 static。 Then I fade it out so it create the transition effect.然后我将其淡出以创建过渡效果。

I'm having trouble though, after I strip the "hov" class from the clone, it KEEPS acting as though its still there.但是我遇到了麻烦,在我从克隆中删除“hov” class 之后,它仍然保持原样。 I can mouse over the clone even though it shouldn't be able to be targeted through hov.我可以将鼠标悬停在克隆上,即使它不应该通过 hov 成为目标。 Any ideas / tips?有什么想法/提示吗?

<a href="#" class="hov rounded-50 action-button">Fade Me Out< /a>

$(".hov").mouseover(function() {

    // Clone the current element, remove the "hov" class so it won't trigger same behavior
    // finally layer it infront of current element

    var $overlay = $(this).clone(true).removeClass("hov").insertAfter($(this));

    // Push it to the side just for testing purposes - fade it out

    $overlay.css({left:'300px'}).fadeOut({duration:500, ease:'easeOutQuad'});
});

No need to clone the element, just fade the original element:无需克隆元素,只需淡化原始元素即可:

$('.hov').mouseenter(function() {
  $(this).fadeOut();
});

// Optionally:
$('.hov').mouseleave(function() {
  $(this).stop(true, true).show();
});

You can also use the hover function:您也可以使用 hover function:

$('.hov').hover(function(){
  $(this).fadeOut();
},
function(){
  $(this).stop(true, true).show();
});

If you just want it to partially fade, you can animate the opacity property:如果您只想让它部分淡化,您可以为 opacity 属性设置动画:

$('.hov').mouseenter(function() {
  $(this).animate({'opacity': 0.5});
});

If you just want it to pulse, then return to normal opacity:如果您只想让它脉动,则恢复正常的不透明度:

$('.hov').mouseenter(function() {
  $this = $(this);

  $this.animate({'opacity': 0.5}, {
    'complete': function(){
      $this.animate({'opacity': 1});
    }
  });
});

Finally, if your willing to forgo support of older browsers, you can do it all with css:最后,如果您愿意放弃对旧浏览器的支持,您可以使用 css 完成所有操作:

.hov {
  -webkit-transition: opacity 0.3s ease-in;
  -moz-transition: opacity 0.3s ease-in;   
}
.hov:hover {
  opacity: 0.5;
}

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

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