【问题标题】:How to add arrow to select?如何添加箭头选择?
【发布时间】:2013-08-07 21:02:17
【问题描述】:

我正在尝试重新设置选择框的样式。想要添加一个 CSS 箭头(缩放页面时看起来不错),它会随着选择框的展开而旋转,但我无法根据选择框正确对齐箭头。尝试使用来自互联网的一些代码,但它对我不起作用。选择框展开时也不能让箭头向下旋转。

问题:

  1. 如何正确对齐箭头?

  2. 如何让它在用户点击选择框时向下旋转?

这是一个小提琴http://jsfiddle.net/QYtaS/ 如您所见,箭头绝对飞出选择框。

HTML:

<div class="arrow">
    <select>
        <option></option>
        <option value="1">1</option>
        <option value="2">2</option>
    </select>
</div>

CSS:

select {
    width: 100px;
    margin: 0;
    background-color: #FFFEFD;
    border: solid 1px #F15922;
    color: #000;
    outline:none;
    display: inline-block;
    -webkit-appearance:none;
    -moz-appearance:none;
    appearance:none;
    cursor:pointer;
}
.arrow {
    position:relative
}
.arrow:after {
    content:'';
    font: 1em"Consolas", monospace;
    width: 0;
    height: 0;
    position: absolute;
    right:8px;
    top:50%;
    padding: 0 0 -25% 0;
    margin-right: 3px;
    border-width: 8px 0 8px 8px;
    border-style: solid;
    border-color: transparent #F15922;
    pointer-events:none;
}

.arrow:before {
    content:'';
    right:6px;
    top:0px;
    width:20px;
    height:20px;
    position:absolute;
    pointer-events:none;
    display:block;
}

【问题讨论】:

  • 不要试图转动标准选择框。真的。压抑。使用 jquery 在 div 中构建自己的。
  • 您需要支持哪些浏览器?如果 IE8 是混合的,这很困难。我个人使用背景图片而不是 :after
  • 好吧,我想至少支持 Chrome、Mozilla 和 IE(最新版本)。我用背景图像做了 versin,但我仍然需要将其调低并重新着色为绿色。至少对此解决方案有任何想法吗?
  • 别管可怜的选择框了。您的用户会感谢您的。

标签: css html html-select css-shapes


【解决方案1】:

这是您在 jsfiddle 的答案:http://jsfiddle.net/ry7ykesy/3/

我对您提供的 html 结构进行了一项更改。

/* This is JAVASCRIPT code */

/* This click event to add class "open" to "custom-selectbox" */
$('.custom-selectbox #day').on('click', function( e ) {
   $(this).parent().toggleClass("open");
   return false;
});
/* This click event on document to detect outside click */
$(document).on('click', function(e) {
	var $selectBoxContainer = $('.custom-selectbox');
	if($selectBoxContainer.hasClass('open')){
		$selectBoxContainer.removeClass('open');
	}
	else{
		return false;
	}
});
/* This is CSS code */

select {
	width: 150px;
	background-color: #FFFEFD;
	border: solid 1px #F15922;
	color: #000;
	outline: none;
	display: inline-block;
	-webkit-appearance: none;
	-moz-appearance: none;
	appearance: none;
	cursor: pointer;
	height: 20px;
	padding-right: 20px;
}
.custom-selectbox{
	position: relative;
	display: inline-block;
}
.custom-selectbox:after{
	content: " ";
	height: 0;
	width: 0;
	border-left: 5px solid transparent;
	border-right: 5px solid transparent;
	border-top: 5px solid #F15922;
	position: absolute;
	right: 6px;
	top: 8px;
	transition: all 0.3s linear;
}
.custom-selectbox.open:after{
	-webkit-transform: rotate(-180deg);
	-moz-transform: rotate(-180deg);
	-o-transform: rotate(-180deg);
	transform: rotate(-180deg);
}
<!-- This is HTML Code -->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="custom-selectbox">
	<select name="day" id="day" required="required">
		<option>-select-</option>
		<option value="1">1</option>
		<option value="2">2</option>
	</select>
</div>

【讨论】:

    【解决方案2】:

    select {
        width: 100px;
        margin: 0;
        background-color: #FFFEFD;
        border: solid 1px #F15922;
        color: #000;
        outline:none;
        display: inline-block;
        -webkit-appearance:none;
        -moz-appearance:none;
        appearance:none;
        cursor:pointer;
    }
    .arrow {
        position:relative;
        display:inline-block;
    }
    .arrow:after {
        content: '';
        font: 1em"Consolas", monospace;
        width: 0;
        height: 0;
        position: absolute;
        right: 3px;
        top: 1px;
        margin-right: 0;
        border-width: 8px 0 8px 8px;
        border-style: solid;
        border-color: transparent #F15922;
        pointer-events: none;
        transition: all 0.3s linear;
    }
    
    .arrow:before {
        content:'';
        right:6px;
        top:0px;
        width:20px;
        height:20px;
        position:absolute;
        pointer-events:none;
        display:block;
    }
    
    .arrow:focus-within:after {
        right: 6px;
        transform: rotate(90deg);
    }
    <div class="arrow">
        <select name="day" id="day" required="required">
            <option></option>
            <option value="1">1</option>
            <option value="2">2</option>
        </select>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-04
      • 2015-09-22
      相关资源
      最近更新 更多