gpt4 book ai didi

javascript - 如何动画定位固定?

转载 作者:太空狗 更新时间:2023-10-29 14:52:47 25 4
gpt4 key购买 nike

我尝试为 1 秒后固定的 DIV 设置动画。但我做不到。我希望一秒钟后名为“homepage-hero-module”的 div 从右向左滑动。正如您在 FIDDLE 中看到的那样,它在一秒钟后变为 fixed。那么如何制作动画呢?

我试过使用 CSS,但没有成功。

-webkit-transition: left 1s;
-moz-transition: left 1s;
-o-transition: left 1s;
transition: left 1s;

-webkit-transition: all 0.5s ease;
-moz-transition: all 0.5s ease;
-o-transition: all 0.5s ease;
transition: all 0.5s ease;

JSFIDDLE

HTML 代码:

<div class="container-fluid">
<div class="homepage-hero-module">
Container with data
</div>
</div>

CSS 代码:

    body, html {
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
}
.container-fluid {
width: 100%;
height: 100%;
position: relative;
}
.homepage-hero-module {
background: #DDD;
width: 100%;
height: 100%;
position: absolute;
top: 0px;
left: 0px;
}
.fixed {
position: fixed;
top: 0px;
left: 0px;
width: 20px;
height: 100%;
background: red;
}
img {
height: 100%;
width: auto;
}

JS代码:

$(document).ready(function() {
setTimeout( function(){
$('.homepage-hero-module').addClass('fixed');
},1000);
});

最佳答案

您需要在位置仍然是绝对的情况下为宽度设置动画,然后将位置设置为固定

<div class="container-fluid">
<div class="homepage-hero-module">
Container with data
</div>
</div>

body, html {
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
}
.container-fluid {
width: 100%;
height: 100%;
position: relative;
}
.homepage-hero-module {
background: #DDD;
width: 100%;
height: 100%;
position: absolute;
top: 0px;
left: 0px;
transition:all .2s ease;
}
.fixed {
top: 0px;
left: 0px;
width: 20px;
height: 100%;
background: red;
}
img {
height: 100%;
width: auto;
}

$(document).ready(function() {
setTimeout( function(){
$('.homepage-hero-module').addClass('fixed');
},1000);
$('.homepage-hero-module').css('position','fixed');
});

关于javascript - 如何动画定位固定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38918319/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com