[英]Jquery doesn't change div's width on smaller screen after auto adjusting on regular screen
获得了根据子代数自动调整子代div宽度的脚本。 在较小的屏幕上,我需要它们的宽度为100%。 我尝试将脚本直接插入可调整常规屏幕宽度的脚本中。 不起作用 还尝试将其插入页面的底部。 也没用。
这是代码
$(".basecollections").each(function(){
var child_width = 100 / $(this).children().length;
child_width = Math.floor(child_width);
$(this).children().css("width", child_width + "%");
if ( screen.width < 1000 ) {
$(this).children().css('width','100%');
}
})
代替screen.width
您需要使用$(window).width()
: DEMO
$(".basecollections").each(function(){
var child_width = 100 / $(this).children().length;
child_width = Math.floor(child_width);
$(this).children().css("width", child_width + "%");
if ( $(window).width() < 1000 ) {
$(this).children().css('width','100%');
}
});
同样,如果您希望更改是动态的,则需要在window元素的resize事件中编写以下代码:
$(window).resize(function(){
$(".basecollections").each(function(){
var child_width = 100 / $(this).children().length;
child_width = Math.floor(child_width);
$(this).children().css("width", child_width + "%");
if ( $(window).width() < 1000 ) {
$(this).children().css('width','100%');
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.