How to make fadeOut effect with pure JavaScript

后端 未结 5 560
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-29 01:59

I\'m trying to make fadeOut effect for a div with pure JavaScript.

This is what I\'m currently using:

//Imagin         


        
5条回答
  •  难免孤独
    2020-11-29 02:35

    In addition to the accepted answer, we now have WAAPI which basically adds animation API to JavaScript.

    For example,

    /**
     * @returns {Object}
    */
    function defaultFadeConfig() {
      return {      
          easing: 'linear', 
          iterations: 1, 
          direction: 'normal', 
          fill: 'forwards',
          delay: 0,
          endDelay: 0
        }  
    }
    
    /** 
     * @param {HTMLElement} el
     * @param {number} durationInMs
     * @param {Object} config
     * @returns {Promise}
     */
    async function fadeOut(el, durationInMs, config = defaultFadeConfig()) {  
      return new Promise((resolve, reject) => {         
        const animation = el.animate([
          { opacity: '1' },
          { opacity: '0', offset: 0.5 },
          { opacity: '0', offset: 1 }
        ], {duration: durationInMs, ...config});
        animation.onfinish = () => resolve();
      })
    }
    
    /** 
     * @param {HTMLElement} el
     * @param {number} durationInMs
     * @param {Object} config
     * @returns {Promise}
     */
    async function fadeIn(el, durationInMs, config = defaultFadeConfig()) {
      return new Promise((resolve) => {         
        const animation = el.animate([
          { opacity: '0' },
          { opacity: '0.5', offset: 0.5 },
          { opacity: '1', offset: 1 }
        ], {duration: durationInMs, ...config});
        animation.onfinish = () => resolve();
      });
    }
    
    window.addEventListener('load', async ()=> {
      const el = document.getElementById('el1');  
      for(const ipsum of "Neque porro quisquam est qui dolorem ipsum quia dolor \uD83D\uDE00".split(' ')) {
        await fadeOut(el, 1000);  
        el.innerText = ipsum;
        await fadeIn(el, 2000);
      }
    });
    .text-center {
      text-align: center;
    }

    ...

提交回复
热议问题