如何动画定位?

时间:2022-10-18 15:01:52

I try to animate a DIV that gets fixed after 1 second. But I can't make it done. I want after one second the div called "homepage-hero-module" to slide from right to left. As you can see in the FIDDLE it changes to fixed after one second. So How to animate this?

我试着给一个一秒后被修复的DIV设置动画。但我做不到。一秒钟后,我想要“主页-英雄模块”的div从右向左滑动。正如你在小提琴中看到的,它在一秒后会变为固定。那么,如何让它动起来呢?

I tried with css, but no luck.

我尝试了css,但没有运气。

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

and

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

JSFIDDLE

JSFIDDLE

HTML CODE:

HTML代码:

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

CSS CODE:

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 code:

JS代码:

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

3 个解决方案

#1


3  

You need to animate the width while position is still absolute, and then set the position to fixed

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

<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');
});   

#2


2  

Already working I guess, check the below snippet and let me know your feedback. Thanks!

我想已经开始工作了,请查看下面的代码片段,并让我知道您的反馈。谢谢!

$(document).ready(function() {
  setTimeout(function() {
    $('.homepage-hero-module').addClass('fixed');
  }, 1000);
});
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;
  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
}
img {
  height: 100%;
  width: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
  <div class="homepage-hero-module">
    Container with data
  </div>
</div>

#3


1  

You can do it just with CSS. Check CSS3 animation.

你可以用CSS来做。检查CSS3动画。

Live demo :

现场演示:

body,
html {
  margin: 0px;
  padding: 0px;
  width: 100%;
  min-height: 100%;
}
.container-fluid {
  width: 100%;
  height: 100%;
  position: relative;
}
.homepage-hero-module {
  width: 100%;
  height: 100vh;
  position: absolute;
  top: 0px;
  left: 0px;
  background-color: #f5f5f5;
  animation: slideleft 1s 0.3s ease-out forwards;
}
img {
  height: 100%;
  width: auto;
}
@keyframes slideleft {
  to {
    background: coral;
    width: 70px;
    position: fixed;
  }
}
<div class="container-fluid">
  <div class="homepage-hero-module">
    Container with data
  </div>
</div>

#1


3  

You need to animate the width while position is still absolute, and then set the position to fixed

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

<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');
});   

#2


2  

Already working I guess, check the below snippet and let me know your feedback. Thanks!

我想已经开始工作了,请查看下面的代码片段,并让我知道您的反馈。谢谢!

$(document).ready(function() {
  setTimeout(function() {
    $('.homepage-hero-module').addClass('fixed');
  }, 1000);
});
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;
  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
  -o-transition: all 0.5s ease;
  transition: all 0.5s ease;
}
img {
  height: 100%;
  width: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
  <div class="homepage-hero-module">
    Container with data
  </div>
</div>

#3


1  

You can do it just with CSS. Check CSS3 animation.

你可以用CSS来做。检查CSS3动画。

Live demo :

现场演示:

body,
html {
  margin: 0px;
  padding: 0px;
  width: 100%;
  min-height: 100%;
}
.container-fluid {
  width: 100%;
  height: 100%;
  position: relative;
}
.homepage-hero-module {
  width: 100%;
  height: 100vh;
  position: absolute;
  top: 0px;
  left: 0px;
  background-color: #f5f5f5;
  animation: slideleft 1s 0.3s ease-out forwards;
}
img {
  height: 100%;
  width: auto;
}
@keyframes slideleft {
  to {
    background: coral;
    width: 70px;
    position: fixed;
  }
}
<div class="container-fluid">
  <div class="homepage-hero-module">
    Container with data
  </div>
</div>