transition(过渡)) 是指从一个状态到另一个状态的变化。比如当鼠标在某个元素上悬停时,我们会修改它的样式,采用 transition 可以创建一个平滑的动画效果。
transition: background 0.5s linear;
<body>
<button id="show">Show it</button>
<div id="wait-show" class="hide">
<h1>Title</h1>
<p>Pressing the button shows this content.</p>
</div>
</body>
body {
background-color: #134;
transition: background 0.5s ease-out;
font-family: HelveticaNeue, Arial, Sans-serif;
}
body.on {
background-color: #099;
}
button{
background: transparent;
border: 2px solid #fff;
border-radius: 1em;
color: #fff;
cursor: pointer;
font-size: 24px;
padding: 1em 2em;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
transition: all 0.4s ease-out;
outline: 0;
}
button:hover{
background: #fff;
color: #099;
}
#wait-show{
color: #fff;
text-align: center;
position: absolute;
left: 50%;
transform: translate(-50%,-50%);
transition: all 0.5s cubic-bezier(.83,-0.43,.21,1.42);
}
.hide{
opacity: 0;
top:calc(50% + 10em);
}
.show{
opacity: 1;
top: calc(50% + 6em);
}
let showBtn = document.getElementById('show');
let body = document.body;
let waitShow = document.getElementById('wait-show');
/**
* 监听点击事件
*/
showBtn.addEventListener('click', function (e) {
// 更改body颜色
body.className = body.className === 'on' ? '' : 'on';
waitShow.className = waitShow.className === 'hide' ? 'show' : 'hide';
})
document.getElementsByTagName('body')