<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

Logo

前往低代码交流专区

更多推荐