Last active
April 21, 2021 16:39
-
-
Save FARCER/6ce4f4444fbad2c3a6900c2330b615dd to your computer and use it in GitHub Desktop.
Sandwich
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.sandwich | |
.sandwich__line.sandwich__line--top | |
.sandwich__line.sandwich__line--middle | |
.sandwich__line.sandwich__line--bottom | |
.sandwich { | |
cursor: pointer; | |
height: 20px; | |
position: relative; | |
width: 32px; | |
&.is { | |
&-active { | |
.sandwich__line { | |
&--top { | |
top: 10px; | |
transform: rotate(45deg); | |
} | |
&--middle { opacity: 0; } | |
&--bottom { | |
top: 10px; | |
transform: rotate(-45deg); | |
} | |
} | |
} | |
} | |
&__line { | |
background-color: #000000; | |
display: block; | |
height: 2px; | |
left: 0; | |
position: absolute; | |
transition: all linear .4s; | |
width: 100%; | |
&--top { top: 0; } | |
&--middle { top: 9px; } | |
&--bottom { top: 18px; } | |
} | |
} | |
const sandwichToggle = function () { | |
// Выбираем элементы, которые нам нужны. В примере мы ищем элементы с классом "sandwich" | |
let sandwichElements = document.querySelectorAll('.sandwich'); | |
// Проходим циклом по всем эдементам и на каждый элемент вешаем слушателя, который по клику будет переключать класс | |
sandwichElements.forEach((item) => { | |
item.addEventListener('click', showSandwichTarget); | |
}); | |
function showSandwichTarget() { | |
let targetId = this.getAttribute('data-target-id'), | |
targetClassToggle = this.getAttribute('data-target-class-toggle'); | |
this.classList.toggle('is-active'); | |
if (targetId && targetClassToggle) { | |
document.getElementById(targetId).classList.toggle(targetClassToggle); | |
} | |
} | |
}; | |
sandwichToggle(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment