简体   繁体   English

如何使用 vue js 制作固定导航栏?

[英]How to make Fixed navbar with vue js?

enter image description here在此处输入图片说明

I try build a landing page using vue.js, with header design like on the picture above.我尝试使用 vue.js 构建一个登陆页面,标题设计如上图所示。

So, I create a component called "header",with contain content according to the design.所以,我创建了一个名为“header”的组件,根据设计包含内容。

How do I make a fixed navbar, when the page is scrolled the navbar is still on top ?如何制作固定的导航栏,当页面滚动时导航栏仍在顶部?

Another option could be to use the bootstrap-vue package.另一种选择是使用bootstrap-vue包。

It has the b-navbar component which can be made fixed to the top它有b-navbar组件,可以固定在 顶部

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

You can set a fixed navbar by applying the following class.您可以通过应用以下类来设置固定导航栏。

.header {
  position:fixed; /* fixing the position takes it out of html flow - knows
                   nothing about where to locate itself except by browser
                   coordinates */
  left:0;           /* top left corner should start at leftmost spot */
  top:0;            /* top left corner should start at topmost spot */
  width:100vw;      /* take up the full browser width */
  z-index:200;  /* high z index so other content scrolls underneath */
  height:100px;     /* define height for content */
}

An element with position:fixed;具有position:fixed;的元素position:fixed; property doesn't change when the window is scrolled, so a fixed positioned element will stay right.滚动窗口时属性不会改变,因此固定定位的元素将保持正确。

 new Vue({ el: "#app", data:{ active: false }, methods: { toggleNavClass(){ if(this.active == false){ return 'nav' } else { return 'sticky-nav' } } }, mounted(){ window.document.onscroll = () => { let navBar = document.getElementById('nav'); if(window.scrollY > navBar.offsetTop){ this.active = true; } else { this.active = false; } } } }) /*scrollY returns the scroll amount in pixels. offsetTop is the px difference between the navBar and closest parent element*/
 body { margin: 0; box-sizing: border-box; } #app { color: #2c3e50; background-color: #ccd6dd; height: 120vh; } a { font-weight: bold; color: white; text-decoration: none; margin: 0 1vw; } a:hover { transition: linear 100ms; color: red; } /* two classes, decided on scroll */ .nav { transition: 100ms; padding: 25px; } .sticky-nav{ transition: 100ms; padding: 20px; } #nav { display: flex; justify-content: space-between; width: 100%; background-color: #55acee; position: fixed; top: 0; } /* have to add the ID nav (#nav) otherwise the backgrnd color won't change as the previous background color is set in an ID and ID trumps class notation */ #nav.sticky{ transition: 150ms; box-shadow: 0px 15px 10px -15px #111; background-color: #ccd6dd; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div id="app"> <div id="nav" :class="{sticky:active}"> <div id="nav-container" :class="toggleNavClass()"><a href="#">Menu</a> </div> <router-view /> </div>

I just built a site using Vue.我刚刚使用 Vue 构建了一个网站。 This is my code这是我的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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