I have designed a webpage with a navigation bar. However, when I click on a button in the navbar, the image refreshes and does not display correctly.
I attempted to show different pictures when each button in the navbar is clicked. Each button should display a unique image. I managed to create the buttons and link them to the JavaScript file successfully.
function changePhotos(e) {
if (e.target == first) {
document.getElementById("images").style.backgroundImage =
'url("image' + 1 + '.jpg")';
return;
}
if (e.target == sec) {
document.getElementById("images").style.backgroundImage =
'url("image' + 2 + '.jpg")';
return;
}
if (e.targer == third) {
document.getElementById("images").style.backgroundImage =
'url("image' + 3 + '.jpg")';
return;
}
}
let first = document.getElementById("pic1");
let sec = document.getElementById("pic2");
let third = document.getElementById("pic3");
first.addEventListener("click", changePhotos);
sec.addEventListener("click", changePhotos);
third.addEventListener("click", changePhotos);
body {
height: 100vh;
width: 100vw;
}
.container-fluid {
position: relative;
}
#bar {
position: relative;
background-color: aqua;
}
#images {
position: absolute;
height: 94%;
bottom: 0;
width: 100%;
}
/* Additional CSS code inserted by user */
#navigate {
position: absolute;
width: 100%;
background-color: grey;
border: 0.1px solid #000;
border-radius: 5px;
padding: 5px;
}
#images {
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="index.css" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous" />
<title>CoOL wEb</title>
</head>
<body>
<div class="container-fluid h-100">
<div class="row" id="bar">
<nav class="navbar navbar-expand-lg navbar-light" id="navigate">
<a class="navbar-brand">Photos</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="" id="pic1">Pic1</a>
</li>
<li class="nav-item">
<a class="nav-link" href="" id="pic2">Pic2</a>
</li>
<li class="nav-item">
<a class="nav-link" href="" id="pic3">Pic3</a>
</li>
</ul>
</div>
</nav>
</div>
<div class="row" id="images">
</div>
</div>
<script src="index.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>
I am looking for a solution where the images do not keep refreshing but are displayed properly. Any suggestions would be appreciated. Thank you!