<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>
参考资料:懒人之家
图片切换特效实现很简单,而且兼容性很好。html页面如下
复制代码 代码如下:
<div class="wrapper">
<div id="focus">
<ul>
<li><a href="http://www.lanrentuku.com/" target="_blank"><img src="img/01.jpg" alt="QQ商城焦点图效果下载" /></a></li>
<li><a href="http://www.lanrentuku.com/" target="_blank"><img src="img/02.jpg" alt="QQ商城焦点图效果教程" /></a></li>
<li><a href="http://www.lanrentuku.com/" target="_blank"><img src="img/03.jpg" alt="jquery商城焦点图效果" /></a></li>
<li><a href="http://www.lanrentuku.com/" target="_blank"><img src="img/04.jpg" alt="jquery商城焦点图代码" /></a></li>
<li><a href="http://www.lanrentuku.com/" target="_blank"><img src="img/05.jpg" alt="jquery商城焦点图源码" /></a></li>
</ul>
</div>
</div><!-- wrapper end -->
</body>
css样式
复制代码 代码如下:
<style type="text/css">
* {margin:0padding:0}
body {font-size:12pxcolor:#222font-family:Verdana,Arial,Helvetica,sans-serifbackground:#f0f0f0}
.clearfix:after {content: "."display: blockheight: 0clear: bothvisibility: hidden}
.clearfix {zoom:1}
ul,li {list-style:none}
img {border:0}
.wrapper {width:800pxmargin:0 autopadding-bottom:50px}
/* qqshop focus */
#focus {width:800pxheight:280pxoverflow:hiddenposition:relative}
#focus ul {height:380pxposition:absolute}
#focus ul li {float:leftwidth:800pxheight:280pxoverflow:hiddenposition:relativebackground:#000}
#focus ul li div {position:absoluteoverflow:hidden}
#focus .btnBg {position:absolutewidth:800pxheight:20pxleft:0bottom:0background:#000}
#focus .btn {position:absolutewidth:780pxheight:10pxpadding:5px 10pxright:0bottom:0text-align:right}
#focus .btn span {display:inline-block_display:inline_zoom:1width:25pxheight:10px_font-size:0margin-left:5pxcursor:pointerbackground:#fff}
#focus .btn span.on {background:#fff}
#focus .preNext {width:45pxheight:100pxposition:absolutetop:90pxbackground:url(img/sprite.png) no-repeat 0 0cursor:pointer}
#focus .pre {left:0}
#focus .next {right:0background-position:right top}
</style>
js脚本
复制代码 代码如下:
$(function() {
var sWidth = $("#focus").width()//获取焦点图的宽度(显示面积)
var len = $("#focus ul li").length//获取焦点图个数
var index = 0
var picTimer
//以下代码添加数字按钮和按钮后的半透明条,还有上一页、下一页两个按钮
var btn = "<div class='btnBg'></div><div class='btn'>"
for(var i=0i <leni++) {
btn += "<span></span>"
}
btn += "</div><div class='preNext pre'></div><div class='preNext next'></div>"
$("#focus").append(btn)
$("#focus .btnBg").css("opacity",0.5)
//为小按钮添加鼠标滑入事件,以显示相应的内容
$("#focus .btn span").css("opacity",0.4).mouseenter(function() {
index = $("#focus .btn span").index(this)
showPics(index)
}).eq(0).trigger("mouseenter")
//上一页、下一页按钮透明度处理
$("#focus .preNext").css("opacity",0.2).hover(function() {
$(this).stop(true,false).animate({"opacity":"0.5"},300)
},function() {
$(this).stop(true,false).animate({"opacity":"0.2"},300)
})
//上一页按钮
$("#focus .pre").click(function() {
index -= 1
if(index == -1) {index = len - 1}
showPics(index)
})
//下一页按钮
$("#focus .next").click(function() {
index += 1
if(index == len) {index = 0}
showPics(index)
})
//本例为左右滚动,即所有li元素都是在同一排向左浮动,所以这里需要计算出外围ul元素的宽度
$("#focus ul").css("width",sWidth * (len))
//鼠标滑上焦点图时停止自动播放,滑出时开始自动播放
$("#focus").hover(function() {
clearInterval(picTimer)
},function() {
picTimer = setInterval(function() {
showPics(index)
index++
if(index == len) {index = 0}
},4000)//此4000代表自动播放的间隔,单位:毫秒
}).trigger("mouseleave")
//显示图片函数,根据接收的index值显示相应的内容
function showPics(index) { //普通切换
var nowLeft = -index*sWidth//根据index值计算ul元素的left值
$("#focus ul").stop(true,false).animate({"left":nowLeft},300)//通过animate()调整ul元素滚动到计算出的position
//$("#focus .btn span").removeClass("on").eq(index).addClass("on")//为当前的按钮切换到选中的效果
$("#focus .btn span").stop(true,false).animate({"opacity":"0.4"},300).eq(index).stop(true,false).animate({"opacity":"1"},300)//为当前的按钮切换到选中的效果
}
})
用到的左右切换图片
方法有很多种,下面我介绍一种纯CSS的实现的方式:
<!DOCTYPE html><html lang="zh-cn">
<head>
<meta charset="utf-8" />
<title></title>
<style>
html,
body {
margin: 0
padding: 0
}
.main {
width: 460px/*每个box的宽度为150px,间隔为5*2*/
margin: 0 auto
}
.demo {
float: left
display: inline-block
}
.box {
height: 150px
width: 150px
background: #3695d5
position: relative
overflow: hidden
}
.inbox {
height: 50px
line-height: 50px
text-align: center
color: #FFF
width: 100%
position: absolute
bottom: -50px
background: rgba(0, 0, 0, 0.3)
transition: 1s/*过渡效果*/
}
.box:hover>.inbox {
bottom: 0
}
</style>
</head>
<body>
<div class="main">
<div class="demo">
<div class="box">
图片
<div class="inbox">
文字
</div>
</div>
</div>
<div class="demo" style="margin: 0 5px 0 5px">
<div class="box">
图片
<div class="inbox">
文字
</div>
</div>
</div>
<div class="demo">
<div class="box">
图片
<div class="inbox">
文字
</div>
</div>
</div>
</div>
</body>
</script>
</html>