• vue-cli项目实现动态锚点定位


    <template>
      <div id="app">
        <header>
          <a href="javascript:void(0);" :class="active == '#home' ? 'active' : ''" @click="toTarget('#home')">首页</a>
          <a href="javascript:void(0);" :class="active == '#team' ? 'active' : ''"  @click="toTarget('#team')">团队</a>
          <a href="javascript:void(0);" :class="active == '#contact' ? 'active' : ''"  @click="toTarget('#contact')">联系</a>
          <a href="javascript:void(0);" :class="active == '#join' ? 'active' : ''"  @click="toTarget('#join')">加入我们</a>
        </header>
        <div id="home">
          首页
        </div>
        <div id="team">
          团队
        </div>
        <div id="contact">
          联系
        </div>
        <div id="join">
          加入我们
        </div>
      </div>
    </template>
    <script>
      export default {
        data() {
          return {
            active: '#home',
            scrollIntoViewOptions: {
              block: 'start',
              behavior: 'smooth'
            }
          }
        },
        mounted() {
          // 一次性计算赋值,减少滚动计算节点位置次数
          this.distance_team = document.querySelector('#team').offsetTop - 60
          this.distance_contact = document.querySelector('#contact').offsetTop - 60
          this.distance_join = document.querySelector('#join').offsetTop - 60
          this.$nextTick(function() {
            document.querySelector('#app').addEventListener('scroll', this.onScroll)
          })
          window.addEventListener('scroll', this.onScroll, true)
        },
        methods: {
          toTarget(target) {
            console.log(target)
            this.active = target
            const toElement = document.querySelector(target)
            toElement.scrollIntoView(this.scrollIntoViewOptions)
          },
          onScroll() {
            const scrolled = document.querySelector('.app-main').scrollTop
            if (scrolled < this.distance_team) {
              this.active = '#home'
            } else if (scrolled >= this.distance_team && scrolled < this.distance_contact) {
              this.active = '#team'
            } else if (scrolled >= this.distance_contact && scrolled < this.distance_join) {
              this.active = '#contact'
            } else {
              this.active = '#join'
            }
          }
        },
        destroyed() {
          window.removeEventListener('scroll', this.onScroll, true)
        }
      }
    </script>
    
    <style lang="scss" scoped>
      *{
        margin: 0;
        padding: 0;
      }
      #app {  //关键代码,需要给容器添加高度
        position: relative;
        width: 100%;
        height: 100vh;
      }
      header{
        width: 100%;
        height: 60px;
        position: fixed;
        top: 0px;
        left: 0px;
        z-index: 1;
        background: #fff;
        display: flex;
        display: -webkit-flex;
        justify-content: center;
        a{
          width: 200px;
          height: 60px;
          display: block;
          text-align: center;
          line-height: 60px;
          color: #333333;
          text-decoration: none;
          &:hover,&.active{
            color: dodgerblue;
          }
        }
      }
      #home,#team,#contact,#join{
        width: 100%;
        height: 500px;
        color: #FFFF;
        font-size: 30px;
        text-align: center;
        line-height: 500px;
      }
      #home{
        background: tomato;
      }
      #team{
        background: deepskyblue;
      }
      #contact{
        background: orange;
      }
      #join {
        height: 1000px;
        background: lightgreen;
      }
    </style>
  • 相关阅读:
    java BufferedImage 合成多张图片
    JQ 输入框控制输入
    SpringBoot读取静态资源文件
    Java获取永久图文素材中的网页端Url
    生成随机32位Token43位asekey
    SQL语句模糊查询年月
    go语言学习笔记3----流程控制
    go语言学习笔记2----变量、常量
    go语言学习笔记1----数据类型
    kettle抽取数据判断是否有数据
  • 原文地址:https://www.cnblogs.com/hxw6/p/11264989.html
Copyright © 2020-2023  润新知