Rub*_*ben 19 css jquery-select2
我在项目中使用Select2来设置搜索表单中某些选择框的样式.我设法将箭头容器的渐变背景更改为黑色渐变:
.select2-container .select2-choice .select2-arrow {
background-image: -khtml-gradient(linear, left top, left bottom, from(#424242), to(#030303));
background-image: -moz-linear-gradient(top, #424242, #030303);
background-image: -ms-linear-gradient(top, #424242, #030303);
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #424242), color-stop(100%, #030303));
background-image: -webkit-linear-gradient(top, #424242, #030303);
background-image: -o-linear-gradient(top, #424242, #030303);
background-image: linear-gradient(#424242, #030303);
}
Run Code Online (Sandbox Code Playgroud)
我希望箭头是白色的,但不幸的是,Select2使用背景图像作为不同的图标而不是字体 - 真棒或类似的东西,所以没有办法只用CSS改变颜色.
什么是使箭头变为白色而不是默认灰色的最简单方法?我真的要用自己的替换背景png(select2.png和select2x2.png)吗?或者有更简单的方法吗?
我的另一个问题是如何更改选择框的高度.我知道如何在打开状态下更改下拉框的高度,但我想在闭合状态下更改选择框的高度.有任何想法吗?
Rub*_*ben 37
感谢评论中的建议.我做了一些肮脏的黑客来得到我想要的东西而不必创建我自己的图像.使用javascript我首先隐藏用于向下箭头的默认标记,如下所示:
$('b[role="presentation"]').hide();
Run Code Online (Sandbox Code Playgroud)
然后我在我的页面中包含了字体 - awesome并添加了我自己的向下箭头,再次使用一行javascript来替换默认值:
$('.select2-arrow').append('<i class="fa fa-angle-down"></i>');
Run Code Online (Sandbox Code Playgroud)
然后用CSS设置选择框的样式.我设置高度,将箭头区域的背景颜色更改为渐变黑色,将宽度,字体大小以及向下箭头的颜色更改为白色:
.select2-container .select2-choice {
padding: 5px 10px;
height: 40px;
width: 132px;
font-size: 1.2em;
}
.select2-container .select2-choice .select2-arrow {
background-image: -khtml-gradient(linear, left top, left bottom, from(#424242), to(#030303));
background-image: -moz-linear-gradient(top, #424242, #030303);
background-image: -ms-linear-gradient(top, #424242, #030303);
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #424242), color-stop(100%, #030303));
background-image: -webkit-linear-gradient(top, #424242, #030303);
background-image: -o-linear-gradient(top, #424242, #030303);
background-image: linear-gradient(#424242, #030303);
width: 40px;
color: #fff;
font-size: 1.3em;
padding: 4px 12px;
}
Run Code Online (Sandbox Code Playgroud)
结果是我想要它的样式:

2015 年5月6日更新正如@Katie Lacy在另一个答案中提到的,类名在Select2的第4版中已经更改.带有新类名的更新CSS应如下所示:
.select2-container--default .select2-selection--single{
padding:6px;
height: 37px;
width: 148px;
font-size: 1.2em;
position: relative;
}
.select2-container--default .select2-selection--single .select2-selection__arrow {
background-image: -khtml-gradient(linear, left top, left bottom, from(#424242), to(#030303));
background-image: -moz-linear-gradient(top, #424242, #030303);
background-image: -ms-linear-gradient(top, #424242, #030303);
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #424242), color-stop(100%, #030303));
background-image: -webkit-linear-gradient(top, #424242, #030303);
background-image: -o-linear-gradient(top, #424242, #030303);
background-image: linear-gradient(#424242, #030303);
width: 40px;
color: #fff;
font-size: 1.3em;
padding: 4px 12px;
height: 27px;
position: absolute;
top: 0px;
right: 0px;
width: 20px;
}
Run Code Online (Sandbox Code Playgroud)
JS:
$('b[role="presentation"]').hide();
$('.select2-selection__arrow').append('<i class="fa fa-angle-down"></i>');
Run Code Online (Sandbox Code Playgroud)
小智 8
这是上面的一个工作示例.http://jsfiddle.net/z7L6m2sc/ 现在select2已更新,类有变化可能是你无法让它工作的原因.这是css ....
.select2-dropdown.select2-dropdown--below{
width: 148px !important;
}
.select2-container--default .select2-selection--single{
padding:6px;
height: 37px;
width: 148px;
font-size: 1.2em;
position: relative;
}
.select2-container--default .select2-selection--single .select2-selection__arrow {
background-image: -khtml-gradient(linear, left top, left bottom, from(#424242), to(#030303));
background-image: -moz-linear-gradient(top, #424242, #030303);
background-image: -ms-linear-gradient(top, #424242, #030303);
background-image: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #424242), color-stop(100%, #030303));
background-image: -webkit-linear-gradient(top, #424242, #030303);
background-image: -o-linear-gradient(top, #424242, #030303);
background-image: linear-gradient(#424242, #030303);
width: 40px;
color: #fff;
font-size: 1.3em;
padding: 4px 12px;
height: 27px;
position: absolute;
top: 0px;
right: 0px;
width: 20px;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
这就是我更改占位符箭头颜色的方法,这两个类用于下拉菜单打开和下拉菜单关闭,您需要将 #fff 更改为您想要的颜色:
.select2-container--default.select2-container--open .select2-selection--single .select2-selection__arrow b {
border-color: transparent transparent #fff transparent !important;
}
.select2-container--default .select2-selection--single .select2-selection__arrow b {
border-color: #fff transparent transparent transparent !important;
}
Run Code Online (Sandbox Code Playgroud)