简体   繁体   English

如何动态更改div的内容而不在js中闪烁

[英]how to Dynamically change the contents of the div without flickering in js

Welcome, well, I have 10 lottie animations, and I want to show them one by one in the same div.欢迎,好吧,我有10个lottie动画,我想在同一个div中一个一个地展示。 When a certain button is pressed, I do a function for each animation and at the beginning of the function I remove all the contents of the div to display the new content, but as is clear This causes flickering, I was wondering if there is a smarter method that does not cause flicker, please note that I am considered a beginner in website development当按下某个按钮时,我为每个 animation 执行 function 并且在 function 的开头,我删除了 div 的所有内容以显示新内容,但是否清晰显示不会导致闪烁的更聪明的方法,请注意我被认为是网站开发的初学者

here is a link http://animationstest.atwebpages.com/ , use W,A,D,S to move, It is not responsive so use a pc这是一个链接http://animationstest.atwebpages.com/ ,使用 W,A,D,S 移动,它没有响应所以使用电脑

But notice that when you quickly press a and then d for example, Flickr does not happen但是请注意,例如,当您快速按 a 然后按 d 时,Flickr 不会发生

    <script>
    var animation = bodymovin.loadAnimation({
      container: document.getElementById('bm'),
      renderer: 'svg',
      loop: false,
      autoplay: true,
      path: 'idle.json',
  
  });


    var state = "idle";
    document.onkeydown = function(e){
    e = e || window.event;
    var key = e.which || e.keyCode;
    if(state === "idle"){
      if(key===65){
          leftmove();
     }
     if(key===68){
          rightmove();
     }
     if(key===87){
          upmove();
     }
     if(key===83){
          downmove();
     }
    }
    if(state === "leftmove"){
      if(key===68){
       leftback();
     }
    }
    if(state === "rightmove"){
      if(key===65){
          rightback();
     }
    }
    if(state === "upmove"){
      if(key===83){
          upback();
     }
    }
    if(state === "downmoved"){
      if(key===87){
          downback();
     }
    }

   }

    function leftmove(){
        document.getElementById('bm').innerHTML = "";
     animation = bodymovin.loadAnimation({
      container: document.getElementById('bm'),
      renderer: 'svg',
      loop: false,
      autoplay: true,
      path: 'leftmove.json',
  
    });
    state = "leftmove";
    }

    function leftback(){
        document.getElementById('bm').innerHTML = "";
       animation = bodymovin.loadAnimation({
      container: document.getElementById('bm'),
      renderer: 'svg',
      loop: false,
      autoplay: true,
      path: 'leftback.json',
  
    });
    state = "idle";
    }

    function rightmove(){
        document.getElementById('bm').innerHTML = "";
       animation = bodymovin.loadAnimation({
      container: document.getElementById('bm'),
      renderer: 'svg',
      loop: false,
      autoplay: true,
      path: 'rightmove.json',
  
    });
    state = "rightmove";
    }
    function rightback(){
        document.getElementById('bm').innerHTML = "";
      animation = bodymovin.loadAnimation({
      container: document.getElementById('bm'),
      renderer: 'svg',
      loop: false,
      autoplay: true,
      path: 'rightback.json',
  
    });
    state = "idle";
    }

    function upmove(){
        document.getElementById('bm').innerHTML = "";
       animation = bodymovin.loadAnimation({
      container: document.getElementById('bm'),
      renderer: 'svg',
      loop: false,
      autoplay: true,
      path: 'upmave.json',
  
    });
    state = "upmove";
    }
    function upback(){
        document.getElementById('bm').innerHTML = "";
       animation = bodymovin.loadAnimation({
      container: document.getElementById('bm'),
      renderer: 'svg',
      loop: false,
      autoplay: true,
      path: 'upback.json',
  
    });
    state = "idle";
    }

    function downmove(){
        document.getElementById('bm').innerHTML = "";
       animation = bodymovin.loadAnimation({
      container: document.getElementById('bm'),
      renderer: 'svg',
      loop: false,
      autoplay: true,
      path: 'downmove.json',
  
    });
    state = "downmoved";
    }
    function downback(){
      document.getElementById('bm').innerHTML = "";
      animation = bodymovin.loadAnimation({
      container: document.getElementById('bm'),
      renderer: 'svg',
      loop: false,
      autoplay: true,
      path: 'downback.json',
  
    });
    state = "idle";
    }
  </script>

I was able to find another way to do what I was doing better, faster and more intelligent, for those who care, I'll explain what I did, simply what I was trying to do is to view a simple svg on the basis of the button that is pressed so I had to delete the old svg and view a new svg every time the user presses W, S, A, D on the keyboard But this Take it off and put it in causes Flickr and in people with slow internet speeds, switching to the new svg may be delayed more than a second,我能够找到另一种方法来做我正在做的事情,更好,更快,更智能,对于那些关心的人,我会解释我做了什么,只是我想做的是查看一个简单的 svg 基于按下的按钮,所以我不得不删除旧的 svg 并在每次用户按键盘上的 W、S、A、D 时查看新的 svg 但这将其取下并放入会导致 Flickr 和互联网速度较慢的人速度,切换到新的 svg 可能会延迟超过一秒,

What i did is that i used an js animation engine, which can animate your elements by using simple code, the best one I've found is: https://github.com/juliangarnier/anime我所做的是我使用了一个 js animation 引擎,它可以通过使用简单的代码来动画你的元素,我发现最好的一个是: https://github.com/juliangarnier/anime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM