var list = ["富强", "民主", "文明", "和谐", "自由", "平等", "公正", "法治", "爱国", "敬业", "诚信", "友善"]; var body = document.querySelector("body"); var index = 0; body.onclick = function(e) { var Xcoord = e.x; var Ycoord = e.y; var span = document.createElement("span"); var color = parseInt(Math.random() * 360); span.innerText = list[index]; span.style.position = "fixed"; span.style.left = `${Xcoord}px` span.style.top = `${Ycoord}px` span.style.color = `hsl(${color},100%,50%,0.7)`; var direction = [ 'LeftTop', 'LeftBottom', 'RightTop', 'RightBottom', ]; var directionIndex = parseInt(Math.random() * direction.length); motion(direction[directionIndex], span, Xcoord, Ycoord); function motion(direction, el, X, Y) { var newX = Math.random() * X var newY = Math.random() * Y setTimeout(function() { switch (direction) { case "LeftTop": el.style.left = `${Xcoord-newX}px` el.style.top = `${Ycoord-newY}px` break; case "RightTop": el.style.left = `${Xcoord+newX}px` el.style.top = `${Ycoord-newY}px` break; case "LeftBottom": el.style.left = `${Xcoord-newX}px` el.style.top = `${Ycoord+newY}px` break; case "RightBottom": el.style.left = `${Xcoord+newX}px` el.style.top = `${Ycoord+newY}px` break; default: break; } }, 300); setTimeout(function() { body.removeChild(el) }, 1300); } body.appendChild(span) console.log(span); index += 1; if (index > 11) { index = 0 } }