1. 跨标签通讯

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>跨标签通讯</title>
  </head>
  <style>
    .card {
      width: 300px;
      height: 300px;
      background-color: #f00;
      position: fixed;
      top: 100px;
      left: 100px;
    }
  </style>
  <body>
    跨标签通讯
    <div class="card">card</div>
  </body>
  <script>
    const barHeight = () => window.outerHeight - window.innerHeight
    const cardDom = document.querySelector('.card')
    cardDom.style.top = 100 + 'px'
    cardDom.style.left = 100 + 'px'
    cardDom.style.background = new URLSearchParams(window.location.search).get('color') || 'red'

    // 屏幕坐标转换为窗口坐标
    const screenToClient = (screenX, screenY) => {
      const clienX = screenX - window.screenX
      const clienY = screenY - window.screenY - barHeight()
      return [clienX, clienY]
    }

    // 窗口坐标转换为屏幕坐标
    const clientToScreen = (clienX, clienY) => {
      const screenX = clienX + window.screenX
      const screenY = clienY + window.screenY + barHeight()
      return [screenX, screenY]
    }

    // 创建 Broadcast Channel
    const channel = new BroadcastChannel('myChannel')
    // 监听消息
    channel.onmessage = (event) => {
      // 处理接收到的消息
      const [clienX, clienY] = screenToClient(...event.data)
      // 不同窗口的卡片要在同一个位置, 要放到同一个坐标系下面,保持屏幕坐标一致
      cardDom.style.left = clienX + 'px'
      cardDom.style.top = clienY + 'px'
    }

    // 发送消息
    const sendMessage = (message) => {
      channel.postMessage(message)
    }

    window.onload = function () {
      cardDom.onmousedown = function (e) {
        cardDom.style.cursor = 'pointer'
        let x = e.pageX - cardDom.offsetLeft
        let y = e.pageY - cardDom.offsetTop
        window.onmousemove = function (e) {
          cardDom.style.left = e.clientX - x + 'px'
          cardDom.style.top = e.clientY - y + 'px'
          // 发送消息
          const clientCoordinateX = e.clientX - x
          const clientCoordinateY = e.clientY - y
          const ScreenCoordinate = clientToScreen(clientCoordinateX, clientCoordinateY)
          sendMessage(ScreenCoordinate)
        }
        window.onmouseup = function () {
          window.onmousemove = null
          window.onmouseup = null
          cardDom.style.cursor = 'unset'
        }
      }
    }
  </script>
</html>
  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值