简体   繁体   中英

Setting default content with jQuery

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.

I tried to use an else statement but it did not work:

    // 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

All that is going on to set up each menu and content item to display on the page is adding the class active . So it looks to me like all you need to do is add that class to the HTML. That way when the page loads it's already "active" and when you click something else you already have it set up to remove the class and set something else as active. So basically, your HTML would look like this:

  <header>My header</header>
  <div class="main-content">
    <div class="content-1 active">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 active"></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>

All I did was give .menu-1 and .content-1 the class of active .

You'll also need to get rid of the css bit which references .content-1.default and .menu-1.default and also set your JS to add the .active back when you click that menu button which you already have. Don't worry about the else statement inside that click function

Let me know if this works out for you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM