特效描述:利用jquery实现 仿淘宝商城 多格焦点图,利用jquery实现仿 淘宝商城多格焦点图

代码结构

1. 引入CSS

<link href=”css/style.css” type=”text/css” rel=”stylesheet” />

2. 引入JS

<script type=”text/javascript” src=”js/jquery.js”></script>

3. HTML代码

<style type=”text/css”>

*{margin:0;padding:0;list-style-type:none;}
body{font:12px/180% Arial, Helvetica, sans-serif, “宋体”;color:#333;background:#f0f0f0;}
a,img{border:0;}
/* demo */
.demo{width:780px;margin:0 auto;}
.demo h2{height:70px;line-height:50px;font-size:22px;font-weight:normal;font-family:”Microsoft YaHei”,SimHei;text-align:center;}
/* focus */
#focus{position:relative;width:780px;height:420px;overflow:hidden;}
#focus ul{height:420px;position:absolute;}
#focus ul li{float:left;width:780px;height:420px;overflow:hidden;position:relative;background:#000;}
#focus ul li div{position:absolute;overflow:hidden;}
#focus .btnBg{position:absolute;width:780px;height:40px;left:0;bottom:0;background:#000;}
#focus .btn{position:absolute;height:30px;left:10px;bottom:4px;}
#focus .btn span{
float:left;display:inline-block;width:30px;height:30px;line-height:30px;text-align:center;font-size:16px;margin-right:10px;cursor:pointer;background:#716564;color:#ffffff;
border-radius:15px;
-moz-border-radius:15px;
-webkit-border-radius:15px;
}
#focus .btn span.on{background:#B91919;color:#ffffff;}
</style>

<script type=”text/javascript”>

$(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=0; i < len; i++){
btn += “<span>

” + (i+1) + “</span>

“;
}
btn += “</div>


$(“#focus”).append(btn);
$(“#focus .btnBg”).css(“opacity”,0.3);
//为数字按钮添加鼠标滑入事件,以显示相应的内容
$(“#focus .btn span”).mouseenter(function(){
index = $(“#focus .btn span”).index(this);
showPics(index);
}).eq(0).trigger(“mouseenter”);
//本例为左右滚动,即所有li元素都是在同一排向左浮动,所以这里需要计算出外围ul元素的宽度
$(“#focus ul”).css(“width”,sWidth * (len + 1));
//鼠标滑入某li中的某div里,调整其同辈div元素的透明度,由于li的背景为黑色,所以会有变暗的效果
$(“#focus ul li div”).hover(function(){
$(this).siblings().css(“opacity”,0.7);
},function() {
$(“#focus ul li div”).css(“opacity”,1);
});
//鼠标滑上焦点图时停止自动播放,滑出时开始自动播放
$(“#focus”).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值
$(“#focus ul”).stop(true,false).animate({“left”:nowLeft},500); //通过animate()调整ul元素滚动到计算出的position
$(“#focus .btn span”).removeClass(“on”).eq(index).addClass(“on”); //为当前的按钮切换到选中的效果
}
function showFirPic(){ //最后一张图自动切换到第一张图时专用
$(“#focus ul”).append($(“#focus ul li:first”).clone());
var nowLeft = -len*sWidth; //通过li元素个数计算ul元素的left值,也就是最后一个li元素的右边
$(“#focus ul”).stop(true,false).animate({“left”:nowLeft},500,function(){
//通过callback,在动画结束后把ul元素重新定位到起点,然后删除最后一个复制过去的元素
$(“#focus ul”).css(“left”,”0″);
$(“#focus ul li:last”).remove();
});
$(“#focus .btn span”).removeClass(“on”).eq(0).addClass(“on”); //为第一个按钮添加选中的效果
}
});
</script>

<div class=”demo”>

<h2>

jquery高仿2012淘宝商城多格焦点图滚动切换</h2>

<div id=”focus”>

<ul>

<li>

<div style=”left:0;top:0;”><a href=”http://cxvn.com/”><img width=”780″ height=”420″ src=”images/5364.jpg” alt=”2012淘宝商城最新多格焦点图源代码” /></a></div>

</li>

<li>

<div style=”left:0;top:0;”><a href=”http://cxvn.com/”><img width=”560″ height=”420″ src=”images/5152.jpg” alt=”2012淘宝商城最新多格焦点图源代码” /></a></div>

<div style=”right:0;top:0;”><a href=”http://cxvn.com/”><img width=”220″ height=”140″ src=”images/234rwe.jpg” alt=”2012淘宝商城最新多格焦点图源代码” /></a></div>

<div style=”right:0;top:140px;”><a href=”http://cxvn.com/”><img width=”220″ height=”140″ src=”images/54325.jpg” alt=”2012淘宝商城最新多格焦点图源代码” /></a></div>

<div style=”right:0;bottom:0;”><a href=”http://cxvn.com/”><img width=”220″ height=”140″ src=”images/as124.jpg” alt=”2012淘宝商城最新多格焦点图源代码” /></a></div>

</li>

<li>

<div style=”left:0;top:0;”><a href=”http://cxvn.com/”><img width=”260″ height=”210″ src=”images/12as.jpg” alt=”2012淘宝商城最新多格焦点图源代码” /></a></div>

<div style=”left:260px;top:0;”><a href=”http://cxvn.com/”><img width=”260″ height=”210″ src=”images/12wf.jpg” alt=”2012淘宝商城最新多格焦点图源代码” /></a></div>

<div style=”left:0;top:210px;”><a href=”http://cxvn.com/”><img width=”520″ height=”210″ src=”images/adf13.jpg” alt=”2012淘宝商城最新多格焦点图源代码” /></a></div>

<div style=”right:0;top:0;”><a href=”http://cxvn.com/”><img width=”260″ height=”420″ src=”images/1235.jpg” alt=”2012淘宝商城最新多格焦点图源代码” /></a></div>

</li>

<li>

<div style=”left:0;top:0;”><a href=”http://cxvn.com/”><img width=”560″ height=”420″ src=”images/5243.jpg” alt=”2012淘宝商城最新多格焦点图源代码” /></a></div>

<div style=”right:0;top:0;”><a href=”http://cxvn.com/”><img width=”220″ height=”140″ src=”images/3246.jpg” alt=”2012淘宝商城最新多格焦点图源代码” /></a></div>

<div style=”right:0;top:140px;”><a href=”http://cxvn.com/”><img width=”220″ height=”140″ src=”images/536.jpg” alt=”2012淘宝商城最新多格焦点图源代码” /></a></div>

<div style=”right:0;bottom:0;”><a href=”http://cxvn.com/”><img width=”220″ height=”140″ src=”images/56dsfa.jpg” alt=”2012淘宝商城最新多格焦点图源代码” /></a></div>

</li>

<li>

<div style=”left:0;top:0;”><a href=”http://cxvn.com/”><img width=”780″ height=”420″ src=”images/52525.jpg” alt=”2012淘宝商城最新多格焦点图源代码” /></a></div>

</li>

</ul>

</div>

</div>

下载地址

立即下载

1.《js焦点图特效:仿淘宝商城多格焦点图》援引自互联网,旨在传递更多网络信息知识,仅代表作者本人观点,与本网站无关,侵删请联系页脚下方联系方式。

2.《js焦点图特效:仿淘宝商城多格焦点图》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。

3.文章转载时请保留本站内容来源地址,https://www.cxvn.com/code/focus/407.html