我是靠谱客的博主 幸福铃铛,这篇文章主要介绍CSS3怎么给背景图片添加动态变色效果,现在分享给大家,希望可以做个参考。

在之前的文章《利用CSS3创建炫酷的三角背景图像》中,我们给大家介绍了一种创建炫酷三角背景图像的方法,感兴趣的朋友可以去了解一下~

而下面本文再给大家介绍一种创建炫酷背景图像方法,带大家了解一下如何利用CSS3创建变色背景图像动画,让你的网页更吸引人!

我们先来看看效果图

1.gif

下面我们来研究一下是怎么实现这个效果的:

首先我们不创建标签,直接在body标签上设置背景图片

复制代码
1
2
3
4
5
6
7
body { background-image: url("https://img.uoften.com/upload/article/000/000/024/612360451cede816.jpg"); background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-position: center; }
登录后复制

1.png

怎么将图片变色呢?这就需要在背景图片上添加一个颜色层作为覆盖层,这个可以利用linear-gradient()函数实现:

复制代码
1
2
3
background-image: linear-gradient(4deg, rgba(0,255,254,0.3) 50%, rgba(0,255,254,0.3) 100%), url("https://img.uoften.com/upload/article/000/000/024/612360451cede816.jpg");
登录后复制

3.png

此时这个还是静态效果,怎么实现不断变色的动态效果?我们可以利用@keyframes和animation属性来实现--添加动画效果:

  • 利用animation属性设置动画名称、播放时间、播放次数等:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
body { background-image: url("https://img.uoften.com/upload/article/000/000/024/612360451cede816.jpg"); background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-position: center; animation-name: background-overlay-animation; animation-duration: 5s; animation-iteration-count: infinite; animation-direction: alternate; animation-timing-function: linear; }
登录后复制
  • 利用@keyframes定义每一帧动画:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@keyframes background-overlay-animation { 0% { background-image: linear-gradient(4deg, rgba(255,78,36,0.3) 50%, rgba(255,78,36,0.3) 100%), url("https://img.uoften.com/upload/article/000/000/024/612360451cede816.jpg"); } 25% { background-image: linear-gradient(4deg, rgba(213,49,127,0.3) 50%, rgba(213,49,127,0.3) 100%), url("https://img.uoften.com/upload/article/000/000/024/612360451cede816.jpg"); } 50% { background-image: linear-gradient(4deg, rgba(36,182,255,0.3) 50%, rgba(36,182,255,1) 100%), url("https://img.uoften.com/upload/article/000/000/024/612360451cede816.jpg"); } 100% { background-image: linear-gradient(4deg, rgba(0,255,254,0.3) 50%, rgba(0,255,254,0.3) 100%), url("https://img.uoften.com/upload/article/000/000/024/612360451cede816.jpg"); } }
登录后复制

下面给出完整代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <style> @keyframes background-overlay-animation { 0% { background-image: linear-gradient(4deg, rgba(255,78,36,0.3) 50%, rgba(255,78,36,0.3) 100%), url("https://img.uoften.com/upload/article/000/000/024/612360451cede816.jpg"); } 25% { background-image: linear-gradient(4deg, rgba(213,49,127,0.3) 50%, rgba(213,49,127,0.3) 100%), url("https://img.uoften.com/upload/article/000/000/024/612360451cede816.jpg"); } 50% { background-image: linear-gradient(4deg, rgba(36,182,255,0.3) 50%, rgba(36,182,255,1) 100%), url("https://img.uoften.com/upload/article/000/000/024/612360451cede816.jpg"); } 100% { background-image: linear-gradient(4deg, rgba(0,255,254,0.3) 50%, rgba(0,255,254,0.3) 100%), url("https://img.uoften.com/upload/article/000/000/024/612360451cede816.jpg"); } } @-webkit-keyframes background-overlay-animation { /* 兼容谷歌浏览器*/ 0% { background-image: linear-gradient(4deg, rgba(255,78,36,0.3) 50%, rgba(255,78,36,0.3) 100%) url("https://img.uoften.com/upload/article/000/000/024/612360451cede816.jpg"); } 25% { background-image: linear-gradient(4deg, rgba(213,49,127,0.3) 50%, rgba(213,49,127,0.3) 100%), url("https://img.uoften.com/upload/article/000/000/024/612360451cede816.jpg"); } 50% { background-image: linear-gradient(4deg, rgba(36,182,255,0.3) 50%, rgba(36,182,255,1) 100%), url("https://img.uoften.com/upload/article/000/000/024/612360451cede816.jpg"); } 100% { background-image: linear-gradient(4deg, rgba(0,255,254,0.3) 50%, rgba(0,255,254,0.3) 100%), url("https://img.uoften.com/upload/article/000/000/024/612360451cede816.jpg"); } } body { background-image: url("https://img.uoften.com/upload/article/000/000/024/612360451cede816.jpg"); background-size: cover; background-repeat: no-repeat; background-attachment: fixed; background-position: center; animation-name: background-overlay-animation; animation-duration: 5s; animation-iteration-count: infinite; animation-direction: alternate; animation-timing-function: linear; } </style> </head> <body> <!-- 你的内容放在这里 --> </body> </html>
登录后复制

靠谱客平台有非常多的视频教学资源,欢迎大家学习《css视频教程》!

以上就是CSS3怎么给背景图片添加动态变色效果的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是幸福铃铛最近收集整理的关于CSS3怎么给背景图片添加动态变色效果的全部内容,更多相关CSS3怎么给背景图片添加动态变色效果内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(150)

评论列表共有 0 条评论

立即
投稿
返回
顶部