改变不透明度使用css过渡和香草JavaScript只工作时淡出

Change of opacity using css transition and vanilla JavaScript works only when fading out

本文关键字:JavaScript 香草 工作 淡出 不透明度 css 改变      更新时间:2023-09-26

这个代码显示了我的问题:http://codepen.io/PiotrBerebecki/pen/pNvpdG

当用户点击大按钮时,css opacity减小为0。因为我已经应用了以下规则:transition: opacity 0.5s ease-in-out;的淡出动画是平滑的。

我想实现同样的平稳过渡时,下一个按钮淡入。然而,由于某些原因,下一个按钮突然出现,没有任何过渡。

你知道问题的原因和解决方法吗?

console.clear();
(function() {
  
  // Data for the app
  const model = {
    buttons: ['tomato', 'blue'],
    currentButton: -1
  };
  
  // Logic for the app
  const controller = {
    init: function() {
      view.init();
    },
    getButtonName: function() {
      model.currentButton = (model.currentButton + 1) % model.buttons.length;
      return model.buttons[model.currentButton];
    }
  };
  
  // View for the app
  const view = {
    init: function() {
      this.root = document.getElementById('root');
      this.showNext();
    },
    
    animationDelay: 500,
    
    showNext: function() {
      // Get next button name
      const buttonName = controller.getButtonName();
      
      // Create button DOM element
      const buttonElement = document.createElement('div');
      buttonElement.className = 'button';
      buttonElement.id = buttonName;
      buttonElement.textContent = buttonName;
      buttonElement.style.opacity = 0;
      
      // Add event listender for the button
      buttonElement.addEventListener('click', event => {
        // Reduce opacity
        buttonElement.style.opacity = 0;
        // Remove the button from DOM
        setTimeout(() => {
          this.root.removeChild(buttonElement);
        }, this.animationDelay + 10);
        // Start the function to show next button
        setTimeout(() => {
          this.showNext();
        }, this.animationDelay + 20);
      });
      
      // Add button to DOM
      this.root.appendChild(buttonElement);
      
      // Show button by increasing opacity
      buttonElement.style.opacity = 1;
      
    }
  };
  
  // Start the app
  controller.init();
}());
#tomato {
  background: tomato;
}
#blue {
  background: DeepSkyBlue;
}
.button {
  transition: opacity 0.5s ease-in-out;
  width: 100%;
  height: 50vh;
  border: solid 3px black;
  cursor: pointer;
}
<div id="root"></div>

应该可以了,代码笔链接:http://codepen.io/saa93/pen/gLbvmQ

你需要添加这个,而不是直接将不透明度设置为1

// Show button by increasing opacity
buttonElement.style.opacity = 0;
setTimeout(() => {
    buttonElement.style.opacity = 1;
}, this.animationDelay + 20);   

添加一个类(在代码片段中是.active),添加以下内容:

CSS

.button {
  opacity: 0;
  transition: opacity 0.5s ease-in-out;
  width: 100%;
  height: 50vh;
  border: solid 3px black;
  cursor: pointer;
}
.button.active {
  opacity: 1;
  transition: opacity 0.5s ease-in-out;
}
JavaScript

  ...
  // Reduce opacity
  buttonElement.classList.toggle('active');
  buttonElement.style.opacity = 0;
  ...
  // Show button by increasing opacity
  buttonElement.classList.toggle('active');
  buttonElement.style.opacity = 1;

片段

console.clear();
(function() {
  // Data for the app
  const model = {
    buttons: ['tomato', 'blue'],
    currentButton: -1
  };
  // Logig for the app
  const controller = {
    init: function() {
      view.init();
    },
    getButtonName: function() {
      model.currentButton = (model.currentButton + 1) % model.buttons.length;
      return model.buttons[model.currentButton];
    }
  };
  // View for the app
  const view = {
    init: function() {
      this.root = document.getElementById('root');
      this.showNext();
    },
    animationDelay: 500,
    showNext: function() {
      // Get next button name
      const buttonName = controller.getButtonName();
      // Create button DOM element
      const buttonElement = document.createElement('div');
      buttonElement.className = 'button';
      buttonElement.id = buttonName;
      buttonElement.textContent = buttonName;
      buttonElement.style.opacity = 0;
      // Add event listender for the button
      buttonElement.addEventListener('click', event => {
        // Reduce opacity
        buttonElement.classList.toggle('active');
        buttonElement.style.opacity = 0;
        // Remove the button from DOM
        setTimeout(() => {
          this.root.removeChild(buttonElement);
        }, this.animationDelay + 10);
        // Start the function to show next button
        setTimeout(() => {
          this.showNext();
        }, this.animationDelay + 20);
      });
      // Add button to DOM
      this.root.appendChild(buttonElement);
      // Show button by increasing opacity
      buttonElement.classList.toggle('active');
      buttonElement.style.opacity = 1;
    }
  };
  // Start the app
  controller.init();
}());
#tomato {
  background: tomato;
}
#blue {
  background: DeepSkyBlue;
}
.button {
  opacity: 0;
  transition: opacity 0.5s ease-in-out;
  width: 100%;
  height: 50vh;
  border: solid 3px black;
  cursor: pointer;
}
.button.active {
  opacity: 1;
  transition: opacity 0.5s ease-in-out;
}
<div id="root"></div>

after this.root.appendChild(buttonElement);

你应该将不透明度设置为0,并让浏览器在buttonElement.style.opacity = 1;之前进行渲染。

顺便说一句,我认为删除和添加元素不是一个好方法来做到这一点

.button {
  width: 100%;
  height: 50vh;
  border: solid 3px black;
  cursor: pointer;
   animation-name: example;
    animation-duration:3.5s;
}
@keyframes example {
        0%   {opacity:1}
        50%  {opacity:0}
    100%  {opacity:1}
}

你真正想要的是像这样使用动画:

这样,动画就可以使用css (

)来完成所有的计时和不透明度。