gsap(一) 跟着鼠标移动的小球

gsap.quickTo()

<div style="width: 500px;height: 400px;background-color: lavender;margin-left:40px;margin-top:30px;" #parent>

  <div style="width: 30px;height: 30px;border-radius: 50%;background-color: lightpink;" #child></div>

</div>
  const parent = this.parent.nativeElement;
    const child = this.child.nativeElement;
    let xTo = gsap.quickTo(child, 'x', {duration: .4, ease: 'power3'});
    let yTo = gsap.quickTo(child, 'y', {duration: .4, ease: 'power3'});
    // 从固定值到x 200的动画
    // xTo(200)
    // 从500->100 的动画
    // xTo(100, 500)
    const offsetX: number = parent.getBoundingClientRect().x
    const offsetY: number = parent.getBoundingClientRect().y
    const width: number = parent.getBoundingClientRect().width;
    const height: number = parent.getBoundingClientRect().height;
    const childWidth: number = child.getBoundingClientRect().width;
    const childHeight: number = child.getBoundingClientRect().height;
    parent.addEventListener('mousemove', (e: MouseEvent) => {
      let x = e.pageX - offsetX - childWidth / 2;
      let y = e.pageY - offsetY - childHeight / 2;
      let leftBound = width - childWidth;
      let bottomBound = height - childHeight;
      console.log(childWidth, bottomBound);
      if (x <= 0) {
        x = 0;
      } else if (x > leftBound) {
        x = leftBound
      }
      if (y <= 0) {
        y = 0;
      } else if (y > bottomBound) {
        y = bottomBound
      }
      xTo(x);
      yTo(y)
    })

gsap.quickSetter()

只需要把对应的改改

   let xTo = gsap.quickTo(child, 'x', {duration: .4, ease: 'power3'});
    let yTo = gsap.quickTo(child, 'y', {duration: .4, ease: 'power3'});
     xSet(x)
      ySet(y)

设置随机位置

var boxSet = gsap.quickSetter("#box", "css");
boxSet({x:"+=100", y:"random(-100, 100)"});

设置圆的位置

var circleSet = gsap 。quickSetter ( "#circle" , "attr" ); 
circleSet ({ cx : "+=100" , cy : "random(-100, 100)" });
posted @   猫神甜辣酱  阅读(276)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2019-05-04 python(二)面向对象知识点
点击右上角即可分享
微信分享提示