简体   繁体   English

用相同的类名更改多个div的宽度的脚本

[英]Script that change the width of multiple divs with the same class name

After gotten help from @michael-p who had made a script that works excellent, i was looking for some code that could work better in my new situation (old topic link) . 在得到了一个非常出色的脚本的@ michael-p的帮助之后,我正在寻找一些可以在我的新情况下更好地工作的代码(旧主题链接)

The "container" class or id (as shown below) displays flexbox content ("box") who go from top to bottom and when reaching the bottom makes another row to the right en start over. “容器”类或id(如下所示)显示从上到下的Flexbox内容(“box”),当到达底部时,右边的另一行重新开始。
This was a issue because as explained in my old topic, Chrome doesn't recognize the flexbox content and does not stretch the width. 这是一个问题,因为正如我在旧主题中所解释的那样,Chrome无法识别Flexbox内容并且不会拉伸宽度。
Michael P solved this however through some scripting but the script was made for one "container" class (it freaks out when multiple divs has the same class name, because they have different amount of "box" classes inside it, so different widths). 迈克尔P通过一些脚本解决了这个问题,但脚本是针对一个“容器”类制作的(当多个div具有相同的类名时它会变得怪异,因为它们里面有不同数量的“盒子”类,因此宽度不同)。

What i would like to achieve: 我想要实现的目标:
A script (can be modified from Michael-P script) that focuses only in the div that its placed in, and stops when the div stops, so there would be no need to specify a certain class by name. 一个脚本(可以从Michael-P脚本修改),它只关注它所放置的div,并在div停止时停止,因此不需要按名称指定某个类。
Doing so there would be no limit of how many div copies there could be with the same class name but different amount of "box" contents in it. 这样做不会限制具有相同类名但可以使用不同数量的“盒子”内容的div副本数量。
Fiddle from Michael P 迈克尔P的小提琴

<div id="container">  
    <div class="box">1</div>  
    <div class="box">2</div>  
    <div class="box">3</div>  
    <div class="box">4</div>  
    <div class="box">5</div>  
    <div class="box">6</div>  
    <div class="box">7</div>  
    <div class="box">8</div>  
    <div class="box">9</div>  
    <div class="box">10</div>  
    <div class="box">11</div>  
    <div class="box">12</div>  
    <div class="box">13</div>  
    maybe here the script? that stops when </div> is shown?
</div>

Script 脚本

// Find the biggest offset right
var maxOffsetRight = 0,
    currentOffsetRight;

$('#container').children().each(function(index) {
    currentOffsetRight = $(this).position().left + $(this).outerWidth(true);
if (currentOffsetRight > maxOffsetRight) {
    maxOffsetRight = currentOffsetRight;
}
});

$('#container').css('width', maxOffsetRight);

JS FIDDLE JS FIDDLE

Change the id of container to a class, and then just iterate over each container class. 将容器的id更改为类,然后迭代每个容器类。 You'll also need to change the scope of your maxOffsetRight var then from global to only applying to each iteration of container. 您还需要将maxOffsetRight var的范围从全局更改为仅应用于容器的每次迭代。

$('.container').each(function(){
    var maxOffsetRight = 0;
    var currentOffsetRight - 0;
    $(this).children().each(function(index) {
       currentOffsetRight = $(this).position().left + $(this).outerWidth(true);
       if (currentOffsetRight > maxOffsetRight) {
           maxOffsetRight = currentOffsetRight;
       }
    });

    $(this).css('width', maxOffsetRight);
})

The script I wrote for your previous question was designed for the case you presented, ie with one flexbox container. 我为您之前的问题编写的脚本是针对您提供的案例而设计的,即使用一个Flexbox容器。 But it could be easily adapted for a whole set of flexbox containers. 但它可以很容易地适应整套flexbox容器。

First we will have to change the id to a class, because we will have more than one container. 首先,我们必须将id更改为类,因为我们将拥有多个容器。 Then, we will iterate on each container, and compute the largest offset right of the flexbox items. 然后,我们将迭代每个容器,并计算flexbox项目的最大偏移量。 Therefore we have to put the variable initialization inside a function iterated on the containers. 因此,我们必须将变量初始化放在迭代容器的函数中。 As follows : 如下 :

$('.container').each(function() {

    var maxOffsetRight = 0,
        currentOffsetRight;

    // Find the biggest offset right of all childs 
    $(this).children().each(function(index) {
        currentOffsetRight = $(this).position().left + $(this).outerWidth(true);
        if (currentOffsetRight > maxOffsetRight) {
            maxOffsetRight = currentOffsetRight;
        }
    });

   // Set the width of the current container
   var offsetLeft = $(this).position().left;
   $(this).css('width', maxOffsetRight - offsetLeft);                     
});

Note that when setting the width, we have to substract the offsetLeft of the container. 请注意,在设置宽度时,我们必须减去容器的offsetLeft。 Should have done that in the previous version, but didn't notice it as there was only one container, and he was at an offset left of 0. 应该在之前的版本中完成,但没有注意到它,因为只有一个容器,并且他在0的左偏移处。

Fiddle 小提琴

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

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