定义标签
1 2 3 4 5
| <div class="card"> <img src="3.3.png" alt="x" width="100%" /> <h3>北极光之夜</h3> <p>生活下去,错误下去,堕落下去,为胜利而欢呼,从生命中重新创造生命!</p> </div>
|
卡片和文字的基本样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| .card { width: 200px; height: 300px; box-shadow: 1px 1px 5px #555; cursor: pointer; background-color: rgb(243, 243, 243); position: relative; overflow: hidden; } .card h3, .card p { padding: 5px; text-align: center; font-family: "fangsong"; font-weight: bold; user-select: none; }
|
cursor: pointer; 鼠标样式为小手。
overflow: hidden; 子元素大小超出卡片区域的被隐藏。
user-select: none; 文本不可选中。
js部分,见注释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <script> var card = document.querySelector(".card"); card.addEventListener("click", function (e) { let x = e.clientX - this.offsetLeft; let y = e.clientY - this.offsetTop; let circle = document.createElement("span"); circle.style.left = x + "px"; circle.style.top = y + "px"; card.appendChild(circle); setInterval(function () { circle.remove(); }, 1000); }); </script>
|
添加上一步创建的 span 元素的css样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| .card span { position: absolute; transform: translate(-50%, -50%);
background-color: rgb(255, 254, 254); border-radius: 50%; animation: big 1s; } @keyframes big { 0% { width: 0px; height: 0px; opacity: 1; } 100% { width: 400px; height: 400px; opacity: 0; } }
|
position: absolute; 绝对定位。
transform: translate(-50%,-50%); 向左和上移动自身宽度和高度的一半。
animation: big 1s; 定义动画,刚好1s完成动画 。
opacity: 1; 不透明。
opacity: 0; 透明。
完整源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| <!doctype html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { height: 100vh; display: flex; justify-content: center; align-items: center; background-image: radial-gradient(white, black); } .card { width: 200px; height: 300px; box-shadow: 1px 1px 5px #555; cursor: pointer; background-color: rgb(243, 243, 243); position: relative; overflow: hidden; } .card h3, .card p { padding: 5px; text-align: center; font-family: "fangsong"; font-weight: bold; user-select: none; } .card span { position: absolute; transform: translate(-50%, -50%);
background-color: rgb(255, 254, 254); border-radius: 50%; animation: big 1s; } @keyframes big { 0% { width: 0px; height: 0px; opacity: 1; } 100% { width: 400px; height: 400px; opacity: 0; } } </style> </head> <body> <div class="card"> <img src="3.3.png" alt="x" width="100%" /> <h3>北极光之夜</h3> <p>生活下去,错误下去,堕落下去,为胜利而欢呼,从生命中重新创造生命!</p> </div> <script> var card = document.querySelector(".card"); card.addEventListener("click", function (e) { let x = e.clientX - this.offsetLeft; let y = e.clientY - this.offsetTop;
let circle = document.createElement("span"); circle.style.left = x + "px"; circle.style.top = y + "px";
card.appendChild(circle);
setInterval(function () { circle.remove(); }, 1000); }); </script> </body> </html>
|