rxjs6 拖拽接龙

原文链接: rxjs6 拖拽接龙

上一篇: rxjs6 简单拖拽

下一篇: rxjs6 区分单击和双击

效果

参考其他博客, 使用rxjs6实现拖拽接龙

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script src="./rxjs.umd.js"></script>
  <style>
    .box {
      width: 50px;
      height: 50px;
      background: deepskyblue;
      position: absolute;
      left: 0;
      right: 0;
      border-radius: 50%;
    }

  </style>
</head>
<body>
<div class="box move" style="background: deepskyblue;z-index: 10" id="box"></div>
<div class="box move" style="background: red;z-index: 9" ></div>
<div class="box move" style="background: orange;z-index: 8"></div>
<div class="box move" style="background: yellow;z-index: 7"></div>
<div class="box move" style="background: green;z-index: 6"></div>
<div class="box move" style="background: blue;z-index: 5"></div>
<div class="box move" style="background: pink;z-index: 4"></div>
<div class="box move" style="background: purple;z-index: 3"></div>

<script>
  // https://ithelp.ithome.com.tw/articles/10187999
  const {range, interval, zip, fromEvent, from} = rxjs;
  const {map, tap, mergeMap, startWith, mapTo, filter, throttleTime, debounceTime, repeat, switchMap, takeUntil} = rxjs.operators;

  let boxes = document.getElementsByClassName('move')
  let box = document.getElementById('box')
  // 使用 zip 操作符构造一个由 boxes 组成的流
  const boxes$ = from([...boxes])
  boxes$.subscribe(i => console.log('boxes$', i))
  // const delayBoxes$ = boxes$.pipe(
  //   // zip(
  //   //   startWith(0),
  //   //   interval(100),
  //   // )
  //   //   interval(100),
  // )
  let delayBoxes$ = zip(
    interval(100),
    boxes$
  )
  // delayBoxes$.subscribe(
  //   console.log
  // )

  let mousedown = fromEvent(box, 'mousedown')
  let mouseup = fromEvent(document, 'mouseup')
  let mousemove = fromEvent(document, 'mousemove')

  function setPosition(elem, position) {
    elem.style['left'] = position.x + 'px'
    elem.style['top'] = position.y + 'px'
  }


  function getPosition(elem) {
    return {
      x: +elem.style.left.replace('px', ''),
      y: +elem.style.top.replace('px', ''),
    }
  }


  mousedown.pipe(
    map(({x: clickX, y: clickY}) => {
        let {x: initX, y: initY} = getPosition(box)
        return {
          initX, initY, clickX, clickY, clickTime: +new Date()
        }
      }
    )).pipe(
    switchMap(({initX, initY, clickX, clickY, clickTime}) => {
      return mousemove.pipe(
        map(e => {
          let dx = e.x - clickX
          let dy = e.y - clickY
          return {
            x: initX + dx,
            y: initY + dy,
            time: +new Date() - clickTime
          }
        }),
        takeUntil(mouseup),
      )


    }),
    mergeMap(
      (position) => {
        // console.log('merge', position)
        return delayBoxes$.pipe(
          tap(
            ([_, box]) => {
              console.log('box position', box, position)
              setPosition(box, position)
            }
          )
        )
      }
    ),
    repeat()
  ).subscribe(console.log)
</script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值