Pio*_*cki 5 html javascript css opacity css-transitions
这个代码笔显示了我的问题:http ://codepen.io/PiotrBerebecki/pen/pNvpdG
当用户点击大按钮时,cssopacity减少到 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();
}());Run Code Online (Sandbox Code Playgroud)
#tomato {
background: tomato;
}
#blue {
background: DeepSkyBlue;
}
.button {
transition: opacity 0.5s ease-in-out;
width: 100%;
height: 50vh;
border: solid 3px black;
cursor: pointer;
}Run Code Online (Sandbox Code Playgroud)
<div id="root"></div>Run Code Online (Sandbox Code Playgroud)
这应该有效,代码笔链接:http://codepen.io/saa93/pen/gLbvmQ
您需要添加它而不是直接将不透明度设置为 1
// Show button by increasing opacity
buttonElement.style.opacity = 0;
setTimeout(() => {
buttonElement.style.opacity = 1;
}, this.animationDelay + 20);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10395 次 |
| 最近记录: |