技术选型:运用html、css、script、及vue组件
开发需求:编写在指定容器区域内随鼠标移动的小球,当鼠标超出容器,小球保持在边界位置不动
程序设计:
1、创建440px*440px的容器;创建30px半径的小球;
2、记录小球在面板中的位置x坐标、y坐标,其中坐标为动态变化,通过相对位置展示,为left和top;
3、创建方法,监听鼠标事件;获取鼠标所在位置x坐标,y坐标;控制小球在容器内,判断上、下,左、右,不超出边界;
实现代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>待办任务列表</title>
<script src="vue.global.js"> </script>
<style>
body {
margin: 0;
padding: 0;
}
.container {
margin: 0;
padding: 0;
position: absolute;
width: 440px;
height: 440px;
background-color: blanchedalmond;
display: inline;
}
.ball {
position: absolute;
width: 60px;
height: 60px;
left: 100px;
top: 100px;
background-color: red;
border-radius: 30px;
z-index: 100;
}
</style>
</head>
<body>
<div id="Application">
<div class="container" @mousemove.stop="move">
<div class="ball" :style="{left: offsetX + 'px', top : offsetY +'px'}">
</div>
</div>
</div>
<script>
const App = {
data() {
return {
offsetX: 0,
offsetY: 0
}
},
methods: {
move(event) {
if (event.clientX + 30 > 440) {
this.offsetX = 440 - 60
} else if (event.clientX - 30 < 0) {
this.offsetX = 0
} else {
this.offsetX = event.clientX - 30
}
if (event.clientY + 30 > 440) {
this.offsetY = 440 - 60
} else if (event.clientY - 30 < 0) {
this.offsetY = 0
} else {
this.offsetY = event.clientY - 30
}
}
}
}
Vue.createApp(App).mount("#Application")
</script>
</body>
创作不易,点击关注不迷路~~~