LoginSignup
0
1

More than 3 years have passed since last update.

クリックした際に、「ⅴ」の向きを変更する方法

Posted at
<section>
      <h1 id="one">&nbsp; メニュー1</h1>
       <ul class="menu" id="open-menu">
             <li>中身1</li>
             <li>中身2</li>
             <li>中身3</li>
        </ul>
    </section>

ulなどは、関係ない要素だが、日常で使う場合はアコーディオンメニューなどで
使うことが多いと予想されるため記載

続いてCSS

/* 記号> */
section > h1::before {
  content: '';
  width: 4px;
  height: 4px;
  border: 0;
  border-right: 2px solid black;
  border-bottom: 2px solid black;
  transform: rotate(45deg); 
  position: absolute;
  margin-top: 15px;
}
/* クリックした際に上向きにする */
.up::before {
  content: '';
  width: 4px;
  height: 4px;
  border: 0;
  border-right: 2px solid black;
  border-bottom: 2px solid black;
  transform: rotate(-135deg); 
  position: absolute;
  margin-top: 15px;
}

まず、疑似要素で>の記号を作る。
その後、jsでクリックした際に、section > h1::beforeに.up::beforeが
つくようなCSSを用意する。

const clickone = document.getElementById('one');
clickone.addEventListener('click', ()=>{
  clickone.classList.toggle('up');
});

まず、h1要素を取得して、そのh1(clickone)をクリックした場合に
.up::beforeがCSSにつくようにする

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1