js图片轮播插件的封装
网络编程 2021-07-04 18:32www.168986.cn编程入门
这篇文章主要为大家详细介绍了js图片轮播插件的封装代码,只需要获取到图片和按钮,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文为大家分享了js图片轮播插件的具体代码,供大家参考,具体内容如下
我封装的这个轮播插件只需要获取到图片和按钮就可以啦。
css 样式
.body{ width: 700px; margin: 100px auto; position: relative; height: 300px; overflow: hidden; } .body img{ width: 700px; position: absolute; display: none; } .body ul{ position: absolute; bottom: 3px; left: 50%; transform: translateX(-50%); } .body li{ list-style: none; float: left; width: 15px; height: 15px; border-radius: 50px; background: none; border: 2px solid #fff; margin-right: 10px; cursor: pointer; } .active{ background-color: #fff !important; } .body a{ text-decoration: none; position: absolute; display: block; : 50%; transform: translateY(-50%); height: 50px; width: 30px; background-size: 100% 60%; background-color: rgba(0,0,0,0.3); } .left{ left: 0; background: url('../img/left.png') no-repeat center center; } .right{ right: 0; background: url('../img/right.png') no-repeat center center; }
页面结构 html 代码
<body> <div class="body"> <img src="img/1.jpg"> <img src="img/2.jpg"> <img src="img/3.jpg"> <ul> <li class="active"></li> <li></li> <li></li> </ul> <a href="javascript:;" class="left"></a> <a href="javascript:;" class="right"></a> </div>
js部分,插件调用
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script> <script type="text/javascript" src="js/slider.js"></script> <script type="text/javascript"> $.slider({ imgElement:$(".body img"), liElement:$(".body li"), leftBtn:$(".left"), rightBtn:$(".right"), time:2000 }) </script>
封装的插件
(function($){ function slider(options){ this.opts=$.extend({},slider.defaluts,options); this._imgSlider(); } //设置默认值 slider.defaluts={ imgElement:null, liElement:null, leftBtn:null, rightBtn:null, time:2000 } slider.prototype._imgSlider=function(){ var opts=this.opts, index=0, len=opts.imgElement.length, timeInter=null; if(opts.imgElement=='') return; opts.imgElement.eq(0).show(); showTime(); //当鼠标经过 轮播停止,移开继续 opts.imgElement.hover(function() { clearInterval(timeInter); }, function() { showTime(); }); //点击li 显示对应的图片 opts.liElement.click(function(){ var id=$(this).index(); show(id); }); //向左向右 opts.leftBtn.click(function(){ clearInterval(timeInter); --index; index=Math.max(0,index); show(index); showTime(); }); opts.rightBtn.click(function(){ clearInterval(timeInter); ++index; index=Math.min(len-1,index); show(index); showTime(); }); function showTime(){ timeInter=setInterval(function(){ index++; if(index>len){ index=0; } show(index); },opts.time); } function show(index){ opts.imgElement.eq(index).fadeIn(1000).siblings('img').fadeOut(1000); opts.liElement.eq(index).addClass('active').siblings('li').removeClass('active'); } } $.extend({ slider:function(options){ new slider(options); } }) })(jQuery)
效果图
精彩专题分享
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 宿迁百度关键词排名指南:实现精准营销的关键
- 四川SEO优化怎么做网络推广
- 立昂技术备案老域名收购:如何为您的业务赋能
- 安徽百度关键词seo贵不贵,一般需要多少钱
- 吉林百度快照排名怎么做电话营销
- 多伦新手做SEO怎么做
- 甘肃优化关键词排名推广怎么做论坛营销
- 沙雅SEO网站推广:提升您的在线可见性
- 四川SEO优化如何提升销售额和销售量
- 聂荣网站排名优化:提升网站可见性的全方位指
- 涞水SEO:提升地方企业在线可见性的策略
- 辽宁百度seo排名怎样做网站排名
- 临湘哪有关键词排名优化:提升网站可见度的关
- 黑龙江百度网站优化有没有优惠
- 凉城优化关键词排名推广:提升您的网络可见性
- 萝北整站优化:提升您网站流量和排名的全面指