繁体   English   中英

两个在一个容器div中具有相等宽度的列表框

[英]Two listboxes with equal width in one container div

我尝试在我的在线应用程序中创建复杂的控件。 控件有两个列表框和两个按钮之间。 列表框的宽度(listbox-target,listbox-source)必须相等。 控制按钮的宽度可以固定。 所有控制的高度也必须相等。 下面是HTMLJS代码。

我试着用css做这个,但不能。 我写了javascript来计算每个元素的宽度和高度。

Width of listbox-target = listbox-source = ('fullwidth of attributeref-two-listboxes' - 'width of control-buttons') / 2. 

它工作得很好,但必须每次从resize事件调用。 有时调整大小事件不会触发,控件宽度也不会改变。

我想,最重要的是只用css制作这个,但是怎么样?! 请帮忙!

function resizeAttributerefTwoListboxes() {
var is_ff = navigator.userAgent.toLowerCase().indexOf('firefox') > -1,
    is_ie = navigator.userAgent.toLowerCase().indexOf('MSIE') > -1 || /Trident.*rv[ :]*(\d+\.\d+)/.test(navigator.userAgent);
$("div.attributeref-two-listboxes div").removeAttr("style");
$("div.attributeref-two-listboxes").each(function(){
    var that = $(this),
        target = that.find(".listbox-target"),
        source = that.find(".listbox-source"),
        control = that.find(".control-buttons");
    if (this.offsetWidth < 420) {
        control.find('a[method="add"] > i').addClass("fa-angle-up").removeClass("fa-angle-left");
        control.find('a[method="remove"] > i').addClass("fa-angle-down").removeClass("fa-angle-right");
        target.css({"width":"100%"});
        control.css({"width":"100%"});
        control.children().css({"display":"inline-block"});
        source.css({"width":"100%"});
    } else {    //horizontal alignment
        control.find('a[method="add"] > i').addClass("fa-angle-left").removeClass("fa-angle-up");
        control.find('a[method="remove"] > i').addClass("fa-angle-right").removeClass("fa-angle-down");
        var w = Math.ceil((this.offsetWidth - control[0].offsetWidth - (is_ff || is_ie ? 2 : 1))/2);
        target.css({"width":w+"px"});
        source.css({"width":w+"px"});
        control.children().css({"height":(target[0].offsetHeight-20)+"px"}); //20 - paddings from top and bottom (10+10)
        control.css({"height":target[0].offsetHeight+"px"});
    }
});
}

<div class="attributeref-two-listboxes">
<div class="listbox-target" style="width: 549px;">
    <select id="Objectint_chg_management_change_user" acode="chg_management_change_user" atype="8" astyle="3" aislist="0" class="form-control tooltipstered" multiple="multiple" style="max-width: 750px;" size="4" name="Objectint[chg_management_change_user][]"></select>
</div>
<div class="control-buttons" style="height: 90px;">
    <div style="height: 70px;">
        <a class="btn btn-primary option mover" target="Objectint_chg_management_change_user" source="Objectint_chg_management_change_user_select" method="add" href="#">
            <i class="fa fa-angle-left"></i>
        </a>
        <a class="btn btn-primary option mover" target="Objectint_chg_management_change_user" source="Objectint_chg_management_change_user_select" method="remove" href="#">
            <i class="fa fa-angle-right"></i>
        </a>
    </div>
</div>
<div class="listbox-source" style="width: 549px;">
    <select id="Objectint_chg_management_change_user_select" acode="chg_management_change_user" atype="8" astyle="3" aislist="0" class="form-control" multiple="multiple" style="max-width: 750px;" size="4" name="[]">
        <option value="68">User 1</option>
        <option value="61">User 2</option>
        <option value="76">User 3</option>
    </select>
</div>

正确调整大小后,这是我的控制

在调整错误后,这是我的控制

使用float: leftmin-width

min-width应至少为元素的宽度。 如果该部分可以跨越页面的整个宽度,使用960px并不是一个坏主意,因为它在大多数形状因子(平板电脑,智能手机,PC,占1080p显示器的1/2)中表现良好。

这样你也可以避免使用display: inline-block ,这在旧浏览器中是不受支持的(尽管那个窗口不断向前滑动,所以这些日子除了企业网络应用程序之外都没有太大的问题)。

 .attributeref-two-listboxes { min-width: 960px; overflow: hidden; font-size: 0; } .chooser { float: left; width: 300px; height: 100px; font-size: 14px; } .buttons { float: left; } .buttons input { width: 30px; height: 30px; background-color: rgb(34,131,203); color: #fff; border: 1px solid #333; display: block; margin: 13px 16px; font-size: 16px; } 
 <div class="attributeref-two-listboxes"> <select multiple size=4 class='chooser'> <option>Uno</option> <option>Dos</option> <option>Tres</option> </select> <div class='buttons'> <input type='button' value='&lt;' /> <input type='button' value='&gt;' /> </div> <select multiple size=4 class='chooser'> <option>Uno</option> <option>Dos</option> <option>Tres</option> </select> </div> 

Flex-box允许在某些情况下避免使用JS。

链接到jsfiddle

@media screen and (max-width: 500px) {
    .flex_container {
        flex-direction: column;
        flex-basis: 100%;
    }
.arrays_v {display : none}
.arrays_h {display : inline}    
}

我想我的解决方案对你有用。

暂无
暂无

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

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