[英]Create a +1 Animation when button is clicked
I was making a game where you get money for clicking on a button.我正在制作一款游戏,您可以通过点击按钮获得金钱。 Here's a part of it:
这是其中的一部分:
var money = 0; var currentMoney = document.getElementById("currentMoney"); function addOne() { money++; currentMoney.innerHTML = money; }
<button onclick="addOne();">+1$</button> <p>Money: <b id="currentMoney">0</b>$</p>
Later i was looking in the Internet and i found a website called Cookie Clicker and i saw this animation that when you click the cookie, a +1 is going up and fades away (It is not so important that the number goes up where the mouse is for my example):后来我在网上找了一个叫做 Cookie Clicker 的网站,我看到了这个动画,当你点击 cookie 时,+1 会上升并消失(数字上升到鼠标的位置并不重要是我的例子):
I tried making this animation but i don't know how i can make it go up:我尝试制作这个动画,但我不知道如何让它上升:
var money = 0; var currentMoney = document.getElementById("currentMoney"); function addOne() { money++; currentMoney.innerHTML = money; var moneyAnimation = document.createElement("p"); moneyAnimation.innerHTML = "+1"; document.getElementById("moneyAnimation").appendChild(moneyAnimation); }
* { margin: 0; padding: 0; } #moneyAnimation { height: 500px; border: 1px solid; width: 200px; float: right; }
<button onclick="addOne();">+1$</button> <p>Money: <b id="currentMoney">0</b>$</p> <div id="moneyAnimation"></div>
Thanks in advance提前致谢
I would recommend using CSS animation like below.我建议使用如下所示的 CSS 动画。 You can read more about keyframe animation here and here
您可以 在此处和此处阅读有关关键帧动画的更多信息
var money = 0; var currentMoney = document.getElementById("currentMoney"); function addOne() { money++; currentMoney.innerHTML = money; var moneyAnimation = document.createElement("p"); moneyAnimation.innerHTML = "+1"; document.getElementById("moneyAnimation").appendChild(moneyAnimation); moneyAnimation.classList.add("moneyAnimation"); // Add the class that animates }
* { margin: 0; padding: 0; } #moneyAnimation { height: 500px; border: 1px solid; width: 200px; float: right; position: relative; /* Added */ } @keyframes moneyAnimation { 0% { opacity: 1; top: 0; } 100% { opacity: 0; top: -50px; } } .moneyAnimation { animation: moneyAnimation 1s forwards; position: absolute; }
<button onclick="addOne();">+1$</button> <p>Money: <b id="currentMoney">0</b>$</p> <div id="moneyAnimation"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.