简体   繁体   English

用JQuery切换div后重新加载CSS

[英]Reload CSS after toggling divs with JQuery

I've found a really clean simple way of toggling the visibility of divs with filters by Tim Robert-Fitzgerald on Codepen and it works great on my site however I need to extend it slightly for my needs. 我发现了一个非常简单的方法,可以在Codepen上使用Tim Robert-Fitzgerald的过滤器切换div的可见性,它在我的网站上工作得很好但是我需要稍微扩展它以满足我的需求。

By default I have an nth-child setup that removes the border of the second div, however when the divs are toggled this isn't reapplied to the second div. 默认情况下,我有一个nth-child设置,删除第二个div的边框,但是当切换div时,这不会重新应用到第二个div。 In reality it is still applied but this is not visible because the divs are set to display:none; 实际上它仍然被应用,但这是不可见的,因为div设置为display:none;

How can I get the nth-child to re-calculate once the divs have been toggled please? 一旦div被切换,我怎样才能让nth-child重新计算?

 var $btns = $('.btn').click(function() { if (this.id == 'all') { $('#parent > div').fadeIn(450); } else { var $el = $('.' + this.id).fadeIn(450); $('#parent > div').not($el).hide(); } $btns.removeClass('active'); $(this).addClass('active'); }) 
 * { box-sizing: border-box; } body { padding: 10px; background: #ecf0f1; font-family: helvetica, sans-serif; font-weight: 200; } .btn { border: none; background: linear-gradient(to bottom, #3498db, #2980b9); border-radius: 3px; font-family: Arial; color: #ffffff; padding: 5px 10px 5px 10px; text-decoration: none; margin: 5px; } .active { background: linear-gradient(to bottom, #3cb0fd, #3498db); text-decoration: none; } .box { background:#2980b9; padding: 10px; height: 100px; width: calc(33% - 10px); float: left; margin: 5px; text-align: center; border-radius: 3px; color: #fff; border:4px solid #000; } .box:nth-child(2) { border:none; } .spacer { clear: both; height: 20px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="active btn" id="all">Show All</button> <button class="btn" id="a">Show A</button> <button class="btn" id="b">Show B</button> <button class="btn" id="c">Show C</button> <button class="btn" id="d">Show D</button> <div class="spacer"></div> <div id="parent"> <div class="box ab">A &amp; B</div> <div class="box a">A</div> <div class="box b">B</div> <div class="box c">C</div> <div class="box d">D</div> </div> 

This isn't working because the elements are still there, just hidden. 这不起作用,因为元素仍然存在,只是隐藏。 You may want to consider using an additional class to control borders (or whatever other styles it is you are wanting) and swapping those with your jQuery the same way you are adding and removing the active class. 您可能需要考虑使用其他类来控制边框(或者您想要的任何其他样式),并使用与添加和删除活动类相同的方式交换jQuery。

The CSS selector you used ( :nth-child(2) ) is not what you need because it's based on HTML DOM elements. 您使用的CSS选择器( :nth-child(2) )不是您需要的,因为它基于HTML DOM元素。 The 2nd element never changes place in the DOM, it just gets hidden. 第二个元素永远不会改变DOM中的位置,它只是被隐藏。

I don't believe there's a CSS solution to this but there is a jQuery solution. 我不相信有一个CSS解决方案,但有一个jQuery解决方案。

I took your Codepen and modified very little things 我拿了你的Codepen并修改了很少的东西

CSS (instead of .box:nth-child(2) ) CSS(而不是.box:nth-child(2)

.box.no-border {
    border:none;
}

JS JS

var $btns = $('.btn').click(function() {
  if (this.id == 'all') {
    $('#parent > div').fadeIn(450);
  } else {
    var $el = $('.' + this.id).fadeIn(450);
    $('#parent > div').not($el).hide();
  }
  $btns.removeClass('active');
  $(this).addClass('active');

  $('.box').removeClass('no-border'); // reinitialize
  $('.box:visible').eq(1).addClass('no-border'); // apply the class
})

Please note that the eq() method is 0 based index, so .eq(1) will get you the second element. 请注意, eq()方法是基于0的索引,因此.eq(1)将为您提供第二个元素。 Adding :visible will only get you those that are visible on screen. 添加:可见只会获取屏幕上可见的内容。

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

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