简体   繁体   English

单击按钮时,每个循环应该只运行一次

[英]each loop should only run once when button is clicked

I want to hide a div when a button is clicked and then show the next div with the same class.我想在单击按钮时隐藏一个 div,然后显示具有相同 class 的下一个 div。 I have tried using this code, but when I do this, the next div that I want shown also fades out.我试过使用这段代码,但是当我这样做时,我想要显示的下一个 div 也会淡出。

 $('.arrow:last-of-type').on('click', function(){
        
        $( ".collaboration" ).each(function( index ) {
            
            if($('.collaboration').css('display') == 'block'){
                $(this).fadeOut()
                $(this).next().fadeIn()
  
            }

        })
    })

Is there a way to stop the each() from running when it is executed once and then run again when the click action happens?有没有办法阻止 each() 在执行一次时停止运行,然后在单击操作发生时再次运行?

Here's the HTML:这是 HTML:

<div id="collaborations">
      <p class="arrow"><</p>
        <div class="collaboration">
            <img src="./img/test.png" />
            <p>Text</p>
            <a href="#" target="_blank"><button>Visit</button></a>
        </div>
        
        <div class="collaboration">
            <img src="./img/test.png" />
            <p>Text</p>
            <a href="#" target="_blank"><button>Visit</button></a>
        </div>

        <div class="collaboration">
             <img src="./img/test.png" />
             <p>Text</p>
             <a href="#" target="_blank"><button>Visit</button></a>
        </div>

        <p class="arrow">></p>
      </div>
    </div>  

Thanks谢谢

Solution解决方案

You could solve this by not iterating through the .collaboration s at all.你可以通过不遍历.collaboration来解决这个问题。

The key thing is that you need to keep track of which one is currently being shown.关键是您需要跟踪当前显示的是哪一个。 If you know that, then what your click handler can do is show the next one and hide the current one.如果您知道这一点,那么您的点击处理程序可以做的是显示下一个并隐藏当前的。

I would suggest doing that with a class .active on the same div as .collaboration .我建议在与 .collaboration 相同的 div 上使用.collaboration .active You can then select the next div by $('.active').next().addClass('.active') , and deselect by $('.active').removeClass('.active') .然后,您可以通过$('.active').next().addClass('.active') select 下一个 div,并通过$('.active').removeClass('.active') ('.active').removeClass('.active') 取消选择。

You might need to store a reference to your first element before you select the next one您可能需要在 select 下一个元素之前存储对第一个元素的引用

Example例子

Here's a quick example of how this might work: https://codepen.io/juancaicedo/pen/LYGgPWa这是一个如何工作的简单示例: https://codepen.io/juancaicedo/pen/LYGgPWa

I moved around the html to group all the collaborations into a div by themselves.我在 html 周围移动,将所有协作单独分组到一个 div 中。

Other approaches其他方法

You'll find that you have to think through some other behaviors with the solution above.您会发现您必须通过上述解决方案考虑其他一些行为。 For example, in my example, there is a moment when two items are on the screen, causing their container div to grow and later shrink.例如,在我的示例中,有两个项目出现在屏幕上的时刻,导致它们的容器 div 增长并随后缩小。

For these reasons, I don't like handling presentation from within javascript (ie using jquery's fadeIn / fadeOut ).由于这些原因,我不喜欢在 javascript 中处理演示文稿(即使用 jquery 的fadeIn / fadeOut )。

If you can find a way to instead using only css, I think that's preferable.如果您能找到一种仅使用 css 的方法,我认为这是更可取的。 Here's an example using css transitions https://codepen.io/juancaicedo/pen/ZEQqzrR这是一个使用 css 转换的示例https://codepen.io/juancaicedo/pen/ZEQqzrR

The each method of jQuery can be stopped whenever you want by returning false inside of the callback, so you can probably fix your code by doing this: jQueryeach方法可以随时通过在回调中返回 false 来停止,因此您可以通过执行以下操作来修复您的代码:

$('.arrow:last-of-type').on('click', function(){

    $( ".collaboration" ).each(function( index ) {

        if($('.collaboration').css('display') == 'block'){
          $(this).fadeOut()
          $(this).next().fadeIn()
          return false;
        }

    });
});

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

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