使用CSS在下拉菜单中实现旋转效果

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

Making rotate in dropdown menu with CSS

问题

Here is the translated text:

我制作了一个下拉菜单,但当我点击它时,角度应该旋转180度。

当它显示为关闭时

当它显示为打开时

我尝试使用.touch:checked,但它不起作用。我怎么才能实现旋转,当我再次点击它(关闭下拉菜单时)时,角度应该再次旋转180度,我不想首先使用JavaScript,所以如果您可以用CSS帮助我,我会很高兴。但如果我们必须使用JavaScript,也没有问题,您可以使用JavaScript来帮助。

        <label for="touch" class="nav-label"><span id="services">Hizmetlerimiz</span>
          <i class="fa-solid fa-angle-down angle" style="color: #f282a6;"></i>
        </label>
        <input type="checkbox" id="touch" class="touch">
        <ul class="slide">
          <li><a href="#">Lorem Ipsum</a></li>
          <li><a href="#">Lorem Ipsum</a></li>
          <li><a href="#">Lorem Ipsum</a></li>
          <li><a href="#">Lorem Ipsum</a></li>
        </ul>
      </div>
.slide {
  list-style-type: none;
  clear:both;
  width:100%;
  height:0px;
  overflow: hidden;
  transition: height .4s ease;
}
.touch:checked ~ .angle{
  transform: rotate(180deg);
  transition: transform 200ms linear;
}
.slide li {padding : 30px;}

#touch {position: absolute; opacity: 0; height: 0px;}    

#touch:checked + .slide {height: 300px;}
英文:

Hi ı made a dropdown menu but ı want to when ı click it the angle should rotate 180 deg

When it seems in close

when it seems in open

I tried to use .touch checked but it didn't work. How can ı make the rotation and when ı click it again (when ı close the dropdown) the angle should rotate 180 deg again ı don't want to use javascript firstly so that if can you help me about this with css ı will be very happy. But if we have to use javascript no problem you can help with javascript too.

        &lt;label for=&quot;touch&quot; class=&quot;nav-label&quot;&gt;&lt;span id=&quot;services&quot;&gt;Hizmetlerimiz&lt;/span&gt;
          &lt;i class=&quot;fa-solid fa-angle-down angle&quot; style=&quot;color: #f282a6;&quot;&gt;&lt;/i&gt;
        &lt;/label&gt;
        &lt;input type=&quot;checkbox&quot; id=&quot;touch&quot; class=&quot;touch&quot;&gt;
        &lt;ul class=&quot;slide&quot;&gt;
          &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Lorem Ipsum&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Lorem Ipsum&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Lorem Ipsum&lt;/a&gt;&lt;/li&gt;
          &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Lorem Ipsum&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/div&gt;

.slide {
  list-style-type: none;
  clear:both;
  width:100%;
  height:0px;
  overflow: hidden;
  transition: height .4s ease;
}
    .touch:checked ~ .angle{
  transform: rotate(180deg);
  transition: transform 200ms linear;
}
.slide li {padding : 30px;}

#touch {position: absolute; opacity: 0; height: 0px;}    

#touch:checked + .slide {height: 300px;}  ```



</details>


# 答案1
**得分**: 0

将复选框移到`&lt;label&gt;`之前。这样,通用兄弟选择器(`~`)就可以为我们工作了。此外,`.angle`元素 *不是* 复选框的兄弟,而是一个兄弟的 *子元素*。所以我们需要使用后代组合来正确选择它。

```html
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->
.slide {
  list-style-type: none;
  clear: both;
  width: 100%;
  height: 0px;
  overflow: hidden;
  transition: height .4s ease;
}

#touch:checked ~ label .angle {
  transform: rotate(180deg);
  transition: transform 200ms linear;
}

.slide li {
  padding: 30px;
}

#touch {
  position: absolute;
  opacity: 0;
  height: 0px;
}

#touch:checked ~ .slide {
  height: 300px;
}

<!-- language: lang-html -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw==" crossorigin="anonymous" referrerpolicy="no-referrer" />

<input type="checkbox" id="touch" class="touch">
<label for="touch" class="nav-label">
  <i class="fa-solid fa-angle-down angle" style="color: #f282a6;"></i>
  <span id="services">Hizmetlerimiz</span>
</label>
<ul class="slide">
  <li><a href="#">Lorem Ipsum</a></li>
  <li><a href="#">Lorem Ipsum</a></li>
  <li><a href="#">Lorem Ipsum</a></li>
  <li><a href="#">Lorem Ipsum</a></li>
</ul>

<!-- end snippet -->

这是您提供的内容的翻译。

英文:

Move the checkbox to be before the &lt;label&gt;. This is so that the general sibling operator (~) can work for us. Furthermore, the .angle element is not a sibling of the checkbox, but rather a child of a sibling. So we will need to use a descendent combination to select it properly.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.slide {
  list-style-type: none;
  clear: both;
  width: 100%;
  height: 0px;
  overflow: hidden;
  transition: height .4s ease;
}

.touch:checked ~ * .angle {
  transform: rotate(180deg);
  transition: transform 200ms linear;
}

.slide li {
  padding: 30px;
}

#touch {
  position: absolute;
  opacity: 0;
  height: 0px;
}

#touch:checked ~ .slide {
  height: 300px;
}

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css&quot; integrity=&quot;sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;
/&gt;

&lt;input type=&quot;checkbox&quot; id=&quot;touch&quot; class=&quot;touch&quot;&gt;
&lt;label for=&quot;touch&quot; class=&quot;nav-label&quot;&gt;
  &lt;span id=&quot;services&quot;&gt;Hizmetlerimiz&lt;/span&gt;
  &lt;i class=&quot;fa-solid fa-angle-down angle&quot; style=&quot;color: #f282a6;&quot;&gt;&lt;/i&gt;
&lt;/label&gt;
&lt;ul class=&quot;slide&quot;&gt;
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Lorem Ipsum&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Lorem Ipsum&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Lorem Ipsum&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Lorem Ipsum&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月25日 18:42:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550002.html
匿名

发表评论

匿名网友

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

确定