vue中根据搜索内容跳转到页面指定位置

<template>
  <div id="app">
    <header>
      <h3>搜索</h3><input type="text" v-model="rs"> <button @click="toTarget('#' + rs)">确定</button>
    </header>

    <div v-for="d in ds" :key="d" :id="d">
        {{d}}
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      rs: '',
      ds: ['home', 'team', 'contact', 'join']
    }
  },
  mounted () {
    this.$nextTick(function () {
      document.querySelector('#app').addEventListener('scroll', this.onScroll)
    })
    window.addEventListener('scroll', this.onScroll, true)
  },
  methods: {
    toTarget (target) {
      console.log(target)
      const toElement = document.querySelector(target) // 获取文档中 id="demo" 的元素:
      toElement.scrollIntoView(this.scrollIntoViewOptions)
    }
  },
  destroyed () {
    window.removeEventListener('scroll', this.onScroll, true)
  }
}
</script>

<style lang="scss" scoped>
  *{
    margin: 0;
    padding: 0;
  }
  #app {  //关键代码,需要给容器添加高度
    position: relative;
    width: 100%;
    height: 100vh;
  }
  header{
      position: fixed;
      z-index: 1;
      background: #fff;
      display: flex;
      /*display: -webkit-flex;*/
      /*justify-content: center;*/
  }
  /*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>

https://www.cnblogs.com/hxw6/p/11264989.html

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值