简体   繁体   English

仅在一个方向上滚动背景

[英]scrolling background in one direction only

All I want to have a background scrolling effect from bottom to top ,but I dont know how to do so. 我只想从下到上具有背景滚动效果,但是我不知道该怎么做。
I have tried it using CSS ,but the problem arises here is it scroll in both direction ie bottom to top and top to bottom. 我已经尝试过使用CSS,但是这里出现的问题是它在两个方向上滚动,即从下到上和从上到下。

 * { margin: 0; padding: 0; } div { width: 100vw; height: 100vh; animation: change 2s infinite ease-in both; background-image: url(http://hdwallpaperbackgrounds.net/wp-content/uploads/2016/07/background-pictures-2.jpg); background-size: cover; background-repeat: no-repeat; } @keyframes change { 0%, 100% { background-position: 0% 5%; } 5% { background-position: 0% 10% } 10% { background-position: 0% 15% } 15% { background-position: 0% 20% } 20% { background-position: 0% 25% } 25% { background-position: 0% 30%; } 30% { background-position: 0% 35% } 35% { background-position: 0% 40% } 40% { background-position: 0% 45% } 45% { background-position: 0% 50% } } 
 <div> <h1> This is animating background ...</h1> </div> 

You can achieve that by just using the to and from attributes for the keyframes change. 您可以通过使用关键帧更改的tofrom属性来实现。 The effect is kind of weird, because the background top and bottom do not fit together... 效果有点怪异,因为背景的顶部和底部并不吻合...

 * { margin: 0; padding: 0; } div { width: 100vw; height: 100vh; animation: change 3s linear infinite; background-image: url(http://hdwallpaperbackgrounds.net/wp-content/uploads/2016/07/background-pictures-2.jpg); background-size: cover; background-repeat: repeat-y; background-position: 0 0; } @keyframes change { from { background-position: 0 100vh; } to { background-position: 0 0; } } 
 <div> <h1> This is animating background ...</h1> </div> 

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

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