I would like to hide individual element blocks that are identified by id`s.
I have used a jquery function to do this.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name=”apple-mobile-web-app-capable” content=”yes “>
<script type="text/javascript" src="http://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
function changeMenu(menu){
$('#Slide_home').css({'display':'none'});
$('#Slide_sub01').css({'display':'none'});
$('#Slide_sub02').css({'display':'none'});
$('#Slide_'+menu).css({'display':'block'});
}
</script>
<style>
body {
margin: 0px;
padding: 0px;
background-color: rgb(53, 61, 64);
color: rgb(255, 255, 255);
height: 100%;
overflow-x: hidden;
}
#Menu {
position: fixed;
bottom: 0px;
width: 100%;
height: 100px;
background-color: rgb(0, 0, 0);
}
.ButtonWrapper {
display: flex;
flex-direction: row;
justify-content: center;
text-align: center;
}
.ButtonWrapper > div {
margin-left: 50px;
margin-top: 10px;
}
</style>
</head>
<body>
<div id="Slide_home">
home
</div>
<div id="Slide_sub01">
01
</div>
<div id="Slide_sub02">
02
</div>
<div id="Menu">
<div class="ButtonWrapper">
<div>
<img class="" style="cursor:pointer" onClick="changeMenu('home');" src="01.png" alt="home"/>
</div>
<div>
<img class="" style="cursor:pointer" onClick="changeMenu('sub01');" src="02.png" alt="sub_01"/>
</div>
<div>
<img class="" style="cursor:pointer" onClick="changeMenu('sub02');" src="03.png" alt="sub_02"/>
</div>
</div>
</div>
</body>
</html>
In principle the function works, when I press a button the corresponding other elements are hidden. But at the beginning all elements are visible. How can I display only the elements for home at the beginning?
Best regards
Jens
2
Answers
Add the following statements to your CSS:
Wrap the slides into a div and hide with CSS, add
active
class to the first slide and display this class as a block:You can add the
active
class with JS:Here you can see that we can easily mark active state for the buttons too.
Rewrite your show/hide code so you fully use jQuery event delegation. That way you don’t need any element IDs and can add as many slides as you want: