【问题标题】:JavaScript CSS animation only works onceJavaScript CSS 动画只能工作一次
【发布时间】:2017-09-10 18:27:36
【问题描述】:

我有一个银行应用程序,它有一只超级酷的手下来,将一枚硬币放入存钱罐中。问题是,这只手只掉硬币一次就停止工作了。

这是我的代码:

* {
  margin:0px;
  padding:0px;
}

body {
  background-image:url('../images/bg.png');
}

@keyframes moveDown {
  0% {}
  100% {margin-top:-220px;}
}

@keyframes fadeIn {
  0% {opacity:0;}
  90%{opacity:1}
  100%{opacity:0;}
}

#hand {
  height:300px;
  width:300px;
  position:absolute;
  left:50%;
  margin-left:-120px;
  margin-top:-350px;
  background-image:url("../images/hand.png");
  opacity:0;
}

#pigBox {
  margin-left:auto;
  margin-right:auto;
  height:600px;
  width:500px;
  margin-top:250px;
  position:relative;

  img {
    margin:0px 50px;
  }

}

input[type=text] {
  float:left;
  display:block;
  font-size:2em;
  width:500px;
  border-radius:10px;
  border:solid 2px pink;
  margin-top:10px;
  font-family: 'Gochi Hand', cursive;
  text-align:center;
  padding:2px;
}

#deposit {
  float:left;
  display:block;
  font-family: 'Gochi Hand', cursive;
  font-size:2em;
  clear:left;
  width:200px;
  margin:10px 25px;
  border-radius:10px;
  background-color:pink;
  border:solid 2px pink;
  padding:2px;
  cursor:pointer;

  &:hover {
    background-color:white;
  }
}

#withdraw {
  float:left;
  display:block;
  font-family: 'Gochi Hand', cursive;
  font-size:2em;
  width:200px;
  margin:10px 25px;
  border-radius:10px;
  background-color:pink;
  border:solid 2px pink;
  padding:2px;
  cursor:pointer;

  &:hover {
    background-color:white;
  }
}

label {
  text-align:center;
  display:block;
  font-family: 'Gochi Hand', cursive;
  font-size:2.5em;
  border-radius:10px;
  width:300px;
  margin-left:100px;
  margin-right:100px;
  margin-top:5px;
  margin-bottom:-15px;
}

document.getElementById('balance').value = "1000"

var balance = document.getElementById('balance').value;
var deposit = document.getElementById('deposit');
var withdraw = document.getElementById('withdraw');
var hand = document.getElementById('hand');

deposit.addEventListener('click', depositCash);
withdraw.addEventListener('click', withdrawCash);

function depositCash() {
  var depositAmt = prompt('How much would you like to deposit?');

  if(depositAmt != Number(depositAmt)) {
    return alert('Please enter a valid integer.');
  }
  else if (Number(depositAmt) < 0) {
    return alert('Please enter a positive integer.');
  }

  hand.style.animation = 'moveDown 1.5s ease-in-out, fadeIn 1.5s ease-in-out';
  balance = Number(balance) + Number(depositAmt);
  document.getElementById('balance').value = balance;
}

function withdrawCash() {
  var withdrawAmt = prompt('How much you you like to withdraw?');

  if(withdrawAmt != Number(withdrawAmt)) {
    return alert('Please enter a valid integer.');
  }
  else if (Number(withdrawAmt) < 0) {
    return alert('Please enter a positive integer.');
  }
  else if(withdrawAmt > balance) {
    return alert("Your balance isn't large enough to withdraw that amount!")
  }


  balance = Number(balance) - Number(withdrawAmt);
  document.getElementById('balance').value = balance;
}

<section id="pigBox">
      <div id="hand"></div><!-- end of hand-->
      <img src="images/pig.png" />
      <label>Balance: </label><input type="text" id="balance" />
      <button id="deposit"> Deposit </button>
      <button id="withdraw"> Withdraw </button>
  </section><!-- end of pigBox-->

<a href="http://imgur.com/FxwmGFi"><img src="http://i.imgur.com/FxwmGFi.png" title="source: imgur.com" /></a>

当您将钱存入存钱罐时,请注意 hand.style 动画。

大家有什么想法吗?

谢谢!

【问题讨论】:

  • 独立的例子请教?我也想看看超酷的手:-)
  • 如何在此处托管我的图片和 css ;)
  • @Brixsta 您可以将图像添加到您的问题中,并且应该为它们分配一个 imgur 链接。对于 CSS,只需像使用 JavaScript 和 HTML 一样发布。
  • 如果您不想上传图片,也可以使用带有数据 uri 和 css background-image 的假图片
  • @Brixsta 您还有另一个问题...您的代码第一次获取余额时,它会将其作为字符串获取,因此如果您最初尝试提款(任何 > 1),它会说您没有足够的美元。用parseInt() 包装你的balance 变量将解决这个问题。

标签: javascript html css


【解决方案1】:

使用动画 svg 的快速简单的解决方案是使用随机查询加载图像,例如;

假设 javascript 环境中已有函数 loadImage,

loadImage('image.svg?u='+new Date().getMilliseconds());

你欺骗浏览器认为它不是同一个文件并且动画开始了。

【讨论】:

  • 不是 .gif 更好的解决方案吗?你不会产生不必要的流量吗?
【解决方案2】:

面临同样的问题。

对于那些使用 jQuery 的人,这是我的解决方案。

我意识到如果我们将 .addClass().removeClass() 串在一起,jQuery 会解释和否定这两个命令,导致没有任何变化。所以我建议使用超时功能的解决方法:

让动画超时

clearTimeout(动画超时)

$('#animatedElement').addClass('animation')

animationTimeout = setTimeout(() => {$('#animatedElement').removeClass('animation')}, animationDuration)

【讨论】:

    【解决方案3】:

    您应该在动画后删除 Hand 的样式(在脚本中添加 3 行):

    ...

    var myHand = false;

    deposit.addEventListener('click', depositCash);

    withdraw.addEventListener('click',withdrawCash);

    函数 depositCash() {

    if (myHand) clearTimeout(myHand);

    ...

    hand.style.animation = 'moveDown 1.5s 缓入出,fadeIn 1.5s 缓入出';

    余额 = 数量(余额)+ 数量(存款金额);

    document.getElementById('balance').value = balance;

    myHand = setTimeout(function(){hand.style.animation = '';}, 2000);

    }

    【讨论】:

      【解决方案4】:

      因为CSS animations don't automatically restart。特别是因为您没有定义时间循环,所以它只执行一次。

      一种方法是让您使用 .addClass('x').removeClass('x') 重新触发在类 X 上定义的动画。

      .addClass() 当然是 jQuery。你可以在 vanilla JS 中做同样的事情,例如,hand.className += 'my-animation';并按照下面的方法在方法的顶部重置。

      //ref: https://css-tricks.com/restart-css-animation/
      
      document.getElementById('balance').value = "1000"
      
      var balance = document.getElementById('balance').value;
      var deposit = document.getElementById('deposit');
      var withdraw = document.getElementById('withdraw');
      var hand = document.getElementById('hand');
      
      deposit.addEventListener('click', depositCash);
      withdraw.addEventListener('click', withdrawCash);
      
      function depositCash() {
        hand.className = 'randoImage';
        var depositAmt = prompt('How much would you like to deposit?');
      
        if(depositAmt != Number(depositAmt)) {
          return alert('Please enter a valid integer.');
        }
        else if (Number(depositAmt) < 0) {
          return alert('Please enter a positive integer.');
        }
      
        hand.className += ' my-animation';
        balance = Number(balance) + Number(depositAmt);
        document.getElementById('balance').value = balance;
      }
      
      function withdrawCash() {
        var withdrawAmt = prompt('How much you you like to withdraw?');
      
        if(withdrawAmt != Number(withdrawAmt)) {
          return alert('Please enter a valid integer.');
        }
        else if (Number(withdrawAmt) < 0) {
          return alert('Please enter a positive integer.');
        }
        else if(withdrawAmt > balance) {
          return alert("Your balance isn't large enough to withdraw that amount!")
        }
      
      
        balance = Number(balance) - Number(withdrawAmt);
        document.getElementById('balance').value = balance;
      }
      .randoImage {
        width: 25px;
        height: 25px;
        background-image: url(data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7);
      }
      
      * {
        margin:0px;
        padding:0px;
      }
      
      @keyframes moveDown {
        0% {}
        100% {margin-top:-220px;}
      }
      
      @keyframes fadeIn {
        0% {opacity:0;}
        90%{opacity:1}
        100%{opacity:0;}
      }
      
      #hand {
        height:300px;
        width:300px;
        position:absolute;
        left:50%;
        margin-left:-120px;
        margin-top:-350px;
        /*background-image:url("../images/hand.png");*/
        opacity:0;
      }
      
      #pigBox {
        margin-left:auto;
        margin-right:auto;
        height:600px;
        width:500px;
        margin-top:250px;
        position:relative;
      
        img {
          margin:0px 50px;
        }
      
      }
      
      input[type=text] {
        float:left;
        display:block;
        font-size:2em;
        width:500px;
        border-radius:10px;
        border:solid 2px pink;
        margin-top:10px;
        font-family: 'Gochi Hand', cursive;
        text-align:center;
        padding:2px;
      }
      
      #deposit {
        float:left;
        display:block;
        font-family: 'Gochi Hand', cursive;
        font-size:2em;
        clear:left;
        width:200px;
        margin:10px 25px;
        border-radius:10px;
        background-color:pink;
        border:solid 2px pink;
        padding:2px;
        cursor:pointer;
      
        &:hover {
          background-color:white;
        }
      }
      
      #withdraw {
        float:left;
        display:block;
        font-family: 'Gochi Hand', cursive;
        font-size:2em;
        width:200px;
        margin:10px 25px;
        border-radius:10px;
        background-color:pink;
        border:solid 2px pink;
        padding:2px;
        cursor:pointer;
      
        &:hover {
          background-color:white;
        }
      }
      
      label {
        text-align:center;
        display:block;
        font-family: 'Gochi Hand', cursive;
        font-size:2.5em;
        border-radius:10px;
        width:300px;
        margin-left:100px;
        margin-right:100px;
        margin-top:5px;
        margin-bottom:-15px;
      }
      
      .my-animation {
        animation: moveDown 1.5s ease-in-out, fadeIn 1.5s ease-in-out;
      }
      <section id="pigBox">
            <div class="randoImage" id="hand"></div><!-- end of hand-->
            <img class="randoImage" />
            <!--<img src="images/pig.png" />-->
            <label>Balance: </label><input type="text" id="balance" />
            <button id="deposit"> Deposit </button>
            <button id="withdraw"> Withdraw </button>
        </section><!-- end of pigBox-->

      【讨论】:

      • 您的解决方案只执行一次。使用 IE11。
      • 我认为已修复,请检查。现在不需要 jQuery!
      • 效果很好。你能详细说明一下逻辑吗?为什么 hand.className = randomImage;工作这么好?是不是因为它有些刷新手部div?
      • hand.className = 'randoImage';正在有效地删除“我的动画”类。下次添加“my-animation”时,它会重新触发动画。
      猜你喜欢
      • 1970-01-01
      • 2021-09-03
      • 2016-11-10
      • 1970-01-01
      • 1970-01-01
      • 2020-11-12
      • 2020-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多