简体   繁体   English

HTML,CSS,下拉箭头,选择

[英]HTML , CSS , Dropdown Arrow , Select

I have created fiddle here, which shows select and dropdown arrow. 我在这里创建了小提琴,其中显示了选择和下拉箭头。

My problem is that the arrow is created using CSS and positioned on top of select. 我的问题是箭头是使用CSS创建的,并位于select的顶部。 however click on arrow doesn't open dropdown. 但是,单击箭头不会打开下拉菜单。

  1. is there a way to use that css as background of select like we do when we use image arrow. 有没有一种方法可以像使用图像箭头一样使用css作为select的背景。
  2. or is it possible to simulate the behavior on arrow click? 还是可以模拟单击箭头时的行为?

 .selectClass { -webkit-appearance: none; -webkit-border-radius: 0; -moz-appearance: none; border: none; width: 100px; background: #EEE; height: 30px; font-size: 20px; padding-left: 5px; } .dropDownArrow { position: relative; margin-left: 75px; margin-top: -20px; width: 0; height: 0; border-left: 10px solid transparent; border-right: 10px solid transparent; border-top: 10px solid #555; } 
 <select class="selectClass"> <option>One</option> <option>Two</option> <option>Three</option> </select> <div class="dropDownArrow"></div> 

I would create a parent div around both elements with the background color, move the dropdown arrow behind the select header, make that header background transparent. 我将使用背景颜色围绕两个元素创建一个父div,将下拉箭头移动到选择标题的后面,使该标题背景透明。 That way you see the arrow but, are only clicking on the select element box on top: 这样,您将看到箭头,但是只单击顶部的选择元素框:

 .dropDownArrow { position: absolute; left: 75px; top: 10px; width: 0; height: 0; border-left: 10px solid transparent; border-right: 10px solid transparent; border-top: 10px solid #555; } .selectClass { -webkit-appearance: none; -webkit-border-radius: 0; -moz-appearance: none; border: none; width: 100px; background: transparent; height: 30px; font-size: 20px; padding-left: 5px; position: absolute; } .dropdownWrapper { position:absolute; width: 100px; height: 30px; background: #ddd; } 
 <div class="dropdownWrapper"> <div class="dropDownArrow"></div> <select class="selectClass"> <option>One</option> <option>Two</option> <option>Three</option> </select> </div> 

By far the simplest solution here would be to ignore the mouse clicks on the arrow and let the clicks "go through" by using pointer-events: none; 到目前为止,这里最简单的解决方案是忽略鼠标在箭头上的点击,并通过使用pointer-events: none;让点击“通过” pointer-events: none; . That's the only needed change to your code, see it here: 这是对代码的唯一需要的更改,请在此处查看:

 .selectClass { -webkit-appearance: none; -webkit-border-radius: 0; -moz-appearance: none; border: none; width: 100px; background: #EEE; height: 30px; font-size: 20px; padding-left: 5px; } .dropDownArrow { pointer-events: none; position: relative; margin-left: 75px; margin-top: -20px; width: 0; height: 0; border-left: 10px solid transparent; border-right: 10px solid transparent; border-top: 10px solid #555; } 
 <select class="selectClass"> <option>One</option> <option>Two</option> <option>Three</option> </select> <div class="dropDownArrow"></div> 

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

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