情况如题,下面请看JS,后面会跟HTML以及CSS,
同时调用了jquery库(<script type="text/javascript" src="http://static-img3.cdn.aliyuncs.com/specials/game/js/require-jquery.js"></script>)
<script type="text/javascript">
$(function() {
var sWidth = $(".flash").width(); //获取焦点图的宽度(显示面积)
var len = $(".flash ul li").length; //获取焦点图个数
var index = 0;
var picTimer; //以下代码添加数字按钮和按钮后的半透明长条
var btn = "
";
for(var i=0; i < len; i++) {
btn += "<span>" + (i+1) + "</span>";
}
btn += "</div>"
$(".flash").append(btn);
$(".flash .btnBg").css("opacity",0.5); //为数字按钮添加鼠标滑入事件,以显示相应的内容
$(".flash .btn span").mouseenter(function() {
index = $("#flash .btn span").index(this);
showPics(index);
}).eq(0).trigger("mouseenter"); //本例为左右滚动,即所有li元素都是在同一排向左浮动,所以这里需要计算出外围ul元素的宽度
$(".flash ul").css("width",sWidth * (len + 1));
//鼠标滑上焦点图时停止自动播放,滑出时开始自动播放
$(".flash").hover(function() {
clearInterval(picTimer);
},function() {
picTimer = setInterval(function() {
if(index == len) { //如果索引值等于li元素个数,说明最后一张图播放完毕,接下来要显示第一张图,即调用showFirPic(),然后将索引值清零
showFirPic();
index = 0;
} else { //如果索引值不等于li元素个数,按普通状态切换,调用showPics()
showPics(index);
}
index++;
},3000); //此3000代表自动播放的间隔,单位:毫秒
}).trigger("mouseleave"); //显示图片函数,根据接收的index值显示相应的内容
function showPics(index) { //普通切换
var nowLeft = -index*sWidth; //根据index值计算ul元素的left值
$(".flash ul").stop(true,false).animate({"left":nowLeft},500); //通过animate()调整ul元素滚动到计算出的position
$(".flash .btn span").removeClass("on").eq(index).addClass("on"); //为当前的按钮切换到选中的效果
} function showFirPic() { //最后一张图自动切换到第一张图时专用
$(".flash ul").append($(".flash ul li:first").clone());
var nowLeft = -len*sWidth; //通过li元素个数计算ul元素的left值,也就是最后一个li元素的右边
$(".flash ul").stop(true,false).animate({"left":nowLeft},500,function() {
//通过callback,在动画结束后把ul元素重新定位到起点,然后删除最后一个复制过去的元素
$(".flash ul").css("left","0");
$(".flash ul li:last").remove();
});
$(".flash .btn span").removeClass("on").eq(0).addClass("on"); //为第一个按钮添加选中的效果
}
});
</script><div class="wrapper">
<div class="flash">
<ul>
<li><a href="#" target="_blank"><img src="img/p01.jpg" /></a></li>
<li><a href="#" target="_blank"><img src="img/p02.jpg" /></a></li>
<li><a href="#" target="_blank"><img src="img/p03.jpg" /></a></li>
<li><a href="#" target="_blank"><img src="img/p04.jpg" /></a></li>
</ul>
</div>
</div>
<style type="text/css">
* {margin:0; padding:0;}
body {font-size:12px; color:#222; font-family:Verdana,Arial,Helvetica,sans-serif; background:#f0f0f0;}
.clearfix:after {content: "."; display: block; height: 0; clear: both; visibility: hidden;}
.clearfix {zoom:1;}
ul,li {list-style:none;}
img {border:0;}
.wrapper {width:600px; margin:0 auto; padding-bottom:50px;}
.wrapper {width:780px;}
/* tmall flash */
.flash {width:300px; height:200px; overflow:hidden; position:relative;}
.flash ul {height:200px; position:absolute;}
.flash ul li {float:left; width:300px; height:200px; overflow:hidden; position:relative; background:#000;}
.flash ul li div {position:absolute; overflow:hidden;}
.flash .btnBg {position:absolute; width:300px; height:20px; left:0; bottom:0; background:#000;}
.flash .btn {position:absolute; width:290px; height:20px; left:0; bottom:0; padding-left:10px;}
.flash .btn span {display:inline-block; _display:inline; _zoom:1; width:18px; height:18px; line-height:18px; text-align:center; font-size:14px; font-family:"Microsoft YaHei",SimHei; margin-right:10px; cursor:pointer; color:#fff;}
.flash .btn span.on {background:#000; color:#fcc;}
</style>
焦点图javascript
同时调用了jquery库(<script type="text/javascript" src="http://static-img3.cdn.aliyuncs.com/specials/game/js/require-jquery.js"></script>)
<script type="text/javascript">
$(function() {
var sWidth = $(".flash").width(); //获取焦点图的宽度(显示面积)
var len = $(".flash ul li").length; //获取焦点图个数
var index = 0;
var picTimer; //以下代码添加数字按钮和按钮后的半透明长条
var btn = "
";
for(var i=0; i < len; i++) {
btn += "<span>" + (i+1) + "</span>";
}
btn += "</div>"
$(".flash").append(btn);
$(".flash .btnBg").css("opacity",0.5); //为数字按钮添加鼠标滑入事件,以显示相应的内容
$(".flash .btn span").mouseenter(function() {
index = $("#flash .btn span").index(this);
showPics(index);
}).eq(0).trigger("mouseenter"); //本例为左右滚动,即所有li元素都是在同一排向左浮动,所以这里需要计算出外围ul元素的宽度
$(".flash ul").css("width",sWidth * (len + 1));
//鼠标滑上焦点图时停止自动播放,滑出时开始自动播放
$(".flash").hover(function() {
clearInterval(picTimer);
},function() {
picTimer = setInterval(function() {
if(index == len) { //如果索引值等于li元素个数,说明最后一张图播放完毕,接下来要显示第一张图,即调用showFirPic(),然后将索引值清零
showFirPic();
index = 0;
} else { //如果索引值不等于li元素个数,按普通状态切换,调用showPics()
showPics(index);
}
index++;
},3000); //此3000代表自动播放的间隔,单位:毫秒
}).trigger("mouseleave"); //显示图片函数,根据接收的index值显示相应的内容
function showPics(index) { //普通切换
var nowLeft = -index*sWidth; //根据index值计算ul元素的left值
$(".flash ul").stop(true,false).animate({"left":nowLeft},500); //通过animate()调整ul元素滚动到计算出的position
$(".flash .btn span").removeClass("on").eq(index).addClass("on"); //为当前的按钮切换到选中的效果
} function showFirPic() { //最后一张图自动切换到第一张图时专用
$(".flash ul").append($(".flash ul li:first").clone());
var nowLeft = -len*sWidth; //通过li元素个数计算ul元素的left值,也就是最后一个li元素的右边
$(".flash ul").stop(true,false).animate({"left":nowLeft},500,function() {
//通过callback,在动画结束后把ul元素重新定位到起点,然后删除最后一个复制过去的元素
$(".flash ul").css("left","0");
$(".flash ul li:last").remove();
});
$(".flash .btn span").removeClass("on").eq(0).addClass("on"); //为第一个按钮添加选中的效果
}
});
</script><div class="wrapper">
<div class="flash">
<ul>
<li><a href="#" target="_blank"><img src="img/p01.jpg" /></a></li>
<li><a href="#" target="_blank"><img src="img/p02.jpg" /></a></li>
<li><a href="#" target="_blank"><img src="img/p03.jpg" /></a></li>
<li><a href="#" target="_blank"><img src="img/p04.jpg" /></a></li>
</ul>
</div>
</div>
<style type="text/css">
* {margin:0; padding:0;}
body {font-size:12px; color:#222; font-family:Verdana,Arial,Helvetica,sans-serif; background:#f0f0f0;}
.clearfix:after {content: "."; display: block; height: 0; clear: both; visibility: hidden;}
.clearfix {zoom:1;}
ul,li {list-style:none;}
img {border:0;}
.wrapper {width:600px; margin:0 auto; padding-bottom:50px;}
.wrapper {width:780px;}
/* tmall flash */
.flash {width:300px; height:200px; overflow:hidden; position:relative;}
.flash ul {height:200px; position:absolute;}
.flash ul li {float:left; width:300px; height:200px; overflow:hidden; position:relative; background:#000;}
.flash ul li div {position:absolute; overflow:hidden;}
.flash .btnBg {position:absolute; width:300px; height:20px; left:0; bottom:0; background:#000;}
.flash .btn {position:absolute; width:290px; height:20px; left:0; bottom:0; padding-left:10px;}
.flash .btn span {display:inline-block; _display:inline; _zoom:1; width:18px; height:18px; line-height:18px; text-align:center; font-size:14px; font-family:"Microsoft YaHei",SimHei; margin-right:10px; cursor:pointer; color:#fff;}
.flash .btn span.on {background:#000; color:#fcc;}
</style>
焦点图javascript
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货