[英]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.