vue实现导行吸顶功能

 

1.先绑定一个类名,用来固定的

 

 2.js求出他的高度

 

window.addEventListener('scroll', this.initHeight,true)
后面要加上true


 

3.还有一个类名

 

 

 

 

 

全文,直接运行肯定报错的,图片看不清楚用的

<template>
  <div class="nav-bar"
       :class="{'is_fixed':isFixed}">
    <div class="container">
      <div class="pro-title">
        {{title}}

      </div>
      <div class="pro-param">
        <a href="javascript:;">概述</a><span>|</span>
        <a href="javascript:;">参数</a><span>|</span>
        <a href="javascript:;">用户评价</a>
        <slot name="buy"></slot>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'NavBar',
  props: {
    title: String
  },
  data () {
    return {
      isFixed: false
    }
  },
  mounted () {
    window.addEventListener('scroll', this.initHeight,true)
  },
  methods: {
    initHeight () {
      // 各种浏览器的滑动偏移参数获取
      const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      this.isFixed = scrollTop > 152
    }
  },
  destroyed () {
    // 销毁,其他页面也会加载,浪费资源
    // true是捕获,false是冒泡
    window.removeEventListener('scroll', this.initHeight, false)
  }
}
</script>
<style lang="scss">
@import "./../assets/scss/config.scss";
@import "./../assets/scss/mixin.scss";
.nav-bar {
  height: 70px;
  line-height: 70px;
  border-top: 1px solid $colorH;
  background-color: $colorG;
  z-index: 10;
  // 是否固定定位
  &.is_fixed {
    position: fixed;
    top: 0;
    width: 100%;
    box-shadow: 0 5px 5px $colorE;
  }
  .container {
    @include flex();
    .pro-title {
      font-size: $fontH;
      color: $colorB;
      font-weight: bold;
    }
    .pro-param {
      font-size: $fontJ;
      span {
        margin: 0 10px;
      }
      a {
        color: $colorC;
      }
    }
  }
}
</style>

 

posted @ 2022-09-26 00:24  漫漫长路</>  阅读(202)  评论(0编辑  收藏  举报