How to animate to position fixed?
Asked Answered
E

3

9

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?

I tried with css, but no luck.

-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

HTML CODE:

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

CSS CODE:

    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:

$(document).ready(function() {
    setTimeout( function(){
              $('.homepage-hero-module').addClass('fixed');
    },1000);
});    
Ency answered 12/8, 2016 at 12:45 Comment(1)
Any reason not to use jQuery in-built slide-in transition? api.jqueryui.com/slide-effectTeem
O
8

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');
});   
Olfactory answered 12/8, 2016 at 12:51 Comment(1)
@jessh Nice answer, You can set position in fixed class also without using jQueryCzar
R
4

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>
Raft answered 12/8, 2016 at 12:52 Comment(0)
F
4

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

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>
Frenchy answered 12/8, 2016 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.