简体   繁体   English

检查一定宽度的div

[英]Check for div's with a certain width

I have a bunch of div's that I have set to animate at the moment. 目前,我已经设置了许多div动画。 However I am wanting a way that if they've been animated (Their width would have changed from 100px to 300px for example) they won't reanimate when I click on them again. 但是我想要一种方法,如果它们已经过动画处理(例如,它们的宽度将从100px变为300px),则当我再次单击它们时它们不会恢复动画。 I'm new to javascript & jquery so I'm unsure how to check the width of a div element to place in an if statement. 我是javascript&jquery的新手,所以我不确定如何检查放置在if语句中的div元素的宽度。 If I need to post the code I can if requested. 如果我需要发布代码,可以应要求提供。 Thanks 谢谢

Like this: 像这样:

$('selector')
    .filter(function() { return $(this).width() < 300; })
    .animate(...);

You should add a class to all elements you want to animate 您应该将类​​添加到所有要设置动画的元素中

<div class="animateMe something something"> 
content
</div>

and in jQuery 并在jQuery中

$('div.animateMe').animate({animate params},duration,function(){
     $(this).removeClass('animateMe');
});

this will act as a flag and once animation has been done, it wont happen again on same element irrespective of height and width. 这将作为一个标志,一旦动画完成,无论高度和宽度如何,它都不会在同一元素上再次发生。

Advantage: Your code will not depend upon height/width params. 优点:您的代码将不依赖于高度/宽度参数。 if tomorrow animation changes and your blocks animate to a different height/width, this code will still be valid. 如果明天的动画改变并且您的块以不同的高度/宽度动画,则此代码将仍然有效。

syntax for animate is jQuery.animate({animation params}, duration, callback) ; 动画的语法是jQuery.animate({animation params},duration,callback) ;

Caution : Don't use the animateMe class for styling 警告 :请勿使用animateMe类进行样式设置

You can use : 您可以使用 :

if($(".bunch-of-divs-class").width() == something)
{
     //Do Something
}

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

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