css设置背景图片随滚动缓慢滚动_(CSS)使背景图像滚动比一切更慢

here is is my CSS code for the body:

body {

padding: 0px;

margin: 0px;

background-image: url("../images/background.jpg");

background-repeat: no-repeat;

background-color: grey;

background-size: 100%;

}

What I want to do is make it so that the image scrolls slower than everything else on the page to make a simple parallax effect. I've looked online and all of the examples I've seen are much more complicated than what I want.

解决方案

I stumbled upon this looking for more flexibility in my parallax speed that I have created with pure CSS and I just want to point out that all these people are wrong and it is possible with pure CSS It is also possible to control the height of your element better.

You will probably have to edit your DOM/HTML a bit to have some container elements, in your case you are applying the background to the body which will restrict you a lot and doesn't seem like a good idea.

Here is how you control the height with Viewport-percentage lenghts based on screen size:

.forefront-element {

-webkit-transform: translateZ(999px) scale(.7);

transform: translateZ(999px) scale(.7);

z-index: 1;

}

.base-element {

-webkit-transform: translateZ(0);

transform: translateZ(0);

z-index: 4;

}

.background-element {

-webkit-transform: translateZ(-999px) scale(2);

transform: translateZ(-999px) scale(2);

z-index: 3;

}

Layer speed is controlled by a combination of the perspective and the Z translation values. Elements with negative Z values will scroll slower than those with a positive value. The further the value is from 0 the more pronounced the parallax effect (i.e. translateZ(-10px) will scroll slower than translateZ(-1px)).

Here is a demo I found with a google search because I know there are a lot of non-believers out there, never say impossible:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值