练习demo
编辑器制作效果展示:
代码展示:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#div {border-radius: 70px;width: 200px;height: 200px;background-color: green;position: absolute;left: 0;top: 0}
#div1 {width: 220px;height: 220px;transform: rotate(45deg);position: absolute;left: -10px;top: -10px}
.bt {color: #6b1d7f;transition: all 0.5s;position: absolute;width: 50px;height: 50px;border-radius: 20px;font-size: 30px;font-weight: bold;outline: 0;border: 0;text-align: center;line-height: 40px}
.bt:hover {background-color: yellow}
.bt:active {color: red;transform: scale(1.2)}
.bt1 {left: 50%;margin-left: -25px}
.bt2 {left: 50%;margin-left: -25px;top: 100%;margin-top: -50px}
.bt3 {top: 50%;margin-top: -25px}
.bt4 {left: 100%;margin-left: -50px;top: 50%;margin-top: -25px}
.span {background-color: deeppink;transform: rotate(-45deg);position: absolute;border-radius: 10px;width: 100px;height: 50px;left: 50%;top: 50%;margin-top: -25px;margin-left: -50px;text-align: center;line-height: 50px;font-size: 40px;color: white}
</style>
</head>
<body>
<div id="div">
<div id="div1">
<button class="bt bt1">↑</button>
<button class="bt bt2">↓</button>
<button class="bt bt3">←</button>
<button class="bt bt4">→</button>
<span class="span">111</span>
</div>
</div>
<script>
var speed = 1;
var timer;
var od = document.querySelector("#div");
var os = document.querySelector(".span");
var obt = document.querySelectorAll(".bt");
// 触控方法
for (var i = 0; i < obt.length; i++) {
obt[i].addEventListener("mouseover", run);
od.addEventListener("click", function () {
clearInterval(timer);
})
}
// 触控逻辑
function run() {
clearInterval(timer);
var v = this.innerText;
switch (v) {
case "↑":
timer = setInterval(function () {
os.innerText = "右上";
od.style.top = od.offsetTop - speed + "px";
od.style.left = od.offsetLeft + speed + "px";
}, 10)
break;
case "↓":
timer = setInterval(function () {
os.innerText = "左下";
od.style.top = od.offsetTop + speed + "px";
od.style.left = od.offsetLeft - speed + "px";
}, 10)
break;
case "←":
timer = setInterval(function () {
os.innerText = "左上";
od.style.top = od.offsetTop - speed + "px";
od.style.left = od.offsetLeft - speed + "px";
}, 10)
break;
case "→":
timer = setInterval(function () {
os.innerText = "右下";
od.style.top = od.offsetTop + speed + "px";
od.style.left = od.offsetLeft + speed + "px";
}, 10)
break;
}
}
</script>
</body>
</html>
整理笔记时发现自己做的小demo,传上来了,么么哒!
来源:https://blog.csdn.net/jiepp/article/details/101149753