仿gsap滑动阻尼ScrollSmoother效果

做项目的时候想要用一下gsap的ScrollSmoother,结果发现收费,想着自己去搞一下ScrollSmoother然后自己用,搜到了一期教程

z​​​​【前端 | 教程】纯干货!三步实现滑动阻尼效果_哔哩哔哩_bilibili

 官网

自己实现

一开始按照教程添加缓动之后发现效果不佳,应该是缓动函数不一样,就去收集了一下gsap网页的滑动时变化的translate数据

const element = document.getElementById("smooth-content");
let start, previousTimeStamp;
let done = false;
let res = [];
function step(timestamp) {
  let temp = { during:0,transform:0 };
  if (start === undefined) {
    start = timestamp;
  }
  const elapsed = timestamp - start;

  if (previousTimeStamp !== timestamp) {
    temp.during = elapsed;
    let str = element.style.transform;
    temp.transform = parseInt(str.split(",")[13]) 
    res.push(temp);
  }

  if (elapsed < 3000) {
    // 3 秒之后停止动画
    previousTimeStamp = timestamp;
    if (!done) {
      window.requestAnimationFrame(step);
    }
  }
  else console.log(res)
}

window.addEventListener("scroll", () => {
  window.requestAnimationFrame(step);
});

把结果放到ecahrts中可视化一下

发现cubic-bezier函数是长这样的,然后通过chrome调试器调一下cubic-bezier就出来了

代码如下

<script setup>
import { nextTick, onMounted, ref } from 'vue';

let scrollbox
const resizeBody= ()=>{
  scrollbox = document.getElementsByClassName('my-scroll-bar')[0]
  document.body.style.height = `${scrollbox.offsetHeight}px`
}
const scroll = ()=>{
  scrollbox.style.transform = `translateY(${-scrollY}px)`
}
onMounted(()=>{
  resizeBody()
  window.addEventListener('load',resizeBody)
  window.addEventListener('resize',resizeBody)
  window.addEventListener('scroll',scroll)
})
</script>

<template>
    <div class="my-scroll-bar">
      <RouterView />
    </div>
</template>

<style>
.my-scroll-bar{
  overflow: hidden;
  transition: transform 2.4s cubic-bezier(0.1, 0.79, 0.11, 0.82);
}
#app{
  position: fixed;
  top: 0;
  width: 100%;
  height: 100vh;
}
</style>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用GSAP(GreenSock Animation Platform)实现烟花效果需要一些基本的HTML、CSS和JS知识,以及对GSAP的了解。下面是一个基本的烟花效果实现步骤: 1. 创建HTML结构 首先,创建一个HTML文件并添加一个canvas元素,用于在其上绘制烟花效果。在此之前,可以添加一些样式来设置canvas的大小和位置。例如: ```html <!doctype html> <html> <head> <meta charset="utf-8"> <title>Fireworks with GSAP</title> <style> body { margin: 0; padding: 0; background-color: #000; } canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } </style> </head> <body> <canvas id="canvas"></canvas> <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.5.1/gsap.min.js"></script> <script src="script.js"></script> </body> </html> ``` 2. 编写JS代码 在JS文件中,使用GSAP创建一个TweenMax对象,用于创建动画效果。下面的代码演示了如何创建一个基本的烟花效果: ```javascript var canvas = document.getElementById("canvas"), ctx = canvas.getContext("2d"), particles = [], particleCount = 30, colors = ["#FFFFFF", "#FF4136", "#0074D9", "#2ECC40", "#FFDC00", "#FF851B", "#B10DC9", "#111111"], WIDTH = window.innerWidth, HEIGHT = window.innerHeight; canvas.width = WIDTH; canvas.height = HEIGHT; function Particle() { this.radius = Math.random() * 20 + 10; this.x = Math.random() * (WIDTH - this.radius * 2) + this.radius; this.y = Math.random() * (HEIGHT - this.radius * 2) + this.radius; this.color = colors[Math.floor(Math.random() * colors.length)]; this.vx = Math.random() * 4 - 2; this.vy = Math.random() * 4 - 2; this.gravity = 0.2; this.opacity = 1; } Particle.prototype.draw = function() { ctx.globalAlpha = this.opacity; ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false); ctx.fillStyle = this.color; ctx.fill(); }; Particle.prototype.update = function() { this.x += this.vx; this.y += this.vy; this.opacity -= 0.01; this.radius -= 0.1; }; function createParticles() { for (var i = 0; i < particleCount; i++) { particles.push(new Particle()); } } function drawParticles() { for (var i = 0; i < particles.length; i++) { particles[i].draw(); } } function updateParticles() { for (var i = 0; i < particles.length; i++) { particles[i].update(); if (particles[i].opacity <= 0) { particles.splice(i, 1); } } } function animate() { requestAnimationFrame(animate); ctx.clearRect(0, 0, WIDTH, HEIGHT); drawParticles(); updateParticles(); } createParticles(); animate(); ``` 代码解释: - 创建canvas元素和一些变量,包括particles数组、particleCount(粒子数量)、颜色数组、canvas的宽度和高度。 - 创建Particle对象,包括粒子的半径、位置、颜色和速度等属性,以及draw(绘制)和update(更新)方法。 - 创建createParticles(创建粒子)、drawParticles(绘制粒子)和updateParticles(更新粒子)函数。 - 创建animate(动画)函数,使用requestAnimationFrame方法实现动画效果。 - 调用createParticles函数和animate函数,实现烟花效果的基本动画。 3. 添加交互效果 要使烟花效果更加有趣,可以添加一些交互效果。例如,点击鼠标时,可以在鼠标位置创建一个新的烟花效果。下面是如何实现这个交互效果的代码: ```javascript function addFirework(x, y) { for (var i = 0; i < particleCount; i++) { var p = new Particle(); p.x = x; p.y = y; particles.push(p); } } canvas.addEventListener("mousedown", function(e) { var x = e.clientX, y = e.clientY; addFirework(x, y); }); ``` 代码解释: - 创建addFirework函数,用于在指定位置创建烟花效果。 - 在canvas元素上添加mousedown事件,当鼠标按下时,获取鼠标位置,并调用addFirework函数创建烟花效果。 4. 添加动画效果 为了让烟花效果更加生动,可以添加一些动画效果,例如粒子的旋转和缩放效果。使用TweenMax对象可以轻松地实现这些效果。下面是如何添加粒子旋转和缩放效果的代码: ```javascript Particle.prototype.update = function() { this.x += this.vx; this.y += this.vy; this.opacity -= 0.01; this.radius -= 0.1; this.rotation += Math.random() * 20 - 10; this.scale -= 0.005; }; function drawParticles() { for (var i = 0; i < particles.length; i++) { var p = particles[i], tl = new TimelineMax(); tl.to(p, 0.5, {scale: 1.2, ease: Power1.easeInOut}) .to(p, 0.5, {scale: 1, ease: Power1.easeInOut}) .to(p, 0.5, {rotation: "+=360", ease: Power1.easeInOut}); p.draw(); } } ``` 代码解释: - 在Particle对象的update方法中,添加rotation(旋转)和scale(缩放)属性,并在更新方法中增加旋转和缩放效果。 - 在drawParticles函数中,使用TweenMax对象创建一个时间轴,包括粒子的缩放和旋转效果。 - 调用p.draw方法绘制粒子。 5. 完整代码 下面是完整的烟花效果代码: ```html <!doctype html> <html> <head> <meta charset="utf-8"> <title>Fireworks with GSAP</title> <style> body { margin: 0; padding: 0; background-color: #000; } canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } </style> </head> <body> <canvas id="canvas"></canvas> <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.5.1/gsap.min.js"></script> <script> var canvas = document.getElementById("canvas"), ctx = canvas.getContext("2d"), particles = [], particleCount = 30, colors = ["#FFFFFF", "#FF4136", "#0074D9", "#2ECC40", "#FFDC00", "#FF851B", "#B10DC9", "#111111"], WIDTH = window.innerWidth, HEIGHT = window.innerHeight; canvas.width = WIDTH; canvas.height = HEIGHT; function Particle() { this.radius = Math.random() * 20 + 10; this.x = Math.random() * (WIDTH - this.radius * 2) + this.radius; this.y = Math.random() * (HEIGHT - this.radius * 2) + this.radius; this.color = colors[Math.floor(Math.random() * colors.length)]; this.vx = Math.random() * 4 - 2; this.vy = Math.random() * 4 - 2; this.gravity = 0.2; this.opacity = 1; this.rotation = 0; this.scale = 1; } Particle.prototype.draw = function() { ctx.save(); ctx.translate(this.x, this.y); ctx.rotate(this.rotation * Math.PI / 180); ctx.scale(this.scale, this.scale); ctx.globalAlpha = this.opacity; ctx.beginPath(); ctx.arc(0, 0, this.radius, 0, Math.PI * 2, false); ctx.fillStyle = this.color; ctx.fill(); ctx.restore(); }; Particle.prototype.update = function() { this.x += this.vx; this.y += this.vy; this.opacity -= 0.01; this.radius -= 0.1; this.rotation += Math.random() * 20 - 10; this.scale -= 0.005; }; function createParticles() { for (var i = 0; i < particleCount; i++) { particles.push(new Particle()); } } function drawParticles() { for (var i = 0; i < particles.length; i++) { var p = particles[i], tl = new TimelineMax(); tl.to(p, 0.5, {scale: 1.2, ease: Power1.easeInOut}) .to(p, 0.5, {scale: 1, ease: Power1.easeInOut}) .to(p, 0.5, {rotation: "+=360", ease: Power1.easeInOut}); p.draw(); } } function updateParticles() { for (var i = 0; i < particles.length; i++) { particles[i].update(); if (particles[i].opacity <= 0) { particles.splice(i, 1); } } } function addFirework(x, y) { for (var i = 0; i < particleCount; i++) { var p = new Particle(); p.x = x; p.y = y; particles.push(p); } } canvas.addEventListener("mousedown", function(e) { var x = e.clientX, y = e.clientY; addFirework(x, y); }); function animate() { requestAnimationFrame(animate); ctx.clearRect(0, 0, WIDTH, HEIGHT); drawParticles(); updateParticles(); } createParticles(); animate(); </script> </body> </html> ``` 运行代码后,点击鼠标可以在指定位置创建烟花效果,粒子会旋转和缩放,直到消失。可以尝试修改代码中的参数,例如粒子数量、颜色数组、速度等,以获得不同的烟花效果

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值