简体   繁体   English

检查所有div宽度并调整大小,使其大于窗口

[英]Check all divs widths and resize any bigger than window

I have a blog with new responsive template. 我有一个带有新的响应模板的博客 I want to check all divs inside the posts to get any that has width bigger than window size and change the width of ONLY those divs to have max-width: windowsize. 我想检查帖子中的所有div,以获取宽度大于窗口大小的任何div,并将仅那些div的宽度更改为具有max-width:windowsize。

Truth is the blog has 350+ posts and I don't have time to edit every single post to remove the ones with divs with fixed width going offscreen. 真相是,该博客有350多个帖子,而且我没有时间编辑每一个帖子以删除固定宽度的div不在屏幕上的帖子。

I'm not very good with jQuery and js, but I got this code and got this far: 我对jQuery和js不太满意,但是我得到了这段代码,并且到目前为止:

var div = $(".post div").width();
var win = $(window).width();

if (div > win ) {
    $(" ONLY THE DIV THATS BIGGER? ").css('max-width', ' WINDOW-WIDTH? ');  
}

But it's changing the size of ALL divs. 但这正在改变所有div的大小。 Any ideas? 有任何想法吗?


[SOLUTION - for my needs, anyway] [解决方案-仍然可以满足我的需求]

As much as all your jQuery codes were amazing, I couldn't get any of them to work on the divs I wanted (but I'm sure they'll help people seeing this post in the future), so I just went for basic CSS and it fits my needs. 尽管您所有的jQuery代码都很棒,但我无法让它们中的任何一个都能在所需的div上工作(但我敢肯定,将来它们会帮助人们看到这篇文章),所以我只想了解基本信息CSS,它符合我的需求。 I was afraid of ruling ALL DIVS inside posts width:100% because it would affect the nested ones, so I just used: 我害怕裁定帖子宽度:100%内的所有DIVS,因为这会影响嵌套的帖子,所以我只使用了:

.post-body > * {
max-width:100% !important;
}

It calls ONLY all direct children of my post, which is already the same size of the window on mobile, and sets the rule max-width to prevent going off the screen. 它仅调用我帖子的所有直接子级,该子级已经与移动设备上的窗口大小相同,并设置规则最大宽度以防止其离开屏幕。 Perfect! 完善!

Thank you for your help! 谢谢您的帮助!

first of all you might want to start using a loop to go through all the divs thats being inside the variable "div" also dont try to immediately go after the width statement but get all the divs 首先,您可能想开始使用循环遍历所有div,它们都位于变量“ div”内,也不要尝试立即在width语句之后执行,但要获取所有div

so your code: 所以你的代码:

var div = $(".post div").width();
var win = $(window).width();

if (div > win ) {
    $(" ONLY THE DIV THATS BIGGER? ").css('max-width', ' WINDOW-WIDTH? ');  
}

should become something like this: 应该变成这样:

var div = $(".post div");
var win = $(window).width();

for(var i=0; i<div.length;i++){
    if(div[i].width() > win){
        $(div[i]).css('max-width','WINDOW-WIDTH?');
    }
}

also if you want to find out what exactly div is use this code: 另外,如果您想找出div的确切含义,请使用以下代码:

var div=$(".post div");
console.log(div);

The problem of overflow does not come from this. 溢出的问题并非由此而来。 In fact, it comes from the div class linkwithin_inner where the width has been set to a fixed width of 680px !important. 实际上,它来自div类linkwithin_inner ,其中宽度已设置为680px!important的固定宽度。

You should change the width to 100% in the CSS found on that page. 您应该在该页面上的CSS中将宽度更改为100%。

.linkwithin_inner {
width: 100% !important;
}

and the problem will be solved out. 问题就会解决。

try 尝试

var win = $(window).width();
$(".post div").each(function () {

   if ( $(this).width() > win ) {
      $(this).css('max-width', win );  
   }

});

You'd need to filter out the divs that are wider than the window and then set max width to only those, like below. 您需要过滤出比窗口宽的div,然后将最大宽度设置为仅那些宽度,如下所示。

var winWidth = $(window).width();

$(".post div").filter(function(){
    return $(this).width() > winWidth
}).css({"max-width": winWidth + "px"});

Hers is a demo along the same lines 她的是同样的演示

Basically you want to loop though all divs which are found by this `$(".post div")' 基本上,您想循环遍历此$(“。post div”)'找到的所有div。

Which boils down to: 归结为:

$(".post div").each(function () {
    $(this).width("50px");
});

And here you have a working fiddle: https://jsfiddle.net/gkLmx6d1/2/ 在这里,您可以使用一些小提琴: https : //jsfiddle.net/gkLmx6d1/2/

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

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