英文:
Pure CSS solution to change the chevron of dropdown options on a click event?
问题
Here is the translation of the non-code part of your request:
有没有一种纯CSS的解决方案,可以在单击事件上使用CSS伪元素和动画组合来更改下拉选项的chevron?
我尝试了以下方法,它在悬停时效果良好,但我需要在单击时也能实现相同的效果。
我对JavaScript解决方案的问题在于它改变了标记。
我希望尽量避免使用JavaScript,但不改变标记。还有这个解决方案,但我不知道如何将其应用到我的情况中。
英文:
Is there a pure CSS solution to change the chevron of dropdown options using a combination of CSS pseudo-elements and animations on a click event?
I tried the following approach:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
body {
background: #000;
}
#sec_opt select {
/* Reset */
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
border: 0;
outline: 0;
font: inherit;
cursor: pointer;
position: absolute;
right: 11px;
width: 7px;
border: 6px solid transparent;
background: url(https://hueu.net/CMF/svg/chevron.svg) no-repeat;
background-position: 87px;
}
#sec_opt select:hover {
background: url(https://hueu.net/CMF/svg/chevronHover.svg) no-repeat;
background-position: 87px;
}
#sec_opt select:hover {
position: absolute;
content: "";
right: 11px;
border: 6px solid transparent;
}
#authorWrap select,
#fieldWrap select {
background-position: 180px;
}
#sec_opt select option {
color: inherit;
}
#sec_opt select:focus {
outline: none;
}
#sec_opt select::-ms-expand {
display: none;
}
#sec_opt select option {
background: #323232;
border: none;
outline: none;
}
select {
appearance: none;
/* Remove default styling */
outline: none;
/* Remove the outline */
background-color: transparent;
/* Set a transparent background */
/* Add any additional styling you want for the dropdown */
}
select option {
border-color: transparent;
/* Set the border color to transparent */
}
#sec_opt select option:hover {
background: #3e3e3e;
}
#sec_opt select {
color: #ffffff;
padding: 0px 16px;
cursor: pointer;
user-select: none;
width: 100%;
padding-right: 0px;
padding-left: 0px !important;
margin-top: 0px;
z-index: 3;
padding-bottom: 8px;
left: -3px;
}
<!-- language: lang-html -->
<section id="sec_opt">
<div class="innerWrap">
<div class="selectWrap year">
<div class="custom-select" id="yearWrap">
<label for="year">Year</label>
<select>
<option selected value="0">All</option>
<option value="1">option1</option>
<option value="2">option2</option>
<option value="3">option3</option>
</select>
</div>
</div>
</div>
</section>
<!-- end snippet -->
...which works well with hover
, but I'd need the same with an onClick
.
My problem with the JavaScript solution is that the markup is changed.
I'd wish to eliminate JavaScript as possible. Can be, but without manipulating the markup.
There's also this solution, but I don't know how to implement it to my case.
答案1
得分: 1
与`:hover`不同,你可以使用`:focus`
当失去焦点时,它会切换回默认背景。
英文:
Instead of :hover, you can use :focus
And when it loses focus it will switch back to the default background.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let sec_opt = document.querySelector("#sec_opt select");
sec_opt.addEventListener("click",(e) => {
sec_opt.classList.toggle("active")
});
<!-- language: lang-css -->
body {
background: #000;
}
#sec_opt select {
/* Reset */
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
border: 0;
outline: 0;
font: inherit;
cursor: pointer;
position: absolute;
right: 11px;
width: 7px;
border: 6px solid transparent;
background: url(https://hueu.net/CMF/svg/chevron.svg) no-repeat;
background-position: 87px;
}
#sec_opt .active{
background: url(https://hueu.net/CMF/svg/chevronHover.svg) no-repeat;
background-position: 87px;
}
#sec_opt select:hover {
position: absolute;
content: "";
right: 11px;
border: 6px solid transparent;
}
#authorWrap select,
#fieldWrap select {
background-position: 180px;
}
#sec_opt select option {
color: inherit;
}
#sec_opt select:focus {
outline: none;
}
#sec_opt select::-ms-expand {
display: none;
}
#sec_opt select option {
background: #323232;
border: none;
outline: none;
}
select {
appearance: none;
/* Remove default styling */
outline: none;
/* Remove the outline */
background-color: transparent;
/* Set a transparent background */
/* Add any additional styling you want for the dropdown */
}
select option {
border-color: transparent;
/* Set the border color to transparent */
}
#sec_opt select option:hover {
background: #3e3e3e;
}
#sec_opt select {
color: #ffffff;
padding: 0px 16px;
cursor: pointer;
user-select: none;
width: 100%;
padding-right: 0px;
padding-left: 0px !important;
margin-top: 0px;
z-index: 3;
padding-bottom: 8px;
left: -3px;
}
<!-- language: lang-html -->
<section id="sec_opt">
<div class="innerWrap">
<div class="selectWrap year">
<div class="custom-select" id="yearWrap">
<label for="year">Year</label>
<select>
<option selected value="0">All</option>
<option value="1">option1</option>
<option value="2">option2</option>
<option value="3">option3</option>
</select>
</div>
</div>
</div>
</section>
<!-- end snippet -->
<!-- begin snippet: js hide: false console: true babel: null -->
<!-- language: lang-css -->
body {
background: #000;
}
#sec_opt select {
/* Reset */
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
border: 0;
outline: 0;
font: inherit;
cursor: pointer;
position: absolute;
right: 11px;
width: 7px;
border: 6px solid transparent;
background: url(https://hueu.net/CMF/svg/chevron.svg) no-repeat;
background-position: 87px;
}
#sec_opt select:focus {
background: url(https://hueu.net/CMF/svg/chevronHover.svg) no-repeat;
background-position: 87px;
}
#sec_opt select:focus {
position: absolute;
content: "";
right: 11px;
border: 6px solid transparent;
}
#authorWrap select,
#fieldWrap select {
background-position: 180px;
}
#sec_opt select option {
color: inherit;
}
#sec_opt select:focus {
outline: none;
}
#sec_opt select::-ms-expand {
display: none;
}
#sec_opt select option {
background: #323232;
border: none;
outline: none;
}
select {
appearance: none;
/* Remove default styling */
outline: none;
/* Remove the outline */
background-color: transparent;
/* Set a transparent background */
/* Add any additional styling you want for the dropdown */
}
select option {
border-color: transparent;
/* Set the border color to transparent */
}
#sec_opt select option:focus {
background: #3e3e3e;
}
#sec_opt select {
color: #ffffff;
padding: 0px 16px;
cursor: pointer;
user-select: none;
width: 100%;
padding-right: 0px;
padding-left: 0px !important;
margin-top: 0px;
z-index: 3;
padding-bottom: 8px;
left: -3px;
}
<!-- language: lang-html -->
<section id="sec_opt">
<div class="innerWrap">
<div class="selectWrap year">
<div class="custom-select" id="yearWrap">
<label for="year">Year</label>
<select>
<option selected value="0">All</option>
<option value="1">option1</option>
<option value="2">option2</option>
<option value="3">option3</option>
</select>
</div>
</div>
</div>
</section>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论