jquery写的banner焦点图滚动切换效果
最近有点闲,用jquery写了个banner滚动切换效果,为了提高扩展性,函数里增加了三个参数,支持class,方向(up,down,left,right)和每张图显示的时间.虽然说授人与鱼,不如授人与渔,只是自我感觉并没有太多的技巧,只是分析效果的每一个事件,然后给每一个事件加动作,在逻辑上肯定有自己没想到的,也肯定有更好的写法,欢迎一起探讨交流.
效果图:
html代码:
<!doctype html>
<html>
<head>
<meta charset=”utf-8″>
<meta name=”author” content=”stealer” />
<link rel=”stylesheet” href=”css/common.css”>
<link rel=”stylesheet” href=”css/scroll.css”>
<title>banner滚动切换</title>
</head>
<body>
<div class=”banner”>
<ul class=”scroll”>
<li><a href=”#”><img src=”images/120323_exchange_538x200.jpg” /></a></li>
<li><a href=”#”><img src=”images/121017_qh_538x200.jpg” /></a></li>
<li><a href=”#”><img src=”images/121019_d1_538x200.jpg” /></a></li>
</ul>
</div>
<div class=”banner”>
<ul class=”scroll”>
<li><a href=”#”><img src=”images/120323_exchange_538x200.jpg” /></a></li>
<li><a href=”#”><img src=”images/121017_qh_538x200.jpg” /></a></li>
<li><a href=”#”><img src=”images/121019_d1_538x200.jpg” /></a></li>
<li><a href=”#”><img src=”images/121017_qh_538x200.jpg” /></a></li>
<li><a href=”#”><img src=”images/121019_d1_538x200.jpg” /></a></li>
</ul>
</div>
</body>
<script src=”js/jquery-1.4.3.min.js”></script>
<script src=”js/stealer.js”></script>
</html>
细心的话会发现这个效果不单单支持图片切换,因为换的是li,内容可以自己写.
css代码:
/* @Author stealer */
.banner{
width:538px;
height:200px;
border:1px solid #d7d7d7;
position:relative;
overflow:hidden;
}
.scroll-dot{
position:absolute;
left:0;
bottom:5px;
width:100%;
}
.scroll-dot li{
display:inline-block;
*display:inline;
*zoom:1;
margin:0 5px;
width:10px;
height:10px;
}
.scroll-dot a{
display:block;
width:100%;
height:100%;
font:1px/1px Arial, Helvetica, sans-serif;
border:1px solid #999;
background:#ddd;
border-radius:5px;
cursor:pointer;
}
.scroll-dot a.hover{
background:#aaa;
}
.scroll-dot a.active{
background:#ff0;
}
jquery代码:
/* @Author stealer */
function Slide(obj,direction,speed){//obj是class名,direction是方向(支持up,down,left,right),speed滚动速度(毫秒为单位).down有个小问题,图片是倒叙来依次显示.
$(“.”+obj).each(function(){
var S=$(this);
var content=$(this).children(“.scroll”);
var top=parseInt(content.css(“margin-top”));
var left=parseInt(content.css(“margin-left”));
var t;
var n=0;//计数,第几个
$(this).append(function(){
var html=”<li><a class=’active’></a></li>”;
for(var i=1;i<content.children(“li”).length;i++){
html+=”<li><a></a></li>”;
}
return(“<ul class=’scroll-dot’>”+html+”</ul>”);
})
$(“.scroll-dot a”).hover(function(){
$(this).toggleClass(“hover”);
});
var dot=$(this).children(“.scroll-dot”).children().children(“a”);
if(direction==”up”){
function scroll(){
top=parseInt(content.css(“margin-top”));
top-=content.children(“li”).height();
n+=1;
if(-top>=content.height()){
content.animate({marginTop:0},500,function(){
n=0;
dot.removeClass(“active”);
dot.eq(n).addClass(“active”);
t=setTimeout(function(){scroll()},speed);
});
}
else{
content.animate({marginTop:top},500,function(){
dot.removeClass(“active”);
dot.eq(n).addClass(“active”);
t=setTimeout(function(){scroll()},speed);
});
}
}
}
if(direction==”down”){
content.css({“margin-top”:-content.children(“li”).height()*(content.children(“li”).length-1)});
function scroll(){
top=parseInt(content.css(“margin-top”));
top+=content.children(“li”).height();
n+=1;
if(top>0){
content.animate({marginTop:-content.children(“li”).height()*(content.children(“li”).length-1)},500,function(){
n=0;
dot.removeClass(“active”);
dot.eq(n).addClass(“active”);
t=setTimeout(function(){scroll()},speed);
});
}
else{
content.animate({marginTop:top},500,function(){
dot.removeClass(“active”);
dot.eq(n).addClass(“active”);
t=setTimeout(function(){scroll()},speed);
});
}
}
}
if(direction==”left”){
content.css({“width”:content.children(“li”).width()*content.children(“li”).length});
content.children(“li”).css({“float”:”left”});
function scroll(){
left=parseInt(content.css(“margin-left”));
left-=content.children(“li”).width();
n+=1;
if(-left>=content.width()){
content.animate({marginLeft:0},500,function(){
n=0;
dot.removeClass(“active”);
dot.eq(n).addClass(“active”);
t=setTimeout(function(){scroll()},speed);
});
}
else{
content.animate({marginLeft:left},500,function(){
dot.removeClass(“active”);
dot.eq(n).addClass(“active”);
t=setTimeout(function(){scroll()},speed);
});
}
}
}
if(direction==”right”){
content.css({“width”:content.children(“li”).width()*content.children(“li”).length,”margin-left”:content.children(“li”).width()*(1-content.children(“li”).length)});
content.children(“li”).css({“float”:”right”});
function scroll(){
left=parseInt(content.css(“margin-left”));
left+=content.children(“li”).width();
n+=1;
if(left>0){
content.animate({marginLeft:-content.children(“li”).width()*(content.children(“li”).length-1)},500,function(){
n=0;
dot.removeClass(“active”);
dot.eq(n).addClass(“active”);
t=setTimeout(function(){scroll()},speed);
});
}
else{
content.animate({marginLeft:left},500,function(){
dot.removeClass(“active”);
dot.eq(n).addClass(“active”);
t=setTimeout(function(){scroll()},speed);
});
}
}
}
$(this).mouseover(function(){
clearInterval(t);
dot.each(function(e){
$(this).click(function(){
var that=$(this);
if(direction==”up”){
top=-parseInt(content.children(“li”).height())*e;
content.stop().animate({marginTop:top},500);
}
else if(direction==”down”){
top=parseInt(content.children(“li”).height())*(e-content.children(“li”).length+1);
content.stop().animate({marginTop:top},500);
}
else if(direction==”left”){
left=-parseInt(content.children(“li”).width())*e;
content.stop().animate({marginLeft:left},500);
}
else if(direction==”right”){
left=parseInt(content.children(“li”).width())*(e-content.children(“li”).length+1);
content.stop().animate({marginLeft:left},500);
}
dot.removeClass(“active”);
that.addClass(“active”);
n=e;
});
});
});
$(this).mouseout(function(){
t=setTimeout(function(){scroll()},speed);
});
t=setTimeout(function(){scroll()},speed);
});
}
$(document).ready(function () {
Slide(“banner”,”right”,3000);//示例
});
转载请注明:jquery写的banner焦点图滚动切换效果 - 编程知识库
您可能还会对这些文章感兴趣
2016-12-20 1,786次只会用jQuery前端到底low不low?
如果你之前没有看过我的《前端工程师如何月薪过4万》这里建议大家仔细读读一下,因为里面有整个前端工程师成长的技术路线图和我的故事。同时很多小伙伴问我的学历后来没再考考么,我是考了成考的北京航空航天大学,也马上快毕业了。不过我觉得这件事不足为提,因为国...
2016-12-17 721次jquery的$().each,$.each的区别
在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法。两个方法是有区别的,从而这两个方法在针对不同的操作上,显示了各自的特点。 $().each,对于这个方法,在dom处理上面用的较多。如果页面有多个input标签类型为checkbox,对于这时用$().each来...
2016-12-15 1,853次jquery加载外部文件(.txt .html)显示在网页上
在web开发中常常需要用到jquery去动态的加载文件显示在网页上面,下面是实现的小方法,希望给大家一点提示,达到抛砖引玉的作用哦!!! 效果图: 要加载显示的文件: 网页加载效果: html代码截图: jquery代码截图: 转载请注明:jquery加载外部文件(.txt ....
2016-12-15 665次javascript实现网页倒计时
web开发中常常需要使用到网页倒计时功能,下面是实现方法: 效果图: 代码截图: 转载请注明:javascript实现网页倒计时 - 编程知识库
大家正在看
- linux 系统中Mysql 进程占用cpu过高的解决
- 二类电商是什么意思? 二类电商有哪些?暴利二类电商还好做吗?
- 【二类电商广点通投放指南】二类电商广点通投放值不值
- 密码保护:支付宝突破微信封锁唤起支付宝代码
- Host is not allowed to connect to this MySQL server解决方法
- 密码保护:移动端js自动复制代码
- linux数据库调优,WordPress MySQL占用cpu高数据库优化
- 2017 年十大网页设计趋势
- 网页端的VR实现离我们还远么?
- 最完整的Chrome浏览器客户端调试大全
- iPhone用户人均每天遭电话骚扰1次
- 3G电子化销售服务系统
- Java WeakReference的理解与使用
- 搞清楚 Python traceback