How do I get the logo to disappear when I scroll the page?

3

I am creating a menu with version 4.1.3 of Bootstrap and would like to know how can I make to scroll the page to disappear? In this case, only the menu should stay.

  

Note: When returning to initial position the logo should reappear.

The menu is already fixed

<nav class="navbar navbar-expand-lg navbar-dark bg-primary">
  <a class="navbar-brand" href="#">
    <img id="brand-image" alt="Website Logo" src="img/logo.png" />
  </a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
    <div class="navbar-nav">
      <a class="nav-item nav-link active" href="#">Home <span class="sr-only">(current)</span></a>
      <a class="nav-item nav-link" href="#">Features</a>
      <a class="nav-item nav-link" href="#">Pricing</a>
      <a class="nav-item nav-link disabled" href="#">Disabled</a>
    </div>
  </div>
</nav>

CSS

#brand-image {
  height: 100px;
  float: center;
}

.navbar {
  position: fixed;
  padding-top: 15px;
  top: 0;
  z-index: 2;
  width: 100%;
}

.navbar-brand {
  position: absolute;
  left: 50%;
  margin-left: -50px;
  display: block;
  margin-top: 18px;
}
    
asked by anonymous 21.09.2018 / 15:56

3 answers

3

This response is just a complement to the @hugocsl response .

When it comes to events like scroll , resize or mousemove it is always important to remember that they are launched several times per second while the user interacts with the page. That is, depending on what you do within the handler function of the event, you can slow your page (eg accessing the DOM).

If there is no need to run this function so repeatedly you can use the techniques debounce and throttle , which serve to limit the execution of a function over a period of time.

debounce will wait for the event to stop. to be released for X seconds before activating the handler function.

throttle will only allow the function handler runs every X seconds.

Example:

// Só vai executar quando o usuário para de digitar por 1 segundo
$elemento.on('keyup', _.debounce(function() {
    console.log('debounce');
}, 1000));


// Enquanto o usuário seguir digitando, vai executar a cada segundo
$elemento.on('keyup', _.throttle(function() {
    console.log('throttle');
}, 1000));

Another, much simpler, optimization is to save the DOM element to a variable for later use, since access to the DOM is costly in terms of performance.

Instead of:

$(window).scroll(function() {
    // é executado cada vez que 'scroll' é lançado
    $("#brand-image").fadeOut();
});

It's more performative to use:

// É executado apenas uma vez
var $brandImage = $("#brand-image");

$(window).scroll(function() {
    // Usa a refêrencia salva anteriormente
    $brandImage.fadeOut();
});

And the last and least shocking is that when you do:

$(window).scrollTop()

You are converting a DOMElement to a jQuery object, it is not as costly as accessing the DOM, but it can still be easily replaced by the window.pageYOffset " which is native and compatibility is acceptable (IE 9+).

Below is the @hugocsl code with the normal handler handler and throttle to illustrate the importance of these techniques.

PS: In the examples I'm using the methods _.debounce() and _.throttle() of the library lodash .

Normal

var $brandImage = $("#brand-image");
var $scrollHelper = $("#scroll-helper");
var count_scroll = 0;

$(window).scroll(function() {
  $scrollHelper.html("scroll triggered: " + ++count_scroll);
  if (this.pageYOffset >= 140) { // 140 é a distancia que vc rola antes da logo sumir
    $brandImage.fadeOut();
  } else {
    $brandImage.fadeIn();
  }
});
#scroll-helper {
  position: fixed;
  top: 80px;
  left: 10px;
}
<link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>

<nav class="navbar navbar-expand-lg navbar-dark bg-primary sticky-top">
  <a class="navbar-brand" href="#">
    <img id="brand-image"src="https://placecage.com/50/50"/></a><buttonclass="navbar-toggler">
    <span class="navbar-toggler-icon"></span>
  </button>
</nav>

<div style="height:1000px; width: 100ps;"></div>
<span id="scroll-helper">scroll triggered: 0</span>

Debounce

var $brandImage = $("#brand-image");
var $scrollHelper = $("#scroll-helper");
var count_scroll = 0;

$(window).scroll(_.debounce(function() {
  $scrollHelper.html("scroll triggered: " + ++count_scroll);
  if (this.pageYOffset >= 140) { // 140 é a distancia que vc rola antes da logo sumir
    $brandImage.fadeOut();
  } else {
    $brandImage.fadeIn();
  }
}, 300));
#scroll-helper {
  position: fixed;
  top: 80px;
  left: 10px;
}
<link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="https://unpkg.com/[email protected]/lodash.min.js"></script><navclass="navbar navbar-expand-lg navbar-dark bg-primary sticky-top">
  <a class="navbar-brand" href="#">
    <img id="brand-image"src="https://placecage.com/50/50"/></a><buttonclass="navbar-toggler">
    <span class="navbar-toggler-icon"></span>
  </button>
</nav>

<div style="height:1000px; width: 100ps;"></div>
<span id="scroll-helper">scroll triggered: 0</span>

Throttle

var $brandImage = $("#brand-image");
var $scrollHelper = $("#scroll-helper");
var count_scroll = 0;

$(window).scroll(_.throttle(function() {
  $scrollHelper.html("scroll triggered: " + ++count_scroll);
  if (this.pageYOffset >= 140) { // 140 é a distancia que vc rola antes da logo sumir
    $brandImage.fadeOut();
  } else {
    $brandImage.fadeIn();
  }
}, 300));
#scroll-helper {
  position: fixed;
  top: 80px;
  left: 10px;
}
<link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="https://unpkg.com/[email protected]/lodash.min.js"></script><navclass="navbar navbar-expand-lg navbar-dark bg-primary sticky-top">
  <a class="navbar-brand" href="#">
    <img id="brand-image"src="https://placecage.com/50/50"/></a><buttonclass="navbar-toggler">
    <span class="navbar-toggler-icon"></span>
  </button>
</nav>

<div style="height:1000px; width: 100ps;"></div>
<span id="scroll-helper">scroll triggered: 0</span>
    
21.09.2018 / 20:15
4

First for this effect to be valid it would be interesting to have your menu always fixed at the top of the page. For this you need to use the .fixed-top class in navbar (see official documentation a>).

As a simple jQuery (jQuery is already part of Bootstrap so it's okay to use it) it is possible to do a fadeOut and fadeIn in the scroll event in>.

To see the result see the example below. I have commented the place where you control the "distance" of scroll before the logo disappears.

$(window).scroll(function() {
  var scroll = $(window).scrollTop();
  if (scroll >= 140) { // 140 é a distancia que vc rola antes da logo sumir
    $("#brand-image").fadeOut();
  } else {
    $("#brand-image").fadeIn();
  }
});
<link rel="stylesheet" type="text/css" media="screen" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" />
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>

<nav class="navbar navbar-expand-lg navbar-dark bg-primary sticky-top">
  <a class="navbar-brand" href="#">
    <img id="brand-image" alt="Website Logo" src="https://placecage.com/50/50"/></a><buttonclass="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
    <div class="navbar-nav">
      <a class="nav-item nav-link active" href="#">Home <span class="sr-only">(current)</span></a>
      <a class="nav-item nav-link" href="#">Features</a>
      <a class="nav-item nav-link" href="#">Pricing</a>
      <a class="nav-item nav-link disabled" href="#">Disabled</a>
    </div>
  </div>
</nav>

<div style="height:1000px; width: 100ps;"></div>
    
21.09.2018 / 16:25
2

1: You have not reported which version of bootstrap you are using.

2: You did not notice if you were able to leave the menu fixed.

In the absence of this information, I made an example using the 4.1 version of bootstrap .

  

Note that I added the class .sticky-top in the navbar so that it stays fixed. Note that this class uses a sticky position, which is not yet supported in IE 11. To access the full list of browsers that support, see here . If you prefer, use the class fixed-top .

Basically, you will need to escultando the scroll event and check when it reaches a certain value you set, then hide and show the image as needed.

The variable length was declared for you to test with other values.

I recommend taking the comment from console.log() to see how scroll works.

$(document).on('scroll', function() {
  let scroll = $(document).scrollTop();
  let length = 100;
  //console.log('scroll: '+ scroll);

  if (scroll > length) {
    $('#brand-image').hide();
  } else {
    $('#brand-image').show();
  }
});
.main {
  height: 1000px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<nav class="navbar navbar-expand-lg sticky-top navbar-dark bg-primary">
  <a class="navbar-brand" href="#">
    <img id="brand-image" alt="Website Logo" src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSgZ_2jtGVpp0G-t966PJgbdGVgJ71RFXH7_5syQEl3WNaPBUmr"/></a><buttonclass="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
    <div class="navbar-nav">
      <a class="nav-item nav-link active" href="#">Home <span class="sr-only">(current)</span></a>
      <a class="nav-item nav-link" href="#">Features</a>
      <a class="nav-item nav-link" href="#">Pricing</a>
      <a class="nav-item nav-link disabled" href="#">Disabled</a>
    </div>
  </div>
</nav>
<div class="main">
</div>
<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://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
    
21.09.2018 / 16:30