来源:
https://www.douyin.com/user/MS4wLjABAAAAi2oukRVcHpgD-HbVdzsxE7tYykr91YuIKukR_X_Yy08EFWRQhRrECDF6FvbvT8Xa?modal_id=7232937220259319095&vid=7232937220259319095
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<img style="position: absolute; width: 150px;" src="https://img1.imgtp.com/2023/05/15/Sq9xkJyQ.png" class="card"/>
</body>
<script src="jscom.js"></script>
</html>
const card = document.querySelector('.card');
//浏览器工具栏的高度,
//如果你是edge浏览器的话, 只要你屏幕缩放比例是100%(高分屏不清楚),应改不用改.
const BAR = 79
//获取坐标
function getClientPoint(screenX , screenY){
//屏幕到浏览器横向坐标
const clientX = screenX - window.screenX;
//屏幕到浏览器竖向坐标, 但是因为安全问题, 浏览器的工具栏的大小js没办法知道
const clientY = screenY - window.screenY - BAR;
return [clientX,clientY];
}
function getScreenPoint(clientX , clientY){
const screenX = clientX + window.screenX;
const screenY = clientY + window.screenY + BAR;
return [screenX,screenY];
}
//图片拖动代码
card.onmousedown = (e) => {
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';
//向其他窗口发送坐标信息, 通知其他窗口,
channel.postMessage(getScreenPoint(cx,cy));
};
window.onmouseup = () => {
window.onmousemove = null;
window.onmouseup = null;
};
};
//隐藏剩下的两个页面的图片, 地址后加 ?hidden
function init(){
if(location.search.includes('hidden')){
card.style.left = '-1000px';
}
}
init();
//标签页通信,传递图片坐标
const channel = new BroadcastChannel('card');
//获取传过来的数据
channel.onmessage = (e) =>{
//通过传过来的屏幕坐标, 算出相对于当前窗口的视口坐标
const [cx,cy] = getClientPoint(...e.data)
//然后更新当前窗口的图片坐标
card.style.left = cx + 'px';
card.style.top = cy + 'px';
}
因为三个窗口中, 有两个是默认不显示图片的, 所以那两个不显示图片的页面要在地址后面加 ?hidden 隐藏图片
*
*
*
关于浏览器工具栏高度获取 的安全问题, chatGPT的解释