vue3一键回到顶部方法

引入组件文件

写一个文件放在components里面
在需要时进行调用它

但是呢 vue3当中怎么拿到呢 请往下看 ==============

content

这个文件是放在component的文件
!<template>
  <div>
    110
  </div>
</template>
<script lang='ts'>
import { reactive,toRefs,onBeforeMount,onMounted,} from 'vue'
import { useRouter,useRoute} from 'vue-router'//引入路由
export default {
    name: '',
      setup() {
          let router = useRouter(),route = useRoute();
          const data= reactive({

          })
          onBeforeMount(() => {
          })
          onMounted(() => {
          })
          const refData = toRefs(data);
          return {
              ...refData,
          }

      }
  };
</script>
<style scoped>
</style>

home

这个文件是调取组件的文件 
//这个与2不同 直接这样来写
  <back-top></back-top>

<script lang='ts'>
  components:{//这个要与setup()同级
    BackTop : defineAsyncComponent(() =>import('../components/conten.vue')),
  },
</script>

一键回到顶部方法

BackTop.vue

  • 在component创建文件 取名为BackTop.vue
<template>
  <transition :name="transitionName">
    <div v-show="visible" :style="customStyle" class="back-to-ceiling" @click="backToTop">
      <svg width="16" height="16" viewBox="0 0 17 17" xmlns="http://www.w3.org/2000/svg" class="Icon Icon--backToTopArrow" aria-hidden="true" style="height:16px;width:16px"><path d="M12.036 15.59a1 1 0 0 1-.997.995H5.032a.996.996 0 0 1-.997-.996V8.584H1.03c-1.1 0-1.36-.633-.578-1.416L7.33.29a1.003 1.003 0 0 1 1.412 0l6.878 6.88c.782.78.523 1.415-.58 1.415h-3.004v7.004z" /></svg>
    </div>
  </transition>
</template>

<script>
export default {
  name: 'BackTop',
  props: {
    visibilityHeight: {
      type: Number,
      default: 400
    },
    backPosition: {
      type: Number,
      default: 0
    },
    customStyle: {
      type: Object,
      default: function() {
        return {
          right: '50px',
          bottom: '50px',
          width: '40px',
          height: '40px',
          'border-radius': '4px',
          'line-height': '45px',
          background: '#e7eaf1'
        }
      }
    },
    transitionName: {
      type: String,
      default: 'fade'
    }
  },
  data() {
    return {
      visible: false,
      interval: null,
      isMoving: false
    }
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll)
  },
  beforeDestroy() {
    window.removeEventListener('scroll', this.handleScroll)
    if (this.interval) {
      clearInterval(this.interval)
    }
  },
  methods: {
    handleScroll() {
      this.visible = window.pageYOffset > this.visibilityHeight
    },
    backToTop() {
      if (this.isMoving) return
      const start = window.pageYOffset
      let i = 0
      this.isMoving = true
      this.interval = setInterval(() => {
        const next = Math.floor(this.easeInOutQuad(10 * i, start, -start, 500))
        if (next <= this.backPosition) {
          window.scrollTo(0, this.backPosition)
          clearInterval(this.interval)
          this.isMoving = false
        } else {
          window.scrollTo(0, next)
        }
        i++
      }, 16.7)
    },
    easeInOutQuad(t, b, c, d) {
      if ((t /= d / 2) < 1) return c / 2 * t * t + b
      return -c / 2 * (--t * (t - 2) - 1) + b
    }
  }
}
</script>

<style scoped>
.back-to-ceiling {
  position: fixed;
  display: inline-block;
  text-align: center;
  cursor: pointer;
}

.back-to-ceiling:hover {
  background: #d5dbe7;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity .5s;
}

.fade-enter,
.fade-leave-to {
  opacity: 0
}

.back-to-ceiling .Icon {
  fill: #9aaabf;
  background: none;
}
</style>

index.vue

<template>
 <div class="home">
    <ul>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <BackTop></BackTop>
    </ul>
  </div>
</template>
<script lang='ts'>
import { reactive,toRefs,onBeforeMount,onMounted,defineAsyncComponent} from 'vue'
import { useRouter,useRoute} from 'vue-router'//引入路由
export default {
    name: '',
      components:{//这个要与setup()同级
    BackTop : defineAsyncComponent(() =>import('../../components/goto.vue')),
  },
      setup() {
let router = useRouter(),route = useRoute();
          const data= reactive({

          })
          onBeforeMount(() => {
          })
          onMounted(() => {
          })
          const refData = toRefs(data);
          return {
              ...refData,
          }

      }
  };
</script>
<style scoped>
.home {
  height: 100vh;
  width: 100%;
}
ul {
  display: block;
  height: 3000px;
  background: yellow;
}
</style>

一键回顶部方法2

BackTop.vue

!<template>
  <div class="goto">
    <div v-if="btnFlag" class="goto" @click="backTop">
      <img src="https://g.csdnimg.cn/side-toolbar/3.0/images/fanhuidingbucopy.png" alt />
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      btnFlag: false
    };
  },
  methods: {
    // 点击图片回到顶部方法,加计时器是为了过渡顺滑
    backTop() {
      const that = this;
      let timer = setInterval(() => {
        let ispeed = Math.floor(-that.scrollTop / 5);
        document.documentElement.scrollTop = document.body.scrollTop =
          that.scrollTop + ispeed;
        if (that.scrollTop === 0) {
          clearInterval(timer);
        }
      }, 16);
    },
    // 为了计算距离顶部的高度,当高度大于60显示回顶部图标,小于60则隐藏
    scrollToTop() {
      const that = this;
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      that.scrollTop = scrollTop;
      if (that.scrollTop > 100) {
        that.btnFlag = true;
      } else {
        that.btnFlag = false;
      }
    }
  },
  // 初始化获取所有数据
  mounted() {
    window.addEventListener("scroll", this.scrollToTop);
  },
  destroyed() {
    window.removeEventListener("scroll", this.scrollToTop);
  }
};
</script>
<style lang="scss" scoped>
.main {
  width: 100%;
  height: 100vh;
  position: relative;
  .goto {
    position: fixed;
    right: 0;
    top: 80%;
    width: 60px;
    border-radius: 10%;
    text-align: center;
    background: gray;
  }
}
</style>

index.vue

<template>
 <div class="home">
    <ul>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <li>我是夏利</li>
      <BackTop></BackTop>
    </ul>
  </div>
</template>
<script lang='ts'>
import { reactive,toRefs,onBeforeMount,onMounted,defineAsyncComponent} from 'vue'
import { useRouter,useRoute} from 'vue-router'//引入路由
export default {
    name: '',
      components:{//这个要与setup()同级
    BackTop : defineAsyncComponent(() =>import('../../components/goto.vue')),
  },
      setup() {
let router = useRouter(),route = useRoute();
          const data= reactive({

          })
          onBeforeMount(() => {
          })
          onMounted(() => {
          })
          const refData = toRefs(data);
          return {
              ...refData,
          }

      }
  };
</script>
<style scoped>
.home {
  height: 100vh;
  width: 100%;
}
ul {
  display: block;
  height: 3000px;
  background: yellow;
}
</style>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
根据提供的引用内容,我们可以实现一个基于Vue一键回到顶部按钮,具体实现过程如下: 1. 在Vue组件中添加一个按钮元素,用于触发回到顶部事件。 2. 在Vue组件的data中添加一个变量isShowBackTop,用于控制回到顶部按钮的显示和隐藏。 3. 在Vue组件的methods中添加一个backTop方法,用于实现回到顶部的操作。在该方法中,我们可以使用window.scrollTo方法将页面滚动到顶部,并添加一个定时器,实现滚动的动画效果。 4. 在Vue组件的mounted生命周期函数中,添加一个滚动事件监听器,用于判断是否需要显示回到顶部按钮。当页面滚动高度大于屏幕高度时,将isShowBackTop变量设置为true,否则设置为false。 下面是一个示例代码: ```vue <template> <div> <!-- 回到顶部按钮 --> <div v-show="isShowBackTop" class="back-top" @click="backTop"> <i class="iconfont icon-top"></i> </div> <!-- 页面内容 --> <div class="content"> ... </div> </div> </template> <script> export default { data() { return { isShowBackTop: false // 控制回到顶部按钮的显示和隐藏 } }, methods: { backTop() { // 回到顶部操作 let currentScroll = document.documentElement.scrollTop || document.body.scrollTop if (currentScroll > 0) { window.requestAnimationFrame(this.backTop) window.scrollTo(0, currentScroll - (currentScroll / 5)) } } }, mounted() { // 滚动事件监听器 window.addEventListener('scroll', () => { let scrollTop = document.documentElement.scrollTop || document.body.scrollTop this.isShowBackTop = scrollTop > window.innerHeight }) } } </script> <style> .back-top { position: fixed; right: 50px; bottom: 50px; width: 50px; height: 50px; line-height: 50px; text-align: center; background-color: #ccc; border-radius: 50%; cursor: pointer; } .icon-top { font-size: 24px; } </style> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值