lavina_bridge/webpages/tutorials/js-book/example.html

69 lines
1.3 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JsBook</title>
<style>
.active {
background-color: green;
}
button {
width: 50px;
height: 50px;
font-size: medium;
}
nav {
position: center;
}
</style>
</head>
<body>
<section>
<img id="output" src="../images/1.png" alt="">
</section>
<nav>
<button id="1" class="active">1</button>
<button id="2">2</button>
<button id="3">3</button>
</nav>
</body>
</html>
<script type="text/javascript">
const output = document.getElementById("output");
const buttons = document.querySelectorAll('nav button');
console.log(buttons);
let active = buttons[0];
console.log(active);
function showImage() {
current = this;
output.src = '../images/' + this.id + '.jpg';
buttons.forEach ( (el) => {
el == this ? el.className = 'active' : el.className = ''
}
)
}
buttons.forEach (
(el) => {
el.addEventListener('click', showImage)
}
)
</script>