zoukankan      html  css  js  c++  java
  • vue中的锚链接跳转问题

    一、在vue中的锚链接和普通的html不同

    关于vue中的锚链接可以参考vue 中的  scrollBehavior 滚动行为。

    在router.js中 

    const router = new VueRouter({
               routes,
          mode: 'history',
          scrollBehavior(to, from, savedPosition) {
            if (to.hash) {
              return {
                selector: to.hash
              }
            }
          }
    })
    export default router;

    在vue中  点击跳转的位置 使用<a>链接包起来

    <div>
        <a href="#populationInformation">人口画像</a>
    </div>
    <div>
        <a href="#peopleCounting">人流统计</a>
    </div>
    <div>
        <a href="#trafficAnalysis">交通分析</a>
    </div>

    在需要跳转到的位置

    <div id='populationInformation'> 人口画像跳转到此</div>
    <div id='peopleCounting'> 人流统计跳转到此 </div>
    <div id='trafficAnalysis'>交通分析跳转到此 </div>

    要保证<a>标签的 href 的地址要和下面id的值是相同的才可以完成相应的跳转,至于在router中的配置也是必须的。

    二、如何在vue中监听滚动事件呢

    例如滚动到一定程度的时候触发什么或者执行什么

    在mounted 中

    window.addEventListener('scroll', this.handleScroll)

    然后方法中

      mounted: function () {
        this.$nextTick(function () {
          window.addEventListener('scroll', this.onScroll)
        })
      },
      methods: {
        onScroll () {
          let scrolled = document.documentElement.scrollTop || document.body.scrollTop
        // 586、1063分别为第二个和第三个锚点对应的距离
          if (scrolled >= 1063) {
            this.steps.active = 2
          } else if (scrolled < 1063 && scrolled >= 586) {
            this.steps.active = 1
          } else {
            this.steps.active = 0
          }
        }
      }

     但是这种有一个问题就是,滚动特别突兀不像jq可以谁知滚动的动画。那么如何实现滚动的平滑呢,具体参考

  • 相关阅读:
    全面了解Cookie
    HTML5实现无刷新修改URL
    闭包的理解
    JS中的prototype、__proto__与constructor
    Array.prototype.slice.call()方法详解
    深入理解 Array.prototype.map()
    网页布局——Flex弹性框布局
    vue项目的各个文件作用
    node使用心得
    node连接Mysql报错ER_NOT_SUPPORTED_AUTH_MODE
  • 原文地址:https://www.cnblogs.com/haonanZhang/p/9376948.html
Copyright © 2011-2022 走看看