[英]Two listboxes with equal width in one container div
I try to create complex control in my online application. 我尝试在我的在线应用程序中创建复杂的控件。 Control have two listboxes and two buttons between.
控件有两个列表框和两个按钮之间。 Width of listboxes (listbox-target, listbox-source) must be equal.
列表框的宽度(listbox-target,listbox-source)必须相等。 Width of control-buttons can be fixed.
控制按钮的宽度可以固定。 Height of all controls must be equal too.
所有控制的高度也必须相等。
HTML
and JS
code below. 下面是
HTML
和JS
代码。
I try to make this with css, but can't. 我试着用css做这个,但不能。 I wrote javascript to calculacte width and height of each element.
我写了javascript来计算每个元素的宽度和高度。
Width of listbox-target = listbox-source = ('fullwidth of attributeref-two-listboxes' - 'width of control-buttons') / 2.
It work good, but must be called everytime from resize event. 它工作得很好,但必须每次从resize事件调用。 Sometimes resize event is not fire and width of controls not change.
有时调整大小事件不会触发,控件宽度也不会改变。
I think, best of all is make this with css only, but how?! 我想,最重要的是只用css制作这个,但是怎么样?! Please, help!
请帮忙!
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>
Use float: left
and min-width
. 使用
float: left
和min-width
。
The min-width
should be at least the widths of the elements. min-width
应至少为元素的宽度。 If the section can span the entire width of the page, using 960px
is not a bad idea because it displays well in most form factors (tablets, smartphones, PCs, occupies 1/2 of a 1080p display). 如果该部分可以跨越页面的整个宽度,使用
960px
并不是一个坏主意,因为它在大多数形状因子(平板电脑,智能手机,PC,占1080p显示器的1/2)中表现良好。
This way you can also avoid using display: inline-block
which is not supported in older browsers (though that window keeps sliding forward so this isn't much of a problem these days except for enterprise web apps,etc.). 这样你也可以避免使用
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='<' /> <input type='button' value='>' /> </div> <select multiple size=4 class='chooser'> <option>Uno</option> <option>Dos</option> <option>Tres</option> </select> </div>
Flex-box allow to avoid using JS in some cases like your. Flex-box允许在某些情况下避免使用JS。
Link to jsfiddle . 链接到jsfiddle 。
@media screen and (max-width: 500px) {
.flex_container {
flex-direction: column;
flex-basis: 100%;
}
.arrays_v {display : none}
.arrays_h {display : inline}
}
I suppose my my solution will be useful for you. 我想我的解决方案对你有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.