react+ts小球抛物线

1 篇文章 0 订阅
1 篇文章 0 订阅

代码奉上

css:

html,body{
    height: 100%;
}
#root,.App,.wrap{
    height: 100%;
    width: 100%;
}

.ball{
    width: 60px;
    height: 60px;
    border-radius: 50%;
    position: absolute;
    background-color: tomato;
    transition: left 1s linear, top 1s linear;
    display: none;
}
.aim{
    width: 100px;
    height: 100px;
    border: 1px solid #000;
    position: absolute;
    bottom: 0;
    right: 0;
}

 tsx代码:

import React, { Component, createRef } from 'react'

import './index.css'

interface IProp { }

interface IsState { }

export default class index extends Component<IsState, IProp> {
  ball = createRef<HTMLDivElement>()
  wrap = createRef<HTMLDivElement>()
  flag: boolean = false
  // eslint-disable-next-line @typescript-eslint/no-useless-constructor
  constructor(props: IProp) {
    super(props);
  }
  FnStart(e: TouchEvent) {

    if (!this.flag) {

      this.flag = true
      // 小球元素
      const ball = this.ball.current as HTMLDivElement;
      // 显示小球
      ball.style.display = 'block';
      // 操纵定位
      ball.style.left = e.changedTouches[0].pageX + 'px';
      ball.style.top = e.changedTouches[0].pageY + 'px';
      // 落脚点元素
      const wrap = this.wrap.current as HTMLDivElement;
      // 小球的落脚点的距离页面顶端的距离
      const H = wrap.offsetTop;
      // 小球落脚点的距离页面左边的位置
      const L = wrap.offsetLeft;

      // 动画过渡时 给小球赋值
      setTimeout(() => {
        ball.style.top = H + 'px';
        ball.style.left = L  + 'px';
        ball.style.transition = 'left 1s linear, top 1s cubic-bezier(0.4, -0.29, 0.7, 0.1)';
      }, 50)
      // 小球过渡结束事件
      ball.addEventListener("transitionend", () => {
        // 过度结束小球隐藏        
        ball.style.display = "none";
        this.flag = false
      })
    }
  }
  componentDidMount() {
    document.ontouchstart = this.FnStart.bind(this)

  }

  render() {
    return (
      <div className='wrap'>
        <div ref={this.wrap} className="aim"></div>
        <div ref={this.ball} className="ball"></div>
      </div>
    )
  }
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值