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


    gsap.quickTo()

    <div style=" 500px;height: 400px;background-color: lavender;margin-left:40px;margin-top:30px;" #parent>
    
      <div style=" 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  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)" });
    
  • 相关阅读:
    JS 可选链操作符?. 空值合并运算符?? 详解,更精简的安全取值与默认值设置小技巧
    手写一个 Promise
    Leetcode 403 青蛙过河 DP
    Leeetcode 221 最大正方形 DP
    Leetcode 139 单词拆分
    Unity周记: 2021.07.26-08.15
    Unity周记: 2021.07.19-07.25
    Unity周记: 2020.07.12-07.18
    Unity周记: 2020.07.05-07.11
    线性规划
  • 原文地址:https://www.cnblogs.com/fangdongdemao/p/16221913.html
Copyright © 2020-2023  润新知