How to make Fixed navbar with vue js?

Yudi Krisnandi picture Yudi Krisnandi · Nov 16, 2018 · Viewed 17.7k times · Source

enter image description here

I try build a landing page using vue.js, with header design like on the picture above.

So, I create a component called "header",with contain content according to the design.

How do I make a fixed navbar, when the page is scrolled the navbar is still on top ?

Answer

rotsendroid picture rotsendroid · Nov 16, 2018

Another option could be to use the bootstrap-vue package.

It has the b-navbar component which can be made fixed to the top

<b-navbar class="header" fixed="top"></b-navbar>

Example:

const vm = new Vue({el: '#app'})
<link href="http://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.css" rel="stylesheet"/><link href="http://unpkg.com/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet"/><script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script><script src="http://unpkg.com/bootstrap-vue@latest/dist/bootstrap-vue.js"></script><script src="http://unpkg.com/babel-polyfill@latest/dist/polyfill.min.js"></script><div id="app">

  <!-- ************************************ -->
  <!-- Place the fixed prop within b-navbar -->
  <!-- ************************************ -->
  
  <b-navbar class="header" type="dark" variant="info" fixed="top">
    <b-navbar-brand href="#"> My fixed header </b-navbar-brand>
  </b-navbar>
  
  <!-- *********************************** -->
  <div style="margin-top: 60px;"><ol><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li><li>link</li></ol></div></div>