【问题标题】:Loop through elements and add/remove class循环遍历元素并添加/删除类
【发布时间】:2019-04-26 16:52:18
【问题描述】:

我想循环遍历每个 h1 > a 元素,添加一个类,然后在一定延迟后删除一个类,但它不起作用。

我做错了什么?

$("h1 a").each(function() {
  $(this).addClass('glow').delay(2000).removeClass('glow');
});
a {
  color: #000;
  text-decoration: none;
  text-shadow: -1px 0px 1px rgba(150, 150, 150, 1);
}

h1 {
  text-align: center;
}

.glow {
  animation: glow .8s infinite alternate;
}

@keyframes glow {
  to {
    text-shadow: 0 0 6px #aaa;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>
  <a href="#">wellcome</a>,&nbsp;
  <a href="#">bonjoure</a>,&nbsp;
  <a href="#">bienvenido</a>,&nbsp;
  <a href="#">benvenuto</a>,<br/>
  <a href="#">добро пожаловать</a>,<br/>
  <a href="#">willkommen</a>
</h1>

【问题讨论】:

    标签: jquery element each


    【解决方案1】:

    好吧,忘记我说的话。这行得通 - 但不是 css 动画,你靠自己。

    var a = [];
    $("h1 a").each(function(index) {
      a[index] = $(this);
      setTimeout(function() {
        a[index].addClass('glow').delay(500).queue(function(next) {
          a[index].removeClass('glow');
          next();
        });
      }, index * 500);
    });
    a {
      color: #000;
      text-decoration: none;
      text-shadow: -1px 0px 1px rgba(150, 150, 150, 1);
    }
    
    h1 {
      text-align: center;
    }
    
    .glow {
      background-color: yellow;
    }
    
    @keyframes glow {
      to {
        text-shadow: 0 0 6px #aaa;
      }
    }
    <h1>
      <a href="#">wellcome</a>,&nbsp;
      <a href="#">bonjoure</a>,&nbsp;
      <a href="#">bienvenido</a>,&nbsp;
      <a href="#">benvenuto</a>,<br/>
      <a href="#">добро пожаловать</a>,<br/>
      <a href="#">willkommen</a>
    </h1>

    使用这些作为参考:

    How to make delay between each loops of jQuery.each function? Callback to .delay()

    【讨论】:

      【解决方案2】:

      如果你想给发光效果和类应该在一段时间后添加和删除,你可以像这样使用setInterval()

      $("h1").each(function(i, item) {
        setInterval(function() {
          $(item).addClass('glow');
      
        }, 2000 + i)
        setInterval(function() {
          $(item).removeClass('glow');
      
        }, 2000 + i)
      
      });
      a {
        color: #000;
        text-decoration: none;
        text-shadow: -1px 0px 1px rgba(150, 150, 150, 1);
      }
      
      h1 {
        text-align: center;
      }
      
      .glow {
        animation: glow .8s infinite alternate;
      }
      
      @keyframes glow {
        to {
          text-shadow: 0 0 6px #aaa;
        }
      }
      <h1>
        <a href="#">wellcome</a>,&nbsp;
        <a href="#">bonjoure</a>,&nbsp;
        <a href="#">bienvenido</a>,&nbsp;
        <a href="#">benvenuto</a>,<br/>
        <a href="#">добро пожаловать</a>,<br/>
        <a href="#">willkommen</a>
      </h1>

      【讨论】:

      • @EhsanSajjad:感谢您的支持。可以无限循环吗?
      • @Pascal setInterval 将在相同的时间间隔后工作,它会在每个时间间隔后调用函数
      猜你喜欢
      • 1970-01-01
      • 2013-07-22
      • 2020-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-30
      相关资源
      最近更新 更多