-
创建原始 html 、 css 、 js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
margin: 0;
padding: 0;
box-sizing: border-box;
}
img{
position: fixed;
left: 0;
top: 0;
width: 200px;
height: 300px;
}
</style>
</head>
<body>
<script src="./index.js"></script>
</body>
</html>
-
构建初始化init函数
const card = document.createElement('img');
document.body.appendChild(card);
(function init() {
const url = new URL(window.location.href);
const type = url.searchParams.get("type") || 'J'
card.src = ./img/${type}.png
card.onmousedown = (e) => {
e.preventDefault()
let x = e.pageX - card.offsetLeft;
let y = e.pageY - card.offsetTop;
window.onmousemove = (e) => {
const cx = e.pageX - x
const cy = e.pageY - y
card.style.left = cx + 'px';
card.style.top = cy + 'px';
}
window.onmouseup = (e) => {
window.onmousemove = null
window.onmouseup = null
}
}
})();
-
创建辅助函数
- 获取浏览器 bom 的高度
- 将点击的视口坐标转换成屏幕坐标
- 将屏幕坐标转换成视口坐标
function getBarHeight() {
return window.outerHeight - window.innerHeight
}
function clientToScreen(x, y) {
const screenx = x + window.screenX
const screeny = y + window.screenY + getBarHeight()
return [screenx, screeny]
}
function screenToClient(x, y) {
const clientX = x - window.screenX
const clientY = y - window.screenY - getBarHeight()
return [clientX, clientY]
}
-
创建 BroadcastChannel 广播频道对象
const channel = new BroadcastChannel("card");
channel.onmessage = (e) => {
console.log(e.data);
const clientPoints = screenToClient(...e.data);
card.style.left = clientPoints[0] + 'px';
card.style.top = clientPoints[1] + 'px';
}
(function init() {
const url = new URL(window.location.href);
const type = url.searchParams.get("type") || 'J'
card.src = ./img/${type}.png
card.onmousedown = (e) => {
e.preventDefault()
let x = e.pageX - card.offsetLeft;
let y = e.pageY - card.offsetTop;
window.onmousemove = (e) => {
const cx = e.pageX - x
const cy = e.pageY - y
card.style.left = cx + 'px';
card.style.top = cy + 'px';
const screenPoints = clientToScreen(cx, cy)
channel.postMessage(screenPoints)
}
window.onmouseup = (e) => {
window.onmousemove = null
window.onmouseup = null
}
}
})();