[英]Each element height if higher then function
Good day, I'm trying to figure out how to do small thing. 美好的一天,我试图找出如何做小事情。
I have several elements on page that needs to checked on height and if height is higher, for example > 100, then I need to call function for them. 我在页面上有几个元素需要检查高度,如果高度更高(例如> 100),则需要为其调用函数。
So far I came up to this: 到目前为止,我想到了这一点:
var filterHeight = $('div.class').height();
if (filterHeight > 100) {
$('div.class').css('height', '100px');
$('div.class').css('overflow-y', 'scroll');
}
Problem with that as you know is that it will add those params to all element because I strictly point to that. 如您所知,问题在于它将这些参数添加到所有元素中,因为我严格指出了这一点。
I guess it I will need to do something with .each(), but... 我想我需要用.each()做些什么,但是...
Thank you for your suggestions and help. 感谢您的建议和帮助。
Because I can't answer my own question I'm updating here. 因为我无法回答自己的问题,所以我在这里进行更新。
I used your solution and change it a little bit, instead adding inline css I added class that fetch css. 我使用了您的解决方案并对其进行了一些更改,而不是添加了内联css,我添加了获取css的类。
So what I did is: 所以我所做的是:
1) When page loads 1)页面加载时
$('div.class').each(function() {
if ($(this).height() > 200) {
$(this).addClass('scroller');
}
});
.scroller {
height: 200px;
overflow-y: scroll !important;
} }
So it checked all blocks with height more than 200px and adds class. 因此,它检查了所有高度超过200px的块并添加了类。
2) After I'm calling for Ajax/JOSN and it gives me new data for those blocks I was in need to check for those elements heigh changes. 2)在我调用Ajax / JOSN之后,它为我提供了那些块的新数据,我需要检查这些元素的高度变化。 So on .ajax complete I removed class and check again.
因此,在.ajax完成时,我删除了类并再次检查。
complete: function () {
$('.box.refine .middle ul').removeClass('scroller')
$('.box.refine .middle ul').each(function() {
if ($(this).height() > 200) {
$(this).addClass('scroller');
}
});
}
Thats all. 就这样。
You need to look at jQuery .each() 您需要查看jQuery .each()
Something like: 就像是:
$('.check-these').each( function(){
if ($(this).height() > 100) {
$(this).css('height', '100px');
$(this).css('overflow-y', 'scroll');
}
});
This isn't tested but should put you on the right track. 这未经测试,但应该使您走上正确的道路。
$('div.class').each(function(){
if($(this).height() > 100)
{
$(this).css({height: "100px", overflow-y: "scroll"});
}
});
Loop through the elements and check each individually: 遍历元素并逐一检查:
$('div.class').each(function() {
if ($(this).height() > 100) {
$(this).css('height', '100px').css('overflow-y', 'scroll');
}
});
But why not just put that in the css to begin with? 但是,为什么不先将其放在CSS中呢?
div.class {
height: 100px;
overflow-y: scroll;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.