使用vue实现一个没有卡顿的动画

    Tip:animation-play-state技术要点

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
    7. <title>Document</title>
    8. <script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script>
    9. <style>
    10. .demo{
    11. width:100px;
    12. height: 100px;
    13. background: pink;
    14. animation: circle 5s linear infinite;
    15. }
    16. .running{
    17. animation-play-state: running;
    18. }
    19. .paused{
    20. animation-play-state:paused;
    21. }
    22. @keyframes circle{
    23. 0%{
    24. transform: rotate(0deg)
    25. }
    26. 50%{
    27. transform: rotate(180deg)
    28. }
    29. 100%{
    30. transform: rotate(360deg);
    31. }
    32. }
    33. </style>
    34. </head>
    35. <body>
    36. <div id="app">
    37. <div :class="['demo',isPlay?'running':'paused']" >
    38. </div>
    39. <button @click="handleClick" style="margin-top: 20px;">切换</button>
    40. </div>
    41. <script>
    42. new Vue({
    43. el:"#app",
    44. data:{
    45. isPlay:false
    46. },
    47. methods:{
    48. handleClick(){
    49. if(this.isPlay){
    50. this.isPlay = false;
    51. }else{
    52. this.isPlay = true;
    53. }
    54. }
    55. }
    56. })
    57. </script>
    58. </body>
    59. </html>