index.html
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Аккордеон</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<button class="accordion">Section 1</button>
<div class="panel"><p>Here goes text 1.</p></div>
<button class="accordion">Section 2</button>
<div class="panel"><p>Here goes text 2.</p></div>
<button class="accordion">Section 3</button>
<div class="panel"><p>Here goes text 3.</p></div>
<script src="script.js"></script>
</body>
</html>
style.css
button.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;
}
button.accordion.active,
button.accordion:hover {
background-color: #ddd;
}
div.panel {
padding: 0 18px;
background-color: white;
display: none;
}
div.panel.show {
display: block;
}
script.js
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
}
}
/*
Свойство nextElementSibling содержит соседа снизу (следующий элемент).
*/
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title>Аккордеон</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<button class="accordion">Section 1</button>
<div class="panel"><p>Here goes text 1.</p></div>
<button class="accordion">Section 2</button>
<div class="panel"><p>Here goes text 2.</p></div>
<button class="accordion">Section 3</button>
<div class="panel"><p>Here goes text 3.</p></div>
<script src="script.js"></script>
</body>
</html>
style.css
button.accordion {
background-color: #eee;
color: #444;
cursor: pointer;
padding: 18px;
width: 100%;
text-align: left;
border: none;
outline: none;
transition: 0.4s;
}
button.accordion.active,
button.accordion:hover {
background-color: #ddd;
}
div.panel {
padding: 0 18px;
background-color: white;
display: none;
}
div.panel.show {
display: block;
}
script.js
var acc = document.getElementsByClassName("accordion");
var i;
for (i = 0; i < acc.length; i++) {
acc[i].onclick = function() {
this.classList.toggle("active");
this.nextElementSibling.classList.toggle("show");
}
}
/*
Свойство nextElementSibling содержит соседа снизу (следующий элемент).
*/
Комментариев нет:
Отправить комментарий