【问题标题】:How to animate to position fixed?如何动画到固定位置?
【发布时间】:2016-12-19 11:43:55
【问题描述】:

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

我尝试使用 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);
});    

【问题讨论】:

标签: javascript jquery html css css-position


【解决方案1】:

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

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

【讨论】:

  • @jessh 不错的答案,您也可以在不使用 jQuery 的情况下在固定类中设置位置
【解决方案2】:

我想已经可以使用了,请检查下面的 sn-p 并让我知道您的反馈。谢谢!

$(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】:

    您只需使用 CSS 即可。检查 CSS3 animation

    现场演示:

    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>

    【讨论】:

      猜你喜欢
      • 2020-12-11
      • 1970-01-01
      • 2012-10-19
      • 1970-01-01
      • 2020-03-24
      • 2015-05-16
      • 1970-01-01
      • 2013-09-22
      • 2016-10-07
      相关资源
      最近更新 更多