jQuery图片轮播插件
2016-01-20 00:00
721 查看
摘要: 简单实现图片的轮播
今天在公司没什么事无聊就写了一个简单的图片轮播的插件,实现比较简单,就直接上代码了。
在这里我只设置了三个默认参数,即父辈的高度、宽度和是否显示下面的bar(默认为true),如果有需要可以根据自己所需要的进行添加,至于样式,我是直接写在扩展里面了,各位可以根据项目的需要写到单独的css样式里面,当然有些也可以写默认参数。执行如下:
今天在公司没什么事无聊就写了一个简单的图片轮播的插件,实现比较简单,就直接上代码了。
;(function($){ $.fn.extend({ slider : function(options){ //参数默认值 var defaults = { count : 1, //默认显示第一位 width : 400, //默认父级宽度 height : 200, //默认父级高度 bar : false, //bar默认不显示 }; var opts = $.extend({}, defaults, options); return this.each(function(){ var o = opts; //将元素集合赋给变量 var parent = $(this); //父辈容器 var child = parent.children().eq(0); //滚动层 var childNodes = parent.children().children(); //滚动节点 var childLen = childNodes.length; //个数 var timer = null; //定时器 var indexPos; //当前所在区的位置 parent.css({ width : o.width, height : o.height, position : 'relative', overflow : "hidden", }); child.css({ width : o.width * childLen, height : o.height, position : "absolute", }); childNodes.css({ width : o.width, height : o.height, float : "left", }); parent.append("<a href='javascript:void(0);' id = 'prev' style = 'left : 0' class='arrow'><</a>\ <a href='javascript:void(0);' id = 'next' style = 'right : 0' class='arrow'>></a>") //判断bar是否需呀 if(o.bar){ if (o.count != 1) { child.css({ left : -(o.count - 1) * o.width }); }; var html = ""; parent.append("<div id = 'buttons'></div>"); $("#buttons").css({ position: 'absolute', bottom: "10px", width : o.width, height : "10px", textAlign: 'center', }); for (var i = 0; i < childLen; i++) { if (i == 0) { html += "<span class = 'actived'></span>"; } else { html += "<span></span>"; } }; $("#buttons").html(html); $("#buttons").find('span').css({ width : "10px", height : "10px", borderRadius : "100%", backgroundColor : "#fff", zIndex : '100000', display: 'inline-block', margin : "0 3px", cursor : "pointer" }); isActived(o.count); } $(".arrow").css({ position: "absolute", fontSize: "40px", width : "40px", height: "40px", top: "50%", marginTop: "-20px", color: "#000", lineHeight: "40px", textAlign: "center", cursor : "pointer", display : "none" }); //鼠标滑入事件 parent.hover(function() { $(".arrow").show(); clearInterval(timer); }, function() { $(".arrow").hide(); timer = setInterval(function(){ $("#next").trigger("click"); }, 3000); }); //下一个 $("#next").on("click",function(){ if(!child.is(':animated')){ if (o.count < childLen) { child.animate({ left : '-=' + o.width }, "slow"); o.count ++; isActived(o.count); } else { child.animate({ left : '+=' + (childLen - 1) * o.width }, "slow"); o.count = 1; isActived(o.count); } } }); //前一个 $("#prev").on("click",function(){ if(!child.is(':animated')){ if (o.count == 1) { child.animate({ left : '-=' + (childLen - 1) * o.width }, "slow"); o.count = childLen; isActived(o.count); } else { child.animate({ left : '+=' + o.width }, "slow"); o.count --; isActived(o.count); } } }); //每隔三秒执行一次 timer = setInterval(function(){ $("#next").trigger("click"); }, 3000); //如果bar存在,点击相对应的圆点到相应的区 if(o.bar){ $("#buttons").on('click', 'span', function(event) { var index = $(this).index(); $("#buttons").find('span').each(function(el){ if($(this).hasClass('actived')){ indexPos = el; if( index > indexPos){ child.animate({ left : '-=' + (index - indexPos) * o.width }, "slow"); }else{ child.animate({ left : '+=' + (indexPos - index) * o.width }, "slow"); } isActived(index + 1); o.count = index + 1; } }) }); } function isActived(param){ if(o.bar){ $("#buttons").find('span').eq(param - 1).addClass('actived') .siblings().removeClass('actived'); } } }) } }) })(jQuery);
在这里我只设置了三个默认参数,即父辈的高度、宽度和是否显示下面的bar(默认为true),如果有需要可以根据自己所需要的进行添加,至于样式,我是直接写在扩展里面了,各位可以根据项目的需要写到单独的css样式里面,当然有些也可以写默认参数。执行如下:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javascript" src = "http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="reset.css"> <script type="text/javascript" src = "slider.js"></script> <style type="text/css"> .actived{background: #f00 !important;} </style> <script type="text/javascript"> $(function(){ $(".containter").slider({ count : 2, bar : true }) }) </script> </head> <body> <div class="containter"> <ul> <li><img src="img/1.jpg"></li> <li><img src="img/2.jpg"></li> <li><img src="img/3.jpg"></li> <li><img src="img/4.jpg"></li> <li><img src="img/5.jpg"></li> </ul> </div> </body> </html>
相关文章推荐
- JQuery1——基础($对象,选择器,对象转换)
- Web布局连载——两栏固定布局(五)
- JavaScript演示排序算法
- 2015-2016网页设计趋势分析 Web Design of Trends
- jQuery Ajax 跨域调用
- 移动端的长按事件
- jquery教程靠边站,一分钱不花让你免费学会jquery
- JQuery+Strusts1.x无刷新登录
- [div+css]晒晒最新制作专题推广页模板
- JavaScript 各种遍历方式详解
- 数组方法汇总
- 设计更快的网页(三):字体和 CSS 调整
- 纯CSS制作的新闻网站中的文章列表
- 10条影响CSS渲染速度的写法与使用建议第1/3页
- BS项目中的CSS架构_仅加载自己需要的CSS
- 很不错的 CSS Hack 又学了一招
- 发一个css比较清爽的写法
- CSS expression控制图片自动缩放效果代码[兼容 IE,Firefox]
- css布局网页水平居中常用方法
- CSS经典技巧十则第1/2页