zoukankan      html  css  js  c++  java
  • 路由跳转中的hash值和锚点冲突

    dealLink() {        //处理a标签情况,1、跳转到别的站点  2、当前页面锚点   3、不是站点又不是锚点,处理成文本形式
          let element = document.querySelectorAll('a')
          element.forEach((item, index) => {
            const href = item.getAttribute('href'),    //获取标签的href属性
              isInclude = href.includes('http'),      
              isHash = item.hash,
              noClick = !isInclude && !isHash         //特殊情况,不是站点又不是锚点的处理情况
            if (noClick) {
              item.style.color = 'rgba(0, 0, 0, 0.65)'
            }
            element[index].addEventListener('click', e => {
              this.stopDefault(e)
              if (noClick) {
                return false
              }
              if (isInclude) {
                window.open(href, '_blank')
              } else {
                this.anchorScroll(href)
              }
            })
          })
        },
        stopDefault(e) {
          if (e && e.preventDefault) {
            e.preventDefault()
          } else {
            window.event.returnValue = false
          }
        },
        anchorScroll(anchorName) {
          document.querySelector(anchorName).scrollIntoView(true)    //当hash值和锚点冲突之后,手动拿到锚点的位置进行滑动
        },
  • 相关阅读:
    23种设计模式之责任链模式
    23种设计模式之中介者模式
    23种设计模式之代理模式
    23种设计模式之原型模式
    23种设计模式之模板方法模式
    23种设计模式之建造者模式
    23中设计模式之抽象工厂模式
    批处理产生001到999之间的数字
    批处理随机显示星期几
    批处理简易密码登录
  • 原文地址:https://www.cnblogs.com/MJmajong/p/13235934.html
Copyright © 2011-2022 走看看