首页 > Web开发 > 详细

[RxJS@7.0.0] AnimationFrames demo

时间:2020-04-27 09:47:01      阅读:100      评论:0      收藏:0      [点我收藏+]

 

import {defer, animationFrames, fromEvent} from ‘rxjs‘
import { animationFrame } from ‘rxjs/internal/scheduler/animationFrame‘
import {endWith, map, tap, takeWhile, finalize, mergeMap, concatMap, switchMap } from ‘rxjs/operators‘
const animation = (duration: number, distance: number) => defer(() => {
  const div = document.createElement(‘div‘)
  document.body.appendChild(div)
  div.className = ‘square‘

  return animationFrames()
    .pipe(
      map(ms => ms / duration),
      takeWhile(s => s < 1), // take max duaration time
      endWith(1),
      map(v => v * distance),
      tap(x => (div.style.transform = `translate3d(${x}px, 0px, 0px)`)),
      finalize(() => {
        div.remove()
      })
    )
})

const button = document.querySelector("#animationBtn")
fromEvent(button, ‘click‘)
  .pipe(
    mergeMap(div => animation(1000, 800)),
    // switchMap(div => animation(1000, 800)),
    // concatMap(div => animation(1000, 800))
  ).subscribe()

animation(2000, 300).subscribe()

  

 

[RxJS@7.0.0] AnimationFrames demo

原文:https://www.cnblogs.com/Answer1215/p/12783860.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!