简体   繁体   English

改变背景颜色的高度

[英]Background color changing it's height onload

I've been reading for couple hours from now, and probably I have problem even with asking right question. 从现在开始我已经阅读了几个小时,即使提出正确的问题,我也可能有问题。 What I would like to achieve is: changing the main page background height from 0 to 100% in let's say 10s, so after 1 second background color got 10% page height, after 2 seconds 20% of page height and so on. 我要实现的是:在10秒的时间内将主页背景高度从0更改为100%,因此1秒后背景色获得10%的页面高度,2秒后获得20%的页面高度,依此类推。 It should start to change after page load. 页面加载后应该开始更改。 It could be jquery, css, or some external library, just want it to work. 它可能是jquery,css或某些外部库,只是希望它能工作。

Something like this? 像这样吗

body {
  width: 100vw;
  height: 100vh;
  position: relative;
  &::before {
    content: "";
    display: block;
    position: fixed;
    width: 100%;
    background: red;
    height: 0;
    z-index: 0;
    animation-fill-mode: forwards;
    animation-name: expand;
    animation-duration: 10s;
  }
}

@keyframes expand {
    from { height: 0; }
    to { height: 100%; }
}

So basically, you add another layer underneath your content, which animates to the height of your page for 10s (the animation-duration) 因此,基本上,您在内容下方添加了另一层,该层将动画显示页面的高度10秒钟(动画持续时间)

Working JSFiddle: https://jsfiddle.net/y4kpcmjc/2/ 工作的JSFiddle: https ://jsfiddle.net/y4kpcmjc/2/

From the official jquery docs 来自官方的jQuery文档

The .animate() method allows us to create animation effects on any numeric CSS property. .animate()方法允许我们在任何数字CSS属性上创建动画效果。 The only required parameter is a plain object of CSS properties. 唯一需要的参数是CSS属性的普通对象。 This object is similar to the one that can be sent to the .css() method, except that the range of properties is more restrictive. 该对象与可以发送给.css()方法的对象类似,不同之处在于属性的范围更具限制性。

Here is the example 这是例子

<img id="image" src="..." />
...
<script>
$(document).ready(function() {
    $('#image').css('height', '0');
    $('#image').animate({
        height: "100%"
    }, 10000, function() {
       //Finished loading
    });

})
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM