简体   繁体   English

让toggleClass淡入但不淡出

[英]Have toggleClass fade in but not fade out

I am currently using the toggleClass() method with jQuery and I'd like to have the class fade in, but I don't want it to fade out. 我目前在jQuery中使用toggleClass()方法,但我希望该类淡入,但我不希望它淡出。 I've been using the "duration" attribute, but given that it is toggleClass, the duration is the same both ways. 我一直在使用“ duration”属性,但是鉴于它是toggleClass,所以两种方式的持续时间都相同。 I don't want to use addClass() with a fade in and removeClass() without a fade out because I feel like the code will get too lengthy and unruly. 我不想用addClass()与和淡入removeClass()没有淡出,因为我觉得自己的代码会得到过于冗长和不羁。 I want small, simple, readable code. 我想要小的,简单的,可读的代码。

Any ideas? 有任何想法吗?

I have this so far: 到目前为止,我有:

$("#e" ).hover(function() {
    $(this).closest("#word").toggleClass("hoverE", 500 )
});

I would like something like this where I can specify fade in duration and fade out duration: 我想要这样的东西,我可以指定淡入时间和淡出时间:

$("#e" ).hover(function() {
    $(this).closest("#word").toggleClass("hoverE", 500, 0 )
});

I've tried something like this, but it doesn't work: 我已经尝试过类似的方法,但是它不起作用:

$("#e" ).hover(function() {
    $(this).closest("#word").toggleClass("hoverE").fadeIn(500)
});

HTML: HTML:

  <div id="word">
    <h1><a id="h" class= "letter" href=#>H</a></h1>
    <h1><a id="e" class= "letter" href=#>E</a></h1>
    <h1><a id="l" class= "letter" href=#>L</a></h1>
    <h1><a id="l2"class= "letter" href=#>L</a></h1>
    <h1><a id="o" class= "letter" href=#>O</a></h1>
  </div>

This doesn't exist. 这个不存在。 You could create your own: 您可以创建自己的:

$.fn.myToggleClass = function(className, showDur, hideDur) {
    if(this.hasClass(className)){
        this.removeClass(className, hideDur);   
    } else {
        this.addClass(className, showDur);
    }
};

http://jsfiddle.net/6QqYQ/ http://jsfiddle.net/6QqYQ/

This is completely an 'out of the box' suggestion, but I think it fulfills the requirement: 这完全是“开箱即用”的建议,但我认为它可以满足要求:

$('#e').toggleClass( "hoverE", ( $('#e').hasClass("hoverE") ? 0 : 500) );

( or ) it would be a better idea to put it in a function: )最好将其放在函数中:

function customToggleClass($el, classname, dur1, dur2){

    dur = $el.hasClass(classname) ? dur2 : dur1;
    $el.toggleClass(classname, dur);

    return $el;

}

call it like: 像这样称呼它:

customToggleClass( $("#e"), "hoverE", 500, 0 );

( OR again ) even better as a jQuery plugin: 或再次使用 )作为jQuery插件更好:

$.fn.toggleClassFade = function(classname, dur1, dur2) {
    $.each(this, function(i, el){
        var $el = $(el);
        var dur = $el.hasClass(classname) ? dur2 : dur1;
        $el.toggleClass(classname, dur);
    });

    return this;
};

used like: 像这样使用:

$('#e').toggleClassFade('hoverE', 500, 0)

(it's also chainable) (也可链接)

hope this helps. 希望这可以帮助。

We need a little more information about your html structure to be able to answer this question. 我们需要有关您的html结构的更多信息,才能回答此问题。 Could you fork this jsfiddle and show us how you have your html elements set up? 您能分叉这个jsfiddle并向我们​​展示如何设置html元素吗?

Like I mentioned in my comment, using closest isn't actually possible here. 就像我在评论中提到的那样,这里实际上不可能使用closest值。 My guess is that you were looking for the next or prev functions rather than closest . 我的猜测是,你正在寻找nextprev函数,而不是closest If this is the case, the fadeIn call wouldn't work if you were not in fact selecting an element. 在这种情况下,如果您实际上未选择元素,则fadeIn调用将不起作用。 Here's a way that you can verify that you are in fact finding an element correctly using closest . 您可以通过以下方式验证您实际上是否使用closest正确找到了元素。

$("#e" ).hover(function() {
    console.log($(this).closest("#word")[0])
});

If you run this code and the result is an empty array, the selector is what went wrong. 如果运行此代码,结果是一个空数组,则选择器出了问题。 If you are getting back the element you are after, you are in quite a confusing situation (refer to my comment on the question). 如果您要找回要处理的要素,那么您的处境就很混乱(请参阅我对这个问题的评论)。

EDIT: After getting a clearer understanding if the effect you were after, this is the code that ended up solving the problem: 编辑:在更清楚地了解您所追求的效果之后,这是最终解决问题的代码:

http://jsfiddle.net/5MScN/5/ http://jsfiddle.net/5MScN/5/

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

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