如下图:
源码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Draggable and Auto-moving Floating Windows</title>
<style>
body {
font-family: Arial, sans-serif;
}
.floating-window {
position: absolute;
width: 300px;
height: 190px;
background-color: #f1f1f1;
border: 1px solid #d3d3d3;
z-index: 9999;
}
.floating-window-header {
padding: 10px;
cursor: move;
background-color: #2196F3;
color: #fff;
font-weight: bold;
}
.floating-window-content {
padding: 0px;
}
</style>
</head>
<body>
<div id="floatingWindow" class="floating-window" style="top: 200px; left: 300px;">
<div id="floatingWindowHeader" class="floating-window-header">
悬浮窗1
</div>
<div class="floating-window-content">
<canvas id="threeCanvas" style="width: 100%; height: 100%;"></canvas>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// 可拖动悬浮窗
const floatingWindow = document.getElementById("floatingWindow");
const floatingWindowHeader = document.getElementById("floatingWindowHeader");
let offsetX = 0;
let offsetY = 0;
let isMouseDown = false;
floatingWindowHeader.addEventListener("mousedown", (e) => {
offsetX = e.clientX - floatingWindow.getBoundingClientRect().left;
offsetY = e.clientY - floatingWindow.getBoundingClientRect().top;
isMouseDown = true;
});
document.addEventListener("mousemove", (e) => {
if (!isMouseDown) return;
floatingWindow.style.left = e.clientX - offsetX + "px";
floatingWindow.style.top = e.clientY - offsetY + "px";
});
document.addEventListener("mouseup", () => {
isMouseDown = false;
});
// Three.js渲染器
const canvas = document.getElementById("threeCanvas");
const renderer = new THREE.WebGLRenderer({ canvas });
// Three.js相机
const camera = new THREE.PerspectiveCamera(75, canvas.clientWidth / canvas.clientHeight, 0.1, 1000);
camera.position.z = 5;
// Three.js场景
const scene = new THREE.Scene();
// Three.js立方体
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
const cube = new THREE.Mesh(geometry, material);
scene.add(cube);
// Three.js渲染循环
function render() {
requestAnimationFrame(render);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
render();
</script>
</body>
</html>