Files
ClassContent/历届学生/font-end-nine/项目练习/上课项目/js案例/富强.html
2024-09-27 02:06:13 +08:00

139 lines
2.8 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>富强</title>
<style>
body {
height: 100vh;
}
body,
ul,
li {
margin: 0;
padding: 0;
list-style: none;
}
span {
position: fixed;
}
</style>
</head>
<body>
</body>
<script>
// 1:在鼠标点击的位置,依次显示字
// 1 为body绑定点击事件,然后拿到 x,y坐标,设置到 span(使用js创建) 标签上
// 2 把做好的span 放到body上
// 2: 让文字运动起来
var textArr = ['富强', '民主', '文明', '和谐', '自由', '平等', '公正', '法治', '爱国', '敬业', '诚信', '友善'];
var index = 0;
$("body").onclick = function (e) {
var time = null;
var { x, y } = e;
var span = document.createElement("span");
span.innerText = textArr[index];
span.style = `
left: ${x}px;
top: ${y}px;
`;
index++
index > 11 ? index = 0 : ''
span.move = 0
$("body").appendChild(span)
function motion() {
span.move += 4;
console.log("span.move: ", span.move);
if (span.move > 100) {
setTimeout(() => $("body").removeChild(span), 300)
// clearTimeout(time)
cancelAnimationFrame(time)
} else {
span.style.top = `${y - span.move}px`
time = requestAnimationFrame(motion)
}
}
motion()
}
function motion(el, y) {
// time = setTimeout(function fn() {
// el.move+=4;
// console.log("el.move: ", el.move);
// if (el.move > 100) {
// setTimeout(() => $("body").removeChild(el),300)
// clearTimeout(time)
// } else {
// el.style.top = `${y - el.move}px`
// time = setTimeout(fn, 1000/60)
// }
// }, 1000/60)
// setTimeout(function () {
// $("body").removeChild(el)
// }, 800)
}
function $(className) {
return document.querySelector(className)
}
// 箭头函数
// var a = test => console.log("test")
// a(1)
// 闭包
// 函数内部的函数访问外部变量的过程
// 为了解决:变量缓存的问题
function a() {
var c = 1
function test() {
c++
return c;
}
return test
}
var t = a();
console.log(t());
console.log(t());
var c = a();
console.log(c());
</script>
</html>