如何CSS实现网页背景三种颜色渐变效果?

html-css011

如何CSS实现网页背景三种颜色渐变效果?,第1张

页面背景颜色渐变可以分为四个部分

一、从上往下渐变:

body{

FILTER: progid:DXImageTransform.Microsoft.Gradient(gradientType=0,startColorStr=#ffffff,endColorStr=#000000)

}

二、从左上至右下渐变:

body{

FILTER: Alpha( style=1,opacity=25,finishOpacity=100,

startX=50,finishX= 100,startY=50,finishY=100)

background-color: skyblue

}

三、从左往右渐变:

body{

FILTER: progid:DXImageTransform.Microsoft.Gradient(gradientType=1,startColorStr=#ffffff,endColorStr=#000000)

}

四、从上往下渐变:

style="filter:progid:DXImageTransform.microsoft.gradient(gradienttype=0,startColorStr=blue,endColorStr=white)"

下面是整合的完整格式:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html charset=gb2312" />

<title>背景渐变</title>

<style type="text/css">

<!--

body {

margin-left: 0px

margin-top: 0px

margin-right: 0px

margin-bottom: 0px

}

-->

</style></head>

<body>

<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">

<tr>

<td height="600" style="filter:progid:DXImageTransform.microsoft.gradient(gradienttype=0,startColorStr=blue,endColorStr=white)">&nbsp</td>

</tr>

</table>

</body>

</html>

如果是在同一个页面里面显示多重渐变效果,可以定义每个渐变的width和height。

css问题filter: alpha(opacity=100,finishopacity=0,style=2)

alpha是来设置透明度的,它的基本属性是filter:alpha(opacity,finishopacity,

style,startX,startY,finishX,finishY).

opacity代表透明度数,选值0-100,0是完全透明,100是不透明.

finishopacity用来设置结束时的透明度,以达到渐变效果.取值范围也是0-100.style指渐变类型,0是无变化,1是线行渐变,2是放射渐变,3是X型渐变.

效果解析

我们分析一下实现这个效果需要实现的功能:

实现一个扫光背景块,因为光是移动的,所以要加入渐变效果 (例如: 手电筒照射的一小块区域)

将扫光背景块控制到文本上 (即实现文本背景)

实现扫光动画 (扫光块从左往右循环移动)

思路理清了,接下来就是一步一步实现了

背景渐变 -webkit-linear-gradient (形成扫光背景块)

background: #111 -webkit-linear-gradient(left, #111, #fff) 0 0 no-repeat

background-size: 80px

默认背景为黑色,从左往右产生黑白渐变效果,背景位于左上角,不重复,背景大小80x80 (扫光背景块)

背景范围 -webkit-background-clip (实现文本背景)

为了实现文字扫光,我们必须把上一步形成的扫光背景块,控制在文字上面

background-clip 有三个属性: border-box, padding-box, content-box (具体效果不细说了)

显然这些属性并不能满足我们的这个需求,并没有可以控制文字背景的属性

但是在 webkit 下 background-clip 就有这样一个属性,那就是 background-clip: text 通常会配合其私有属性 -webkit-text-fill-color (填充文本颜色) 一起使用

背景动画 (扫光动画)

@-webkit-keyframes slideShine {

0% {

background-position: 0 0

}

100% {

background-position: 100% 100%

}

}

通过调整背景的位置(从左往右),来达到扫动的效果

效果实例

CSS Code

.bg {

background: #000

width: 1000px

height: 300px

margin: 0 auto

padding-top: 100px

}

.slideShine {

width: 1000px

font-family: "Microsoft YaHei"

font-size: 60px

text-align: center

background: #111 -webkit-linear-gradient(left, #111, #fff) 0 0 no-repeat

-webkit-background-size: 80px

-webkit-background-clip: text

-webkit-text-fill-color: rgba(255, 255, 255, 0.3)

-webkit-animation: slideShine 3s infinite

}

@-webkit-keyframes slideShine {

0% {

background-position: 0 0

}

100% {

background-position: 100% 100%

}

}

HTML Code

<div class="bg">

<p class="slideShine">Welcome to xinpureZhu Blog</p>

</div>

效果示图