简体   繁体   English

滚动 vuejs 上的导航栏不透明度

[英]Navbar opacity on scroll vuejs

I want to change the opacity of my fixed navbar background when user scrolling the page.我想在用户滚动页面时更改固定导航栏背景的不透明度。 Initially navbar background should be transparent and when scrolling down it background needs to be change to white.最初导航栏背景应该是透明的,向下滚动时背景需要更改为白色。 Like this example https://codepen.io/michaeldoyle/pen/Bhsif像这个例子https://codepen.io/michaeldoyle/pen/Bhsif

I have found various examples for this scenario using jquery.我已经使用 jquery 找到了针对此场景的各种示例。 But I need to achieve this using vuejs.但我需要使用 vuejs 来实现这一点。

$(window).scroll(function() {
  if ($(document).scrollTop() > 200) {
    $('nav').addClass('transparent');
  } else {
    $('nav').removeClass('transparent');
  }
}); 

I tried above code inside my vue page.我在我的 vue 页面中尝试了上面的代码。 I put it inside mounted().我把它放在mounted()里面。 But it doesn't work.但它不起作用。 I need to done this using vue.我需要使用 vue 完成此操作。 Not like above jquery.不像上面的jquery。

<nav class="navbar navbar-inverse">
      <ul class="nav menu">
<li>
  <router-link to="/about" @click.native="closeNavBarFromChild">About</router-link>
</li>
<li class="hidden-lg hidden-md">
  <router-link to="/product" @click.native="closeNavBarFromChild">Product</router-link>
</li>
</ul>
    </nav>

This is how my navbar component looks like.这就是我的导航栏组件的样子。

nav.navbar{
   -webkit-transition: all 0.4s ease;
   transition: all 0.8s ease;
}

.navbar-inverse {
    background-color: rgba(255,255,255,0);
}

nav.navbar.transparent {
  background-color:rgba(0,0,0,1);
}

this is the css part that i used.这是我使用的 css 部分。

Set your listener in created lifecycle hook:created生命周期钩子中设置你的监听器:

export default {
  created () {
    window.addEventListener('scroll', this.onScroll);
  },
  destroyed () {
    window.removeEventListener('scroll', this.onScroll);
  },
  methods: {
    onScroll (event) {
      // add/remove class
    }
  }
}

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

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