1 Star 1 Fork 0

程小右/固定动画

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
index.html 3.73 KB
一键复制 编辑 原始数据 按行查看 历史
程小右 提交于 2023-11-26 21:13 . 第一次提交
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=500,height=500 initial-scale=1.0" />
<title>冬子</title>
<style>
:root {
--offset-x: 0;
--offset-y: 0;
}
* {
margin: 0;
padding: 0;
}
.box {
width: 50px;
height: 50px;
background-color: skyblue;
position: absolute;
top: calc(100px + var(--offset-y));
left: calc(100px + var(--offset-x));
border-radius: 50%;
font-size: 16px;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
.arrows {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.arrows-head {
width: 23px;
height: 5px;
background-color: red;
transform: rotate(-45deg) translateY(0px) translateX(-5px);
}
.arrows-head-left {
width: 20px;
height: 5px;
background-color: red;
transform: rotate(-135deg) translateY(-10px) translateX(-7px);
}
.arrows-body {
width: 100px;
height: 5px;
background-color: red;
}
</style>
</head>
<body>
<div class="box">
<br />
程小右
</div>
<div class="arrows">
<div class="arrows-head"></div>
<div class="arrows-head-left"></div>
<div class="arrows-body"></div>
</div>
<script>
const boxDom = document.querySelector(".box");
let availTop = 0;
let availLeft = 0;
// 需要用户点击授权getScreenDetails这个API
// boxDom.onclick = async () => {
// const screenDetails = await window.getScreenDetails();
// console.log(screenDetails);
// };
(async () => {
const screenDetails = await window.getScreenDetails();
availTop = screenDetails.currentScreen.availTop;
availLeft = screenDetails.currentScreen.availLeft;
window.requestAnimationFrame(update);
console.log("运行几次");
window.resizeTo(500, 500);
window.resizeBy(500, 500);
})();
function update() {
const screenX = window.screenX;
const screenY = window.screenY;
boxDom.style.setProperty("--offset-x", `${availLeft - screenX}px`);
boxDom.style.setProperty("--offset-y", `${availTop - screenY}px`);
window.requestAnimationFrame(update);
updateAngle();
}
function updateAngle() {
// 修改箭头角度
let arrowsDom = document.querySelector(".arrows");
// 获取两个dom的位置
// console.log(boxDom.getBoundingClientRect());
// console.log(arrowsDom.getBoundingClientRect());
const rect1 = boxDom.getBoundingClientRect();
const rect2 = arrowsDom.getBoundingClientRect();
const centerX1 = rect1.left + rect1.width / 2;
const centerY1 = rect1.top + rect1.height / 2;
const centerX2 = rect2.left + rect2.width / 2;
const centerY2 = rect2.top + rect2.height / 2;
const angle =
Math.atan2(centerY2 - centerY1, centerX2 - centerX1) *
(180 / Math.PI);
arrowsDom.style.transform = `translate(-50%, -50%) rotate(${angle}deg)`;
if (crash(rect1, rect2)) {
alert("被你发现了!!!哈哈哈哈");
}
}
function crash(rect1, rect2) {
if (
rect1.left > rect2.right ||
rect1.top > rect2.bottom ||
rect1.right < rect2.left ||
rect1.bottom < rect2.top
) {
return false; // 未碰撞
} else {
return true; // 碰撞
}
}
</script>
</body>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/C_D_S_admin/fixed-animation.git
git@gitee.com:C_D_S_admin/fixed-animation.git
C_D_S_admin
fixed-animation
固定动画
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385