jq插件处女座 图片轮播
好久没写博客了,变得好懒呀,无地自容。最近一直在学sass和jq插件的写法,照猫画虎的谢了一个jq的插件,也算是第一次真正称得上插件的插件 ,废话不多说 上代码
(function($) {
$.fn.carousel = function(options) {
if($(this).length==)return false;
var opts = $.extend({},$.fn.carousel.defaults,options);
function Slide(o){
this.o = o;
this.box = o.find(opts['imgbox']);
this.cirEle = this.box.find(opts['cirEle'])
this.nav = o.find(opts['slideNav']);
this.rightAr = o.find(opts['rightAr']);
this.leftAr = o.find(opts['leftAr']);
this.et = opts['eventType'];
this.autoPlay = opts['autoPlay'];
this.navClass = opts['navClass'];
this.speed = opts['speed']
this.timer = null;
this.len = this.cirEle.length;
this.cirNavEle = null;
this.onOff = true;
this.activeIndex = ;
this.iNow = ;
this.boxWidth = this.box.outerWidth()
}
Slide.prototype.init = function(){
var _this = this;
_this.createNav().togglePage();
this.leftAr.on('click',function(){
_this.play();
})
this.rightAr.on('click',function(){
_this.play();
})
if(this.autoPlay){
this.stop();
this.timer = setInterval(function(){
_this.play();
},this.speed[]);
this.o.hover(function(){
_this.stop();
},function(){
_this.timer = setInterval(function(){
_this.play();
},_this.speed[])
})
}
}
Slide.prototype.createNav = function(){
var _this = this;
var arr = [];
$.each(_this.cirEle,function(i,n){
if(i == ){
arr.push('<span class="cur">'+ (i+) +'</span>');
}else{
arr.push('<span>'+ (i+) +'</span>');
_this.cirEle.eq(i).css({'left':_this.boxWidth});
}
});
_this.nav.html(arr.join(''));
this.cirNavEle = _this.nav.find('span');
return this;
}
Slide.prototype.togglePage = function(){
var _this = this;
this.cirNavEle.on(_this.et,function(){
if(_this.onOff){
_this.onOff = false;
_this.activeIndex = $(this).index();
$(this).addClass(_this.navClass).siblings().removeClass(_this.navClass);
if(_this.activeIndex > _this.iNow){
_this.toggleRight();
}else if( _this.activeIndex < _this.iNow ){
_this.toggleLeft();
}
_this.cirEle.eq(_this.activeIndex).animate({'left':},_this.speed[],function(){
_this.onOff = true;
$(this).css({'z-index':}).siblings().css({'z-index':})
})
_this.iNow = _this.activeIndex;
}
})
return this;
}
Slide.prototype.toggleLeft= function(){
var _this = this;
_this.cirEle.eq(_this.activeIndex).css({'left':-_this.boxWidth})
_this.cirEle.eq(_this.iNow).animate({'left':_this.boxWidth},_this.speed[])
}
Slide.prototype.toggleRight= function(){
var _this = this;
_this.cirEle.eq(_this.activeIndex).css({'left':_this.boxWidth})
_this.cirEle.eq(_this.iNow).animate({'left':-_this.boxWidth},_this.speed[])
}
Slide.prototype.play = function(dir){
var _this = this;
if(_this.onOff){
_this.onOff = false;
if(!dir){
_this.activeIndex++;
_this.activeIndex %= _this.len;
_this.toggleRight();
}else{
_this.activeIndex--;
if(_this.activeIndex <= ){
_this.activeIndex = ;
}
_this.toggleLeft();
}
_this.cirEle.eq(_this.activeIndex).animate({'left':},_this.speed[],function(){
_this.onOff = true;
_this.iNow = _this.activeIndex;
_this.cirNavEle.eq(_this.activeIndex).addClass(_this.navClass).siblings().removeClass(_this.navClass);
$(this).css({'z-index':}).siblings().css({'z-index':})
})
}
}
Slide.prototype.stop = function(){
clearInterval(this.timer);
}
return this.each(function () {
var $this = $(this);
var obj = new Slide($this);
obj.init();
});
};
//默认配置
$.fn.carousel.defaults = {
'imgbox' : '.carousel-box', //滚动元素父容器
'cirEle' : 'li', //滚动元素
'slideNav' : '.carousel-nav', //图片索引菜单
'autoPlay' : true, //是否自动轮播
'rightAr' : '.arR', //下一张
'leftAr' : '.arL', //上一张
'speed':[,], //速度 一张显示出来的时间, 间隔时间
'eventType' : 'click', //切换方式
'navClass' : 'cur' //当前菜单高亮css
}
})(jQuery);
HTML
<div class="area">
<div class="carousel-box">
<ul>
<li><a href="#1"><img src="data:images/1.jpg" alt=""></a></li>
<li><a href="#2"><img src="data:images/2.jpg" alt=""></a></li>
<li><a href="#3"><img src="data:images/3.jpg" alt=""></a></li>
<li><a href="#4"><img src="data:images/4.jpg" alt=""></a></li>
<li><a href="#5"><img src="data:images/5.jpg" alt=""></a></li>
<li><a href="#6"><img src="data:images/6.jpg" alt=""></a></li>
<li><a href="#7"><img src="data:images/7.jpg" alt=""></a></li>
</ul>
</div>
<div class="carousel-nav"></div>
<span class="arL"><</span>
<span class="arR">></span> </div>
<script>
$(function(){
$('.area').carousel();
})
</script>
css
.area, .area .carousel-box {
width: 310px; }
.area .carousel-nav span, .area .arR, .area .arL {
width: 20px;
height: 20px;
border: 1px solid #666;
text-align: center;
display: inline-block; }
.area .carousel-box ul, .area .carousel-box li {
position: absolute;
left:;
top:; }
.area {
margin: 50px auto; }
.area .carousel-box {
height: 350px;
overflow: hidden;
position: relative; }
.area .carousel-nav span.cur {
background: #000;
color: #FFF; }
.area .arR, .area .arL {
margin-top: 20px; }
现在回头想想,其实jq插件并不是想象的那么的难,难的还是在实现功能的思路上,写法和平常js也都差不多,上面现在只是简单的实现左右轮播,有时间把上下方向也加上,嘎嘎
jq插件处女座 图片轮播的更多相关文章
- html+jq实现简单的图片轮播
今天上班没事,就自己琢磨着写一下图片轮播,可是没想到,哈哈竟然写出来啦,下面就贴出来代码,作为纪念保存下下哈: <body style="text-align: center;&quo ...
- jQuery插件slider实现图片轮播
1:引入相应的js文件 jquery.SuperSilder.js 2:HTML: 结构 注:此地加载图片的代码也可以从后台库中读取图片的集合列表,然后通过循环的方式显示出来 3:CSS 样式定义左 ...
- 原生JS实现"旋转木马"效果的图片轮播插件
一.写在最前面 最近都忙一些杂七杂八的事情,复习软考.研读经典...好像都好久没写过博客了... 我自己写过三个图片轮播,一个是简单的原生JS实现的,没有什么动画效果的,一个是结合JQuery实现的, ...
- PgwSlideshow-基于Jquery的图片轮播插件
0 PgwSlideshow简介 PgwSlideshow是一款基于Jquery的图片轮播插件,基本布局分为上下结构,上方为大图轮播区域,用户可自定义图片轮播切换的间隔时间,也可以通过单击左右方向按键 ...
- 12款经典的白富美型—jquery图片轮播插件—前端开发必备
图片轮播是网站中的常用功能,用于在有限的网页空间内展示一组产品图片或者照片,同时还有非常吸引人的动画效果.本文向大家推荐12款实用的 jQuery 图片轮播效果插件,帮助你在你的项目中加入一些效果精美 ...
- Nivo Slider - 世界上最棒的 jQuery 图片轮播插件
Nivo Slider 号称世界上最棒的图片轮播插件,有独立的 jQuery 插件和 WordPress 插件两个版本.目前下载量已经突破 1,800,000 次!jQuery 独立版本的插件主要有如 ...
- 图片轮播插件-carouFredSel
carouFredSel图片轮播插件基于Jquery,比较常规的轮播插件,支持滚轮及键盘左右按键,加入其它插件可实现更加复杂的特效. 主页地址:http://caroufredsel.dev7stud ...
- jq实现图片轮播:圆形焦点+左右控制+自动轮播
来源:http://www.ido321.com/862.html html代码: 1: <!DOCTYPE html> 2: <html lang="en"&g ...
- JQuery插件之图片轮播插件–slideBox
来源:http://www.ido321.com/852.html 今天偶然发现了一个比较好用的图片轮播插件—slideBox 先看看效果:http://slidebox.sinaapp.com/ 代 ...
随机推荐
- JSP和Servlet的区别。
1.jsp经编译后就变成了Servlet.(JSP的本质就是Servlet,JVM只能识别java的类,不能识别JSP的代码,Web容器将JSP的代码编译成JVM能够识别的java类)2.jsp更擅长 ...
- JavaScript语法学习笔记
1.关于执行JavaScript代码的方法: 第一种方法是将JavaScript代码放到文档<head>标签中的<script>标签之间: <head> & ...
- Hdu1094
#include <stdio.h> int main() { ; while(scanf("%d",&n)!=EOF){ ;i<n;i++){ scan ...
- Android Listview异步动态加载网络图片
1.定义类MapListImageAndText管理ListViewItem中控件的内容 package com.google.zxing.client.android.AsyncLoadImage; ...
- UML--核心元素之业务实体
如果说参与者和用例描述了我们在这个问题领域中达到什么样的目标,那么业务实体就描述了我们使用什么来达到业务目标以及通过什么来记录这个业务目标. 如果把问题领域比喻成一幢大楼的话,业务实体就是构成这幢大楼 ...
- POJ3255--次短路
求1到N的次短路,到某个顶点v的次短路有两种可能,一是到其他某个顶点u的最短路+edge(u,v)二是到其他某个点u的次短路+edge(u,v): 因此在松弛的时候不仅要记录最短路,同时也要记录次短路 ...
- mysql用户和权限管理
用户和权限管理 Information about account privileges is stored in the user, db, host, tables_priv, columns_p ...
- 【强烈推荐】《剑指Offer:名企面试官精讲典型编程题》一书中IT名企经典面试题
各位程序猿: <剑指Offer>一书源自该书作者何海涛坚持更新与编写的博客(http://zhedahht.blog.163.com/),该博客收集整理了大量如微软.Goo ...
- cookielib模块基础学习
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import cookielib #主要用于处理http客户端的co ...
- Hibernate(四)——缓存策略+lazy
Hibernate作为和数据库数据打交道的框架,自然会设计到操作数据的效率问题,而对于一些频繁操作的数据,缓存策略就是提高其性能一种重要手段,而Hibernate框架是支持缓存的,而且支持一级和二级两 ...