在点击事件中更改下拉选项中的 chevron 的纯 CSS 解决方案?

huangapple go评论72阅读模式
英文:

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: &quot;&quot;;
  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 -->

&lt;section id=&quot;sec_opt&quot;&gt;
  &lt;div class=&quot;innerWrap&quot;&gt;
    &lt;div class=&quot;selectWrap year&quot;&gt;
      &lt;div class=&quot;custom-select&quot; id=&quot;yearWrap&quot;&gt;
        &lt;label for=&quot;year&quot;&gt;Year&lt;/label&gt;
        &lt;select&gt;
          &lt;option selected value=&quot;0&quot;&gt;All&lt;/option&gt;
          &lt;option value=&quot;1&quot;&gt;option1&lt;/option&gt;
          &lt;option value=&quot;2&quot;&gt;option2&lt;/option&gt;
          &lt;option value=&quot;3&quot;&gt;option3&lt;/option&gt;
        &lt;/select&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/section&gt;

<!-- 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(&quot;#sec_opt select&quot;);

sec_opt.addEventListener(&quot;click&quot;,(e) =&gt; {
  sec_opt.classList.toggle(&quot;active&quot;)
});

<!-- 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: &quot;&quot;;
  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 -->

&lt;section id=&quot;sec_opt&quot;&gt;
  &lt;div class=&quot;innerWrap&quot;&gt;
    &lt;div class=&quot;selectWrap year&quot;&gt;
      &lt;div class=&quot;custom-select&quot; id=&quot;yearWrap&quot;&gt;
        &lt;label for=&quot;year&quot;&gt;Year&lt;/label&gt;
        &lt;select&gt;
          &lt;option selected value=&quot;0&quot;&gt;All&lt;/option&gt;
          &lt;option value=&quot;1&quot;&gt;option1&lt;/option&gt;
          &lt;option value=&quot;2&quot;&gt;option2&lt;/option&gt;
          &lt;option value=&quot;3&quot;&gt;option3&lt;/option&gt;
        &lt;/select&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/section&gt;

<!-- 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: &quot;&quot;;
  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 -->

&lt;section id=&quot;sec_opt&quot;&gt;
  &lt;div class=&quot;innerWrap&quot;&gt;
    &lt;div class=&quot;selectWrap year&quot;&gt;
      &lt;div class=&quot;custom-select&quot; id=&quot;yearWrap&quot;&gt;
        &lt;label for=&quot;year&quot;&gt;Year&lt;/label&gt;
        &lt;select&gt;
          &lt;option selected value=&quot;0&quot;&gt;All&lt;/option&gt;
          &lt;option value=&quot;1&quot;&gt;option1&lt;/option&gt;
          &lt;option value=&quot;2&quot;&gt;option2&lt;/option&gt;
          &lt;option value=&quot;3&quot;&gt;option3&lt;/option&gt;
        &lt;/select&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/section&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月30日 01:00:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定