简体   繁体   English

在Windows.resize上切换类

[英]Toggle class on windows.resize

I'm using Isotope (with Packery) to create some fancy responsive grid on my website. 我正在使用同位素(与Packery)在我的网站上创建一些漂亮的响应式网格。 With jQuery I'm trying to get all elements in the first row and give them some styles: 使用jQuery,我试图在第一行中获取所有元素并为其提供一些样式:

$('.grid .grid-item').filter(function() {
    return $(this).css('top') == '0px';
}).css("opacity", "0.5");

On large screens there are two grid-elements in a row. 在大屏幕上,连续有两个网格元素。 On small screens there is only one. 在小屏幕上只有一个。 To make my script responsive I've added $windows.on('resize') around my script. 为了使脚本具有响应性,我在脚本周围添加了$windows.on('resize')

$(window).on('resize', function(){
    $('.grid .grid-item').filter(function() {
        return $(this).css('top') == '0px';
    }).css("opacity", "0.5");
});

The script works quite fine, but if I resize the screen to get the small-version (grid-items with 100% width) the second grid-item still has the css-property. 该脚本可以正常工作,但是如果我调整屏幕大小以获取小版本(宽度为100%的网格项目),则第二个网格项目仍然具有css属性。 Is there a way to get rid of the css-style on resize? 有没有办法摆脱大小调整的CSS样式?

Check it out (and resize the screen)! 签出(并调整屏幕大小)!

The problem with the method you have implemented is that Isotope takes time to reposition each .grid-item , so at the time you are checking for items with a top of 0 they may not have finished moving into their final position. 您实现的方法的问题在于,同位素需要花费时间来重新定位每个.grid-item ,因此在检查顶部为0的项目时,它们可能尚未完成移动到最终位置。

In addition, you are not resetting the items' opacity so once an item has been given the style it doesn't change back. 此外,您不会重置项目的不透明度,因此一旦为项目指定了样式,它就不会更改。

And finally, the resize event is called many times during the resize and should be debounced to only actually execute the changes once the resize event has stopped. 最后,调整大小事件在调整大小期间被多次调用,并且应该在重新调整大小事件停止后进行去抖动处理,以便仅实际执行更改。 Something like: 就像是:

var debouncer;
$(window).on('resize', function(){
  clearTimeout(debouncer);
  debouncer=setTimeout(function(){
    $('.grid .grid-item').css("opacity", "1").filter(function() {
        return $(this).css('top') == '0px';
    }).css("opacity", "0.5");
  },200);
});

https://codepen.io/anon/pen/oYMKRj https://codepen.io/anon/pen/oYMKRj

So even with the above amends you will struggle to determine which .grid-item s are at the top unless you wait for Isotope to complete its animation (which is a clue to how we solve this)... 因此,即使进行了上述修正, 除非您等待同位素完成动画(这是我们如何解决此问题的线索), 否则.grid-item很难确定哪个.grid-item位于顶部。

So How? 又怎样?

Luckily Isotope and Packery have their own custom events you can listen for. 幸运的是, 同位素包装厂都有自己的自定义事件,您可以收听。 So instead of the above you can just listen for the layoutComplete event which is triggered (as you might expect) when the layout is completed ... 因此,除了上面的内容外,您还可以只听布局完成时触发的layoutComplete事件(您可能希望如此)...

$grid.on('layoutComplete', function(event,items){// when the layout completes
  $('.grid .grid-item')// get all grid-items
      .css("opacity", "1")// reset their opacity
      .filter(function() {// filter to return
          return $(this).css('top') == '0px'; // the items with a top of 0
      })
      .css("opacity", "0.5");//set the opacity of the matched items
});

https://codepen.io/anon/pen/vyzBYy https://codepen.io/anon/pen/vyzBYy

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

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