
.panel{
z-index: 1;
position: sticky;
top: 0;
transition:
margin-bottom 1s,
transform 1s;
}
.hero {
position: relative;
}
*{
margin:0;
padding:0;
}
html {--smokeGrey:#eee}
.notif-panel{
display:grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
grid-template-areas: "panel-content";
}
.panel-content{
display:flex;
justify-content:center;
align-items:center;
background-color:var(--smokeGrey);
}
.panel-text{
display:inline;
}
.cookie, .privacy, .tos{
text-decoration:none;
}
.panel-button{
display:inline;
padding: 10px 16px;
background-color: #007bc1;
color: white;
border: none;
border-radius: 2px;
}
.panel-button:hover{
background-image: linear-gradient(rgba(0, 0, 0, 0.1) 0 0);
}
@media(max-width:675px){
.notif-panel{
height: 10%;
}
.panel-content{
padding: 0 5px;
}
.panel-text{
padding-right: 15px;
}
.panel-button{
font-size: 17px;
}
}
@media(max-width:605px){
.notif-panel{
height: 15%;
}
.panel-content{
align-items: left;
display: block;
padding: 10px;
font-size: 18px;
}
.panel-text{
padding-bottom: 10px;
display: block;
}
.panel-button{
font-size: 17px;
}
}
.main-page{
width: 100%;
}
.hero{
display: flex;
justify-content: center;
align-items: center;
background-image: linear-gradient(rgba(0,74,117,.52),rgba(0,74,117,.52)), url('assets/work-desk__dustin-lee.jpg');
height: 600px;
}
.logo{
height: 50px;
width: 50px;
position: absolute;
left: 30px;
top: 25px;
}
.hero-content{
margin:0;
text-align: center;
color: white;
}
.name{
font-size: 30px;
}
.contact-text{
margin-top: 8px;
padding-bottom: 25px;
}
.contact-button{
font-weight: bold;
color: white;
background-color: transparent;
border: white 2px solid;
padding: 12px 15px;
border-radius: 2px;
}
.contact-button:hover{
color: var(--blue);
background-color: white;
}const panel = document.getElementById('panel');
const page = document.getElementById('main-page');
function closePanel(){
const {bottom} = panel.getBoundingClientRect();
panel.style.marginBottom = 0 + "px";
panel.addEventListener("transitionend", () => panel.replaceWith());
// marginBottom shrinks the element;
// translateY() slides it out.
setTimeout(() => {
panel.style.marginBottom = `${-bottom}px`;
panel.style.transform = `translateY(${-bottom}px)`;
}, 0);
}注意事项:
CSS Transitions 允许您在 CSS 属性值发生变化时,平滑地过渡到新的值。通过精确控制滑出面板和页面主体的 top 属性,可以实现同步的动画效果。
代码示例:
<div class="panel" id="panel">
<div class="notif-panel" id="notifPanel">
<div class="panel-content">
<p class="panel-text">By accessing and using this website, you acknowledge that you have read and
<span class="brCust" id="brCust"></span>understand our
<a class="cookie" href="#">Cookie Policy</a>,
<a class="privacy" href="#">Privacy Policy</a>, and our
<a class="tos" href="#">Terms of Service</a>.
</p>
<button class="panel-button" onclick="closePanel()">Got it</button>
</div>
</div>
</div>
<div class="main-page" id="main-page">
<section class="hero" id="hero">
<img src="assets/y-logo-white.png" class="logo">
<div class="hero-content">
<p class="name">Hello! I'm Dylan Anderton</p>
<h2 class="slogan">Consult, Design, and Develop Websites</h2>
<p class="contact-text">Have something great in mind? Feel free to contact me.<br> I'll help you to make it happen.</p>
<button class="contact-button">LET'S MAKE CONTACT</button>
</div>
</section>
</div>/* I had to move position:fixed to this top-level element */
.panel{
z-index: 1;
position: fixed;
}
*{
margin:0;
padding:0;
}
html {--smokeGrey:#eee}
.notif-panel{
display:grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
grid-template-areas: "panel-content";
}
.panel-content{
display:flex;
justify-content:center;
align-items:center;
background-color:var(--smokeGrey);
}
.panel-text{
display:inline;
}
.cookie, .privacy, .tos{
text-decoration:none;
}
.panel-button{
display:inline;
padding: 10px 16px;
background-color: #007bc1;
color: white;
border: none;
border-radius: 2px;
}
.panel-button:hover{
background-image: linear-gradient(rgba(0, 0, 0, 0.1) 0 0);
}
@media(max-width:675px){
.notif-panel{
height: 10%;
}
.panel-content{
padding: 0 5px;
}
.panel-text{
padding-right: 15px;
}
.panel-button{
font-size: 17px;
}
}
@media(max-width:605px){
.notif-panel{
height: 15%;
}
.panel-content{
align-items: left;
display: block;
padding: 10px;
font-size: 18px;
}
.panel-text{
padding-bottom: 10px;
display: block;
}
.panel-button{
font-size: 17px;
}
}
.main-page{
position: absolute;
width: 100%;
}
.hero{
display: flex;
justify-content: center;
align-items: center;
background-image: linear-gradient(rgba(0,74,117,.52),rgba(0,74,117,.52)), url('assets/work-desk__dustin-lee.jpg');
height: 600px;
}
.logo{
height: 50px;
width: 50px;
position: absolute;
left: 30px;
top: 25px;
}
.hero-content{
margin:0;
text-align: center;
color: white;
}
.name{
font-size: 30px;
}
.contact-text{
margin-top: 8px;
padding-bottom: 25px;
}
.contact-button{
font-weight: bold;
color: white;
background-color: transparent;
border: white 2px solid;
padding: 12px 15px;
border-radius: 2px;
}
.contact-button:hover{
color: var(--blue);
background-color: white;
}const panel = document.getElementById('panel');
const page = document.getElementById('main-page');
function closePanel(){
const rectPanel = panel.getBoundingClientRect();
panel.style.top = 0;
page.style.top = rectPanel.bottom + "px";
panel.style.transition = "top 1s ease-in-out";
page.style.transition = "top 1s ease-in-out";
// Don't forget to remove it from the DOM
panel.addEventListener("transitionend", () => panel.replaceWith());
setTimeout(() => {
panel.style.top = -rectPanel.bottom + "px";
page.style.top = 0;
}, 0);
}代码解释:
注意事项:
Web Animations API 提供了更强大的动画控制能力,允许您使用 JavaScript 创建复杂的动画效果。
代码示例:
<div class="panel" id="panel">
<div class="notif-panel" id="notifPanel">
<div class="panel-content">
<p class="panel-text">By accessing and using this website, you acknowledge that you have read and
<span class="brCust" id="brCust"></span>understand our
<a class="cookie" href="#">Cookie Policy</a>,
<a class="privacy" href="#">Privacy Policy</a>, and our
<a class="tos" href="#">Terms of Service</a>.
</p>
<button class="panel-button" onclick="closePanel()">Got it</button>
</div>
</div>
</div>
<div class="main-page" id="main-page">
<section class="hero" id="hero">
<img src="assets/y-logo-white.png" class="logo">
<div class="hero-content">
<p class="name">Hello! I'm Dylan Anderton</p>
<h2 class="slogan">Consult, Design, and Develop Websites</h2>
<p class="contact-text">Have something great in mind? Feel free to contact me.<br> I'll help you to make it happen.</p>
<button class="contact-button">LET'S MAKE CONTACT</button>
</div>
</section>
</div>/* I had to move position:fixed to this top-level element */
.panel{
z-index: 1;
position: fixed;
}
*{
margin:0;
padding:0;
}
html {--smokeGrey:#eee}
.notif-panel{
display:grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr;
grid-template-areas: "panel-content";
}
.panel-content{
display:flex;
justify-content:center;
align-items:center;
background-color:var(--smokeGrey);
}
.panel-text{
display:inline;
}
.cookie, .privacy, .tos{
text-decoration:none;
}
.panel-button{
display:inline;
padding: 10px 16px;
background-color: #007bc1;
color: white;
border: none;
border-radius: 2px;
}
.panel-button:hover{
background-image: linear-gradient(rgba(0, 0, 0, 0.1) 0 0);
}
@media(max-width:675px){
.notif-panel{
height: 10%;
}
.panel-content{
padding: 0 5px;
}
.panel-text{
padding-right: 15px;
}
.panel-button{
font-size: 17px;
}
}
@media(max-width:605px){
.notif-panel{
height: 15%;
}
.panel-content{
align-items: left;
display: block;
padding: 10px;
font-size: 18px;
}
.panel-text{
padding-bottom: 10px;
display: block;
}
.panel-button{
font-size: 17px;
}
}
.main-page{
position: absolute;
width: 100%;
}
.hero{
display: flex;
justify-content: center;
align-items: center;
background-image: linear-gradient(rgba(0,74,117,.52),rgba(0,74,117,.52)), url('assets/work-desk__dustin-lee.jpg');
height: 600px;
}
.logo{
height: 50px;
width: 50px;
position: absolute;
left: 30px;
top: 25px;
}
.hero-content{
margin:0;
text-align: center;
color: white;
}
.name{
font-size: 30px;
}
.contact-text{
margin-top: 8px;
padding-bottom: 25px;
}
.contact-button{
font-weight: bold;
color: white;
background-color: transparent;
border: white 2px solid;
padding: 12px 15px;
border-radius: 2px;
}
.contact-button:hover{
color: var(--blue);
background-color: white;
}const panel = document.getElementById('panel');
const page = document.getElementById('main-page');
function closePanel(){
const {bottom} = panel.getBoundingClientRect();
const duration = 1000; // ms
panel.animate(
{ top: [0, `${-bottom}px`] },
{ duration }
).finished.then(() => {
panel.replaceWith(); // Don't forget to remove element from DOM!
});
page.animate(
{ top: [`${bottom}px`, 0] },
{ duration }
);
}代码解释:
注意事项:
本文介绍了三种解决滑出动画不平滑问题的方法:使用 position: sticky、CSS Transitions 和 Web Animations API。每种方法都有其优缺点,您可以根据实际需求选择最合适的方案。
无论选择哪种方案,都需要确保动画同步,避免视觉上的不协调,从而提升用户体验。此外,还应注意代码的兼容性,确保在不同浏览器下都能正常运行。
以上就是实现平滑滑出动画效果:优化页面元素过渡的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号