In the code below, the default main content is empty.
(在下面的代码中,默认主要内容为空。)
Unless I click on any of the bottom navbar buttons, no content will show up.(除非单击底部的导航栏按钮,否则不会显示任何内容。)
I'd like to set content-1 and menu-1 (its respective button) to be the default, ie when the user opens the webpage it would be the first thing they see and the button would be black indicating that it is active.
(我想将content-1和menu-1 (其各自的按钮)设置为默认值,即,当用户打开网页时,这将是他们看到的第一件事,并且该按钮将为黑色,表明它处于活动状态。)
I tried to use an else statement but it did not work:
(我尝试使用else语句,但是它不起作用:)
// set menu-1 as default
else {
$('.menu-1').addClass('default')
$('.content').addClass('default')
}
Find the entire code below:
(在下面找到完整的代码:)
$(document).ready(function() {
// only show menu-1
$('.menu-1').click(function() {
if ($('.menu-2, .menu-3').hasClass('active')) {
$('.menu-2, .menu-3').removeClass('active');
$('.content-2, .content-3').removeClass('active');
}
// set menu-1 as default
// else {
// $('.menu-1').addClass('default')
// $('.content').addClass('default')
// }
$('.menu-1').addClass('active');
$('.content-1').addClass('active');
});
// only show menu-2
$('.menu-2').click(function() {
if ($('.menu-1, .menu-3').hasClass('active')) {
$('.menu-1, .menu-3').removeClass('active');
$('.content-1, .content-3').removeClass('active');
}
$('.menu-2').addClass('active');
$('.content-2').addClass('active');
});
// only show menu-3
$('.menu-3').click(function() {
if ($('.menu-2, .menu-1').hasClass('active')) {
$('.menu-2, .menu-1').removeClass('active');
$('.content-2, .content-1').removeClass('active');
}
$('.menu-3').addClass('active');
$('.content-3').addClass('active');
});
});
.container {
margin: 0 auto;
background-color: #eee;
border: 1px solid lightgrey;
width: 20vw;
height: 90vh;
font-family: sans-serif;
position: relative;
}
header {
background-color: lightgreen;
padding: 5px;
text-align: center;
text-transform: uppercase;
}
.bottom-navbar {
position: absolute;
bottom: 0;
width: 100%;
padding: 6px 0;
overflow: hidden;
background-color: lightgreen;
border-top: 1px solid var(--color-grey-dark-3);
z-index: 50;
display: flex;
justify-content: space-between;
> a {
display: block;
color: green;
text-align: center;
text-decoration: none;
font-size: 20px;
padding: 0 10px;
&.active {
color: black;
}
}
}
.menu-1.default,
.menu-1.active,
.menu-2.active,
.menu-3.active {
color: black;
}
.content-1,
.content-2,
.content-3 {
display: none;
}
.content-1.default,
.content-1.active,
.content-2.active,
.content-3.active {
display: block;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
<div class="container">
<header>My header</header>
<div class="main-content">
<div class="content-1">House content</div>
<div class="content-2">Map content</div>
<div class="content-3">Explore content</div>
<div class="bottom-navbar">
<a href="#" class="menu-1"><i class="fa fa-home"></i></a>
<a href="#" class="menu-2"><i class="fa fa-map"></i></a>
<a href="#" class="menu-3"><i class="fa fa-search"></i></a>
</div>
</div>
In case you find it easier, here's my CodePen : https://codepen.io/fergos2/pen/vYYaRzN
(如果您觉得更简单,请使用我的CodePen : https : //codepen.io/fergos2/pen/vYYaRzN)
ask by Fergo translate from so