How to create a scrolling menu?

Asked

Viewed 1,936 times

4

As is done this fixed menu at the top of the page that when the user scrolls down the screen when arriving at a particular page location the menu decreases getting compact. And by turning the screen up the menu goes back to the initial styling? This can be done with the logo too?

Example taken from the Website: https://builtwith.com/

inserir a descrição da imagem aqui

To fix the menu I’m using

position: fixed;

However, I don’t know how this effect is being realized whether it is Javascript or css.

Using the information obtained here i manage to make the menu bar have an animation, however the animation only applies to the menus, but that’s not quite what I want and what the answers below proposed. I want to have this "animation" effect on my logo as well. Could someone help me?

$(window).scroll(function() {
  if ($(document).scrollTop() > 50) {
    $('nav').addClass('shrink');
  } else {
    $('nav').removeClass('shrink');
  }
});
body {
  padding-top: 50px;
}

nav a {
  padding-top: 20px !important;
  padding-bottom: 20px !important;
  font-size: 18px;
}

nav .navbar-toggle {
  margin: 13px 15px 13px 0;
}

.navbar-brand {
  font-size: 30px;
  -webkit-transition: all 0.3s;
  -moz-transition: all 0.3s;
  -ms-transition: all 0.3s;
  -o-transition: all 0.3s;
  transition: all 0.3s;
}


}
nav.navbar.shrink {
  min-height: 35px;
}
nav.shrink a {
  padding-top: 10px !important;
  padding-bottom: 10px !important;
  font-size: 15px;
}
nav.shrink .navbar-brand {
  font-size: 25px;
}
nav.shrink .navbar-toggle {
  padding: 4px 5px;
  margin: 8px 15px 8px 0;
}
*Position logo*/ .navbar-brand img {
  padding: 10px 0;
}
<nav class="navbar navbar-inverse navbar-fixed-top">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#"><img src="https://d2z0lf9itclnw8.cloudfront.net/img/logo/[email protected]" width="250px" class="img-responsive" alt="">

    </div>
    <div class="collapse navbar-collapse">
      <ul class="nav navbar-nav pull-right">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="#about">About</a></li>
        <li><a href="#contact">Contact</a></li>
      </ul>
    </div>
    <!--/.nav-collapse -->
  </div>
</nav>

div class="jumbotron">
<div class="container">
  <h1>Hello, world!</h1>
  <p>This is a template for a simple marketing or informational website. It includes a large callout called a jumbotron and three supporting pieces of content. Use it as a starting point to create something more unique.</p>
  <p><a class="btn btn-primary btn-lg" href="#" role="button">Learn more &raquo;</a></p>
</div>
</div>

<div class="container">
  <!-- Example row of columns -->
  <div class="row">
    <div class="col-md-4">
      <h2>Heading</h2>
      <p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
      <p><a class="btn btn-default" href="#" role="button">View details &raquo;</a></p>
    </div>
    <div class="col-md-4">
      <h2>Heading</h2>
      <p>Donec id elit non mi porta gravida at eget metus. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Etiam porta sem malesuada magna mollis euismod. Donec sed odio dui. </p>
      <p><a class="btn btn-default" href="#" role="button">View details &raquo;</a></p>
    </div>
    <div class="col-md-4">
      <h2>Heading</h2>
      <p>Donec sed odio dui. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus.</p>
      <p><a class="btn btn-default" href="#" role="button">View details &raquo;</a></p>
    </div>
  </div>
  <!-- /.container -->

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

  • To do this effect, we use Javascript and CSS. This example I think is what you want: Header Resize.

  • I’m looking here Douglas. I did several searches on google, however I did not know the name to produce this effect so I had to come here to ask.

  • This is exactly what Douglas was looking for if he can formulate his answer I’ll give an upvote.

  • Hello friend see on the link the option in practice with bootstrap of the effect you want to hopefully aggregate to formulate your project ! Bootstrap method

  • @Rblima This example is basically what my above CSS and Javascript code does, but I haven’t gotten the desired result so far.

1 answer

4


The way I usually do it is to apply a class to the menu after scrolling. From the class you just style your elements the way you want. In this example that I am sending, I only encouraged the max-height from the "menu" which in this case is only a bar. But I believe it is quite clear what has been done.

$(function() {
  //caches a jQuery object containing the header element
  var header = $(".menu");
  $(window).scroll(function() {
    var scroll = $(window).scrollTop();

    if (scroll >= 500) {
      header.addClass("menuScroll");
    } else {
      header.removeClass("menuScroll");
    }
  });
});
body {
  margin: 0;
  height: 2000px;
}

.menu {
  position: fixed;
  width: 100%;
  height: 100px;
  max-height: 100px;
  background: black;
  -webkit-transition: max-height 0.2s ease-in-out;
  -moz-transition: max-height 0.2s ease-in-out;
  -o-transition: max-height 0.2s ease-in-out;
  transition: max-height 0.2s ease-in-out;
}

.menu.menuScroll {
  max-height: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="menu">

</div>

  • I understood how it works @Leon Freire even implemented in my layout using Bootstrap, but I’m having trouble with the logo. added . img-Responsive to have the effect too, but did not get the expected result.

  • @Renatolazaro In this case I would need to see your CSS to see what is happening.

  • @Douglas Garrido Caro updated my question with what I get so far.

  • Dear updated my question with what I get so far.

  • @Renatolazaro Just apply a size to the logo inside your menu after the reduction. Try something like .shrink img.img-responsive { width: 100px; }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.