当前位置: 首页 > 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.jsqmd.com/news/66968/

相关文章:

  • AOI光学检测设备厂家哪家好?实力品牌大盘点
  • 2025地磅售后完善厂家TOP5权威推荐:正规供应商甄选指南
  • 2025年湖南快手开户渠道权威推荐:湖南信息流开户/湖南朋友圈开户预定/湖南360开户承办商综合实力榜单
  • 2025年行业领先的 GEO 优化公司:权威榜单深度测评
  • 国内发展较快的AI公司有哪些?行业创新力量盘点
  • 靠谱的AI公司有哪些?聚焦技术实力与服务能力的选择
  • 2025年护栏网源头厂家推荐,隔离护栏网机构推荐全解析
  • 2025年高纯度乳酸菌原料源头工厂TOP5推荐:专业乳酸菌代
  • 2025年松木桩6米供货厂家权威推荐榜单:益阳打桩松木‌/松木桩4米‌/石家庄木桩‌‌源头厂家精选
  • 2025年GEO 优化公司哪家性价比高?:十大精选报告揭秘
  • 2025年十大防盗门品牌客服服务排行榜,星月神防盗门厂家推荐
  • 2025年重庆十大板栗鸡店推荐:本地人去的板栗鸡餐厅有哪些?
  • doc-llm-autotest 基于大模型的文档自动化测试平台::用户提交文件进行文档测试
  • 第九届艺术、教育与管理国际学术会议(ICAEM 2026)
  • 详细介绍:指纹浏览器字体模拟实践
  • Adobe Stock 中国合作伙伴推荐:卓特视觉官网及代理服务详解
  • 2025年GEO 优化公司优化效果怎么样?:官方TOP10报
  • 2025年GEO 优化公司成功案例有哪些?:最新TOP10深
  • 案例深析 | 兰亭妙微如何助力“智选家居”实现数字化转型,线上销售额激增300%
  • 2025年上海资深离婚律师排名:著名离婚律师收费标准全解析
  • OEM Parking Brake Caliper Actuator for Mercedes-Benz S-Class (2014-2020) - Direct Fit Reliability
  • 2025年市场洞察公司年度排名:市场洞察哪家公司好
  • 2025年市面上知名的CTU货架批发厂家口碑推荐,轻型货架/贯通货架/仓库货架/钢制货架/模具架/CTU货架批发厂家推荐榜单
  • 2025年GEO 优化公司收费标准是什么?:最新深度解析精选
  • DASCTF 2025下半年赛 OnePanda战队WP
  • 2025 十大高清免费版权图片素材下载网站推荐
  • 2025年性价比高的 GEO 优化公司有哪些?:十大权威测评
  • 2025年杭州高德团购服务商排名:高德团购服务商家服务哪家便
  • 中科大2022Hackergame--Xcaptcha思路及做法(含python代码)
  • 2025玻璃钢格栅生产企业TOP5权威推荐:耐腐蚀格栅选型指