美文网首页
Vue导航条吸顶效果

Vue导航条吸顶效果

作者: 地主家也没余粮叻 | 来源:发表于2019-03-06 11:28 被阅读0次
需求

官网首页,导航条一开始隐藏,往下滑动鼠标,就出现导航栏(如不需要一开始隐藏,直接去掉class "clearfix" 即可 )

html
<header id="boxFixed" class="clearfix" :class="{'is_fixed' : isFixed}">
            <section class="header-box">
                <img src="./src/logo2.png" alt="">
                <nav>
                    <a>关于</a>
                    <a>产品功能</a>
                    <a>APP下载</a>
                    <a>免费注册使用</a>
                </nav>
            </section>
        </header>

 export default {
    data(){
      return {
        isFixed: false,
        offsetTop:0
      }
    },
    mounted(){
      window.addEventListener('scroll',this.initHeight);
      this.$nextTick( () => {
        this.offsetTop = document.querySelector('#boxFixed').offsetTop;
      })
    },
    methods:{
      initHeight () {
        var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
        this.isFixed = scrollTop > this.offsetTop ? true : false;
      },
    },
    destroyed () {
      window.removeEventListener('scroll', this.handleScroll)
    },
  }
style
.clearfix{
display:none;
}
.isFixed{
position:fixed;
top:0;
left:0;
z-index:2;}

相关文章

网友评论

      本文标题:Vue导航条吸顶效果

      本文链接:https://www.haomeiwen.com/subject/hdhtpqtx.html