Chapter.6JavaScript(jQuery)基礎

04クリックイベントでクリックした要素の疑似要素を変更する

イベント発生させる要素(と操作したい要素)のID名 #chapter6-4-btn
追加または削除するクラス名 .active

-- js --
$(function(){
  $('#chapter6-4-btn').click(function(){
    $(this).toggleClass('active');
  });
});

-- css --
.group {
  position: relative;
  width: 400px;
  height: 100px;
  margin: 0 auto;
  background: #ddd;
}
.group::before {
  content: "";
  position: absolute;
  top: 50%;
  right: 20px;
  width: 10px;
  height: 10px;
  margin-top: -4px;
  border-bottom: 2px solid #000;
  border-left: 2px solid #000;
  transform: rotate(-45deg) translateY(-50%);
}
.group.active::before {
  margin-top: 1px;
  border-bottom: none;
  border-left: none;
  border-top: 2px solid #000;
  border-right: 2px solid #000;
}