js 动画滚动到指定位置 ES6
### 开始 ###
写一个自动滚动过度到指定位置的一个函数 通过Class进行封装
/** * 滚动动画过度 * @param {Object} position 定位(只支持Y轴) * @param {Number} delay 单位毫秒 default 200 * @param {Number} speed 单位毫秒 default 10 * 误差:滚动距离越短误差越小 */ export class AnimationScrollTop { constructor (position, delay = 200, speed = 10) { this.position = position this.delay = delay this.speed = speed this.step = this.delay / this.speed this.dimension = this.position.y / this.step this.thisTop = window.pageYOffset this.delayt = this.thisTop this.upOrDown = this.thisTop > this.position.y this.delays = null // 初始化 this.init() } init () { this.delays = setInterval(() => { if (!this.upOrDown) { if (this.delayt >= this.position.y) { clearInterval(this.delays) } this.delayt += this.dimension } else { if (this.delayt <= this.position.y) { clearInterval(this.delays) } this.delayt -= this.dimension } window.scrollTo(this.position.x, this.delayt) }, this.speed) } }使用方式
new AnimationScrollTop({x: 0, y: 500})
### END ###
分类:
other
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器