【问题标题】:Fade in a .addClass淡入 .addClass
【发布时间】:2013-08-12 16:09:26
【问题描述】:

我看到以前有人问过这个问题,但我使用的功能与示例不同,所以我无法将它们放在一起。

我有一个“悬停”类,我正在将其添加到带有 .steps 类的 div 中,该类会更改背景图像。但是我希望它淡入淡出(就像我在另一个 div 上使用 .steps-hover 类所做的那样。)

这是我所拥有的:

<script type='text/javascript'>
$(document).ready(function() {
  $('.steps').hover(over, out);
});

function over(event) {
  $('.steps').addClass("hover");
  $('.steps-hover', this).stop(true, true, true).fadeIn(500);
  $('.steps-hover', this).css("display", "normal");
}

function out(event) {
  $('.steps').removeClass("hover");
  $('.steps-hover', this).stop(true, true, true).fadeOut(500);
}
</script>

那么如何让 .addClass 和 .removeClass 淡入淡出呢?我觉得这很简单,但是我尝试过的事情没有奏效,而且我是一个 jQuery 初学者。感谢您的帮助!

【问题讨论】:

    标签: jquery


    【解决方案1】:

    应该是

    $(document).ready(function() {
      $('.steps').hover(over, out);
    });
    
    function over(event) {
      $(this).addClass("hover"); // the hover class has to be added to the specific `.steps` element which was hovered, not to all `.steps` elements
      $('.steps-hover', this).stop(true, true, true).fadeIn(500).css("display", "normal");
    }
    
    function out(event) {
      $(this).removeClass("hover"); //same as above
      $('.steps-hover', this).stop(true, true, true).fadeOut(500);
    }
    

    【讨论】:

    • 谢谢!我没有意识到这将是一个问题!现在有办法让课程淡入淡出吗?
    • @user2574250 滑入/滑出?
    • 只是淡出,就像 .steps-hover 正在做的那样。我需要它们同步。
    • 或者我的方法不对?我有 3 个按钮,我想更改悬停时的背景图像。每个图像都是不同的。我认为添加一个类是保持它们独一无二的方法。
    猜你喜欢
    • 2011-02-12
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多