当前位置: 首页 > news >正文

【论术】 复盘-回顶按钮以及功能

项目的回顶按钮以及功能,顺滑滚动/当前处于第一屏内不展示

<template><div class="back-top" :class="{ visible: isVisible }"><a @click="scrollToTop" title="回到顶部"><SvgIcon iconClass="arrowTop" style="font-size: 22px; color: #fff" /></a></div>
</template><script>
import SvgIcon from "@/components/SvgIcon.vue";
export default {name: "BackToTop",props: {// 要监听的滚动元素IDscrollElementId: {type: String,required: true,},// 滚动阈值,超过此值显示按钮scrollThreshold: {type: Number,default: 50,},// 滚动动画持续时间(毫秒)scrollDuration: {type: Number,default: 500,},},components: {SvgIcon,},data() {return {isVisible: false,scrollElement: null,};},mounted() {// 获取滚动元素this.scrollElement = document.getElementById(this.scrollElementId);if (this.scrollElement) {// 添加滚动事件监听this.scrollElement.addEventListener("scroll", this.handleScroll);} else {console.warn(`BackToTop: 未找到ID为"${this.scrollElementId}"的元素`);}},beforeDestroy() {// 移除滚动事件监听if (this.scrollElement) {this.scrollElement.removeEventListener("scroll", this.handleScroll);}},methods: {handleScroll() {// 检查滚动位置this.isVisible = this.scrollElement.scrollTop > this.scrollThreshold;},scrollToTop() {// 平滑滚动到顶部const startPosition = this.scrollElement.scrollTop;const startTime = performance.now();const animateScroll = (currentTime) => {const elapsed = currentTime - startTime;const progress = Math.min(elapsed / this.scrollDuration, 1);// 使用缓动函数使滚动更自然const easeInOutCubic = (t) =>t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;this.scrollElement.scrollTop = startPosition * (1 - easeInOutCubic(progress));if (progress < 1) {requestAnimationFrame(animateScroll);}};requestAnimationFrame(animateScroll);},},
};
</script><style scoped>
.back-top {width: 46px;height: 46px;position: fixed;bottom: 60px;right: 14px;z-index: 1000;opacity: 0;transition: opacity 0.5s ease;pointer-events: none;
}.back-top.visible {opacity: 1;pointer-events: auto;
}.back-top a {display: block;width: 46px;height: 46px;background: #0052d9;text-align: center;height: 46px;border-radius: 50%;text-decoration: none;font-size: 28px;box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);transition: all 0.3s ease;cursor: pointer;
}/* 响应式设计 */
/* @media (max-width: 768px) {.back-top {bottom: 20px;right: 20px;}.back-top a {width: 46px;height: 46px;line-height: 46px;font-size: 24px;}
} */
</style>
父组件使用:
<BackUp scroll-element-id="homeRef" :scroll-duration="600" /> 

以上。

http://www.rkmt.cn/news/77507.html

相关文章:

  • 2025年湖南快手开户渠道权威推荐:湖南信息流开户/湖南朋友圈开户预定/湖南360开户承办商综合实力榜单
  • 2025年行业领先的 GEO 优化公司:权威榜单深度测评
  • 2025年高纯度乳酸菌原料源头工厂TOP5推荐:专业乳酸菌代
  • 2025年十大防盗门品牌客服服务排行榜,星月神防盗门厂家推荐
  • 第九届艺术、教育与管理国际学术会议(ICAEM 2026)
  • doc-llm-autotest 基于大模型的文档自动化测试平台::用户提交文件进行文档测试
  • 2025年GEO 优化公司成功案例有哪些?:最新TOP10深
  • OEM Parking Brake Caliper Actuator for Mercedes-Benz S-Class (2014-2020) - Direct Fit Reliability
  • 2025 十大高清免费版权图片素材下载网站推荐
  • 2025年性价比高的 GEO 优化公司有哪些?:十大权威测评
  • 中科大2022Hackergame--Xcaptcha思路及做法(含python代码)
  • 2025玻璃钢格栅生产企业TOP5权威推荐:耐腐蚀格栅选型指
  • 2025年中国高性价比护栏网品牌企业推荐:知名的护栏网公司有
  • 2025年12月尼龙齿套厂家优质企业推荐榜,耐高温尼龙齿套、齿式联轴器尼龙齿套、增强型尼龙齿套、聚焦服务品质与产品竞争力深度剖析
  • 2025年十大铱金火花塞供应商推荐,实力强的铱金火花塞源头工
  • 水管连接处密封胶:性能、价格与耐用性深度对比
  • 实用指南:JD京东线下HR面(准备)
  • Hall 定理
  • RS485在开断电发送乱码
  • 2025年靠谱的 GEO 服务商名单:十大测评精选必读
  • 2025年GEO 服务商服务内容有哪些?:权威榜单与攻略揭秘
  • 手写RPC框架
  • Java 8 函数式编程详解:从思想到实战
  • 2025年市场活动策划推荐及选择参考
  • Shell脚本实用技巧|运维必备的10个脚本
  • 2025年性价比高的 GEO 服务商有哪些?:权威精选测评推
  • 盘点2025年楼板搭建优质公司,口碑与实力并存,别墅现浇/现浇楼梯/现浇钢筋混凝土楼板/阁楼现浇/现浇搭建楼板搭建公司哪个好选哪家
  • 2025年铱金火花塞供应商推荐、火花塞源头工厂排名全解析
  • 2025年靠谱的打包机供应商推荐:五大实力强的纤维与油压打包
  • 哈尔滨律师事务所选哪家