简体   繁体   English

找到第一个类,然后逐渐将类添加到具有相同名称的所有div

[英]Find first class then gradually add class to all divs with same name

I have a selection of dots which I'm making from a table like the below: 我有一个点,我正在从如下表格制作的点:

<div class="steps-wrapper">
    <table class="steps">
        <tbody>
            <tr>

                <td></td>
                <td></td>
                <td class="active"></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td class="active"></td>
            </tr>
            <tr>
                <td class="active"></td>
                <td class="active"></td>
                <td class="active"></td>
            </tr>
            <tr>
                <td class="active"></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td class="active"></td>
                <td></td>
                <td></td>
            </tr>
        </tbody>
    </table>
</div>

Here is a fiddle to view aswell. 这里还有一个小提琴。

I would like it so that when the td 's are visible they gradually load in. So I would find the first td with the class active and fade it in. It would then find the next td with the class active and fade that in 0.2s after the last one and so on. 我希望这样当td可见时它们会逐渐加载。所以我会找到第一个td ,并且该类处于活动状态并将其淡入。然后它会找到下一个td ,类激活并在0.2中淡出s在最后一个之后,依此类推。

Looking at the jsFiddle the first dot to load in would be top right and then it would gradually load in from the right, across and down to bottom left. 看看jsFiddle ,加载的第一个点将在右上角,然后它将从右侧,从上到下逐渐加载到左下角。 However the shape of the dots will change so sometimes it could load from top left down to bottom right. 但是,点的形状会发生变化,因此有时可能会从左上角向右下角加载。

This is my jQuery so far: 到目前为止这是我的jQuery:

$(document).ready(function () {
$(function($, win) {
  $.fn.inViewport = function(cb) {
     return this.each(function(i,el){
       function visPx(){
         var H = $(this).height(),
             r = el.getBoundingClientRect(), t=r.top, b=r.bottom;
         return cb.call(el, Math.max(0, t>0? H-t : (b<H?b:H)));  
       } visPx();
       $(win).on("resize scroll", visPx);
     });
  };
}(jQuery, window));

$(".steps-wrapper").inViewport(function(px){
    if(px) $('td.active').addClass("o-fade-in");
});

});

I'm not sure how to make each individual dot load in. Im currently loading in all the td's at the same time when visible. 我不确定如何使每个点加载。我当前在可见时同时加载所有td。


Update: The animation now works perfectly. 更新:动画现在完美无缺。 However if one .steps-wrapper is activated it then activates all on the page. 但是,如果激活了一个.steps-wrapper ,它将激活页面上的所有内容。 How can I make it so that other dots on the page dont animate until they are seen in the viewport just like the first one? 我怎样才能使页面上的其他点不会生成动画,直到它们在视口中看到就像第一个点一样?

Here is a new fiddle showing the issue. 这是一个显示问题的新小说

Well I have made a few changes to your code in order to work the way you want: 好吧,我已经对您的代码进行了一些更改,以便按照您想要的方式工作:

1. Make your table dots rtl that way when we apply the class will get from right to left: 1.当我们申请上课时,让你的桌子点rtl从右到左:

.steps {
  direction:rtl;
}

Also check the markup for the table on the fiddle 还要检查小提琴上桌子的标记

2. Adding the fade effect with transition : 2.通过transition添加淡入淡出效果:

.steps td {
  width: 25px;
  height: 25px;
  opacity: 0;
  visibility: hidden;
  /*Add this*/
  transition:2s linear;
}

3. Add the class one at time: 3.及时添加第一课:

$(".steps-wrapper").inViewport(function(px){
    if(px) fadeDots()
});

function fadeDots() {
  var i = 0;
  setInterval(function(){
    $('td.active').eq(i).addClass('o-fade-in');
    i++
  },100)
}

Fiddle Demo 小提琴演示


Edit:: 编辑::

After testing I see that the setInterval keeps running and can mess up your performance so also we can check when all dots are visible and then stop the interval: 经过测试后,我发现setInterval一直在运行,可能会影响你的性能,所以我们也可以检查所有点何时可见,然后停止间隔:

function fadeDots() {
  var i = 0,
      l = $('td.active').length;

  var m = setInterval(function(){
      $('td.active').eq(i).addClass('o-fade-in');
      i++
      if(i == l) {
        clearInterval(m);
      }
  },100)
}

NewFiddle NewFiddle

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

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