用css如何实现背景图片的自动替换?

html-css016

用css如何实现背景图片的自动替换?,第1张

哈哈,这几楼的回答都不太正确,css可以自己完成图片的自动切换,不需要什么js辅助。人家楼主问的如何用css来实现,你搞的那个什么用js来实现,答非所问,还弄个专业回答,逗子终结者?

css有个执行简单动画的写法,比如animation。

我给你个例子:

#head-pic,#head-pic2{

width:145pxheight:145pxposition:absolute

top:200px

left:68px

z-index:2

border-radius:100px

-webkit-border-radius:100px

-moz-border-radius:100px

border:3px solid #fff

}

/*为浏览器兼容设置*/

@keyframes headpic-slider{

from{background:url(../images/head-pic.jpg)opacity:1.0}

to{background:url(../images/head-pic.jpg)opacity:0}

}

@keyframes headpic-slider2{

from{background:url(../images/head-pic2.jpg)opacity:0}

to{background:url(../images/head-pic2.jpg)opacity:1.0}

}

@-moz-keyframes headpic-slider{

from{background:url(../images/head-pic.jpg)opacity:1.0}

to{background:url(../images/head-pic.jpg)opacity:0}

}

@-moz-keyframes headpic-slider2{

from{background:url(../images/head-pic2.jpg)opacity:0}

to{background:url(../images/head-pic.jpg)opacity:1.0}

}

@-webkit-keyframes headpic-slider{

from{background:url(../images/head-pic.jpg)opacity:1.0}

to{background:url(../images/head-pic.jpg)opacity:0}

}

@-webkit-keyframes headpic-slider2{

from{background:url(../images/head-pic2.jpg)opacity:0}

to{background:url(../images/head-pic2.jpg)opacity:1.0}

}

@-o-keyframes headpic-slider{

from{background:url(../images/head-pic.jpg)opacity:1.0}

to{background:url(../images/head-pic.jpg)opacity:0}

}

@-o-keyframes headpic-slider2{

from{background:url(../images/head-pic2.jpg)opacity:0}

to{background:url(../images/head-pic2.jpg)opacity:1.0}

}

#head-pic{

animation:headpic-slider 20s linear infinite

-moz-animation:headpic-slider 20s linear infinite

-webkit-animation:headpic-slider 20s linear infinite

}

#head-pic2{

animation:headpic-slider2 20s linear infinite

-moz-animation:headpic-slider2 20s linear infinite

-webkit-animation:headpic-slider2 20s linear infinite

}

<html>

<head>

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

<title>纯css实现图片相册幻灯片切换</title>

<style type="text/css">

*{ margin:0 padding:0 list-style:none}

.lanrenzhijia{ width:180px height:162px margin:100px auto}

.lanrenzhijia dl { position:relative width:160px height:142px border:10px solid #EFEFDA }

.lanrenzhijia dd { width:160px height:120pxoverflow:hidden}

.lanrenzhijia img { border:2px solid #000 }

.lanrenzhijia dt { position:absolute right:0px bottom:0pxwidth:160pxheight:22px background:#FBFBEA }

.lanrenzhijia a {display:blockfloat:rightmargin:2pxwidth:18px height:18pxtext-align:center color:#fff text-decoration:none background:#666filter:alpha(opacity=70) opacity:.7}

.lanrenzhijia a:hover {background:#000}

</style>

</head>

<body>

<div class="lanrenzhijia">

<dl>

    <dt>

        <a href="#indexa6">6</a><a href="#indexa5">5</a><a href="#indexa4">4</a><a href="#indexa3">3</a><a href="#indexa2">2</a><a href="#indexa1">1</a>

    </dt>

    <dd>

        <img id="indexa1" name="indexa1" src="images/图片1.jpg" />

        <img id="indexa2" name="indexa2" src="images/图片2.jpg" />

        <img id="indexa3" name="indexa3" src="images/图片3.jpg" />

        <img id="indexa4" name="indexa4" src="images/图片4.jpg" />

        <img id="indexa5" name="indexa5" src="images/图片5.jpg" />

        <img id="indexa6" name="indexa6" src="images/图片6.jpg" />

    </dd>

</dl>

</div>

</body>

</html>

参考资料:懒人之家