jquery 轮播图
slider.js
(function(){
/**
parent //父容器
changeTime //每次间隔几秒切换下一条
leaveTime //鼠标从小图上离开过后几秒继续切换
index //从第几个开始
changeFn //新的显示完全后的回调
hoverFn//鼠标移到小图上的回调
initFn//初始化完成后的回调
dataFormat //数据格式化函数
*/
var def = {
changeTime : 5000,
leaveTime : 5000,
index : 0,
dataFormat : function(obj){
return obj;
}
}
function Slider(opt){
opt = opt || {};
for(var p in def){
if(opt[p] === undefined){
opt[p] = def[p];
}
}
this.opt = opt;
this.data = [];
this.idx = this.opt.index || 0;
this.imgArr = [];
this.navArr = [];
this.opt.parent && (this.opt.parent.innerHTML = '');
}
Slider.prototype.setData = function(data){
/**
* img
* thumb
* title
* url
* rgb
*/
for(var i=0;i<data.length;i++){
this.data.push(this.opt.dataFormat(data[i]));
}
}
Slider.prototype.init = function(){
var parentDom = this.opt.parent;
parentDom.className = 'slide_imgs';
var imgCon = this.imgCon = document.createElement('div');
imgCon.className = 'imgs_list';
var navCon = this.navCon = document.createElement('div');
navCon.className = 'item_points';
parentDom.appendChild(imgCon);
parentDom.appendChild(navCon);
this.createImg();
this.createThumb();
this.go(this.opt.index);
this.opt.initFn && this.opt.inifFn(this);
}
Slider.prototype.createImg = function(){
var list = this.data , temp ;
for(var i=0;i<list.length;i++){
temp = document.createElement('a');
temp.className = 'img_item';
// temp.style.backgroundColor = 'rgb('+list[i].rgb+')';
temp.style.display = this.opt.index == i ? 'block' : 'none';
if(list[i].url && list[i].url != '#' && list[i].url.indexOf('javascript') == -1){
temp.setAttribute('href',list[i].url);
temp.setAttribute('target','_blank');
}else{
temp.setAttribute('href','javascript:;');
}
temp.style.background = 'url('+list[i].img+') no-repeat center center';
temp.onclick = function(){
this.blur();
}
this.imgCon.appendChild(temp);
this.imgArr.push(temp);
temp.setAttribute('title',list[i].title);
}
}
Slider.prototype.createThumb = function(){
var list = this.data , temp , border , img , that = this , len = list.length;
for(var i=0;i<len;i++){
temp = document.createElement('a');
temp.className = 'point';
(function(idx){
var _idx = idx;
$(temp).hover(function(){
if(idx == that.idx) return;
if(that.timer){
clearTimeout(that.timer);
}
that.go(idx,true);
that.opt.hoverFn && that.opt.hoverFn.call(that,list[idx]);
},function(){
if(that.timer){
clearTimeout(that.timer);
}
that.timer = setTimeout(function(){
if(idx == len -1){
_idx = 0;
}else{
_idx ++;
}
that.go(_idx);
},that.opt.leaveTime);
})
})(i);
this.navCon.appendChild(temp);
this.navArr.push(temp);
temp.setAttribute('title',list[i].title);
}
}
Slider.prototype.next = function(){
var idx = this.idx + 1;
this.go(idx);
}
Slider.prototype.prev = function(){
var idx = this.idx - 1;
this.go(idx);
}
Slider.prototype.go = function(idx,handle){
var len = this.data.length, that = this;
idx = Math.min(Math.max(0,idx),this.data.length - 1);
if(len == 1) {
this.navArr[idx].className = 'point cur';
return;
}
if(this.timer){
clearTimeout(this.timer);
}
var old = this.idx;
if(this.imgArr[old]){
$(this.imgArr[old]).stop(0,0).animate({opacity:0},200,function(){
$(this).hide();
});
this.navArr[old].className = 'point';
this.imgArr[idx].style.display = 'block';
$(this.imgArr[idx]).css('opacity',0);
$(this.imgArr[idx]).stop(0,0).animate({opacity:1},200,function(){
that.opt.changeFn && that.opt.changeFn(that,that.data(idx));
});
this.navArr[idx].className = 'point cur';
this.idx = idx;
if(!handle){
this.timer = setTimeout(function(){
if(idx == len -1){
idx = 0;
}else{
idx ++;
}
that.go(idx);
},this.opt.changeTime);
}
}
}
window.Slider = Slider;
})();
index.js
$(function(){
function createBanner(){
var list = [
{img : '/callcenter.jpg',url:'',title:'客服中心'},
{img : '/imprestcenter.jpg',url:'',title:'充值中心'},
{img : '/safecenter.jpg',url:'',title:'安全中心'},
{img : 'http://gameapi.xiaoyou-game.com/static/usercenter/usercenter.jpg',url:'',title:'用户中心'}
]
var slider = new Slider({
parent : $('.slide_imgs')[0]
});
slider.setData(list);
slider.init();
}
createBanner();
})
html
<div class="slide_imgs">
</div>
1、在html里面占位置,
2、index.js将轮播图信息写好,
3、新建一个Slider对象,并实例化即可。
jquery 轮播图的更多相关文章
- Jquery 轮播图简易框架
=====================基本结构===================== <div class="carousel" style="width: ...
- jQuery轮播图--不使用插件
说明:引入jquery.min.js 将轮播图放入imgs文件夹 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitiona ...
- jQuery轮播图(手动点击轮播)
下面来看看最终做的手动点击轮播效果: 一.原理说明 (1)首先是轮播图的架构,我采用了一个最外边的大div包住两个小div,一个小div里面放四张图片,另一个小div里面放四个数字按钮 (2)对最外边 ...
- jQuery轮播图(二)利用构造函数和原型创建对象以实现继承
本文是在我开始学习JavaScript继承时,对原型继承的一些理解和运用.文中所述的继承方式均是使用js特有的原型链方式,实际上有了ES6的类之后,实现继承的就变得十分简单了,所以这种写法现在也不在推 ...
- jQuery轮播图(一)轮播实现并封装
利用面向对象自己动手写了一个封装好的jquery轮播组件,可满足一般需求,不仅使用简单且复用性高. demo:点此预览 代码地址:https://github.com/zsqosos/componen ...
- jquery 轮播图实例
实现效果:1.图片每2秒钟切换1次. 2.当鼠标停留在整个页面上时,图片不进行轮播. 3.当点击右下角的小球时,出现该选项的对应图片,而且切换页选项的背景颜色发生相应的变化. 4.当图片发生轮播切换时 ...
- 《第31天:JQuery - 轮播图》
源码下载地址:链接:https://pan.baidu.com/s/16K9I... 提取码:0ua2 写这篇文章,当做是对自已这一天的一个总结.写轮播图要准备的东西:三张尺寸大小一样的图片.分为三个 ...
- 超级简单的jquery轮播图demo
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 支持触屏的jQuery轮播图插件
移动轮播图我看到两类, 一款是无线天猫的m.tmall.com和携程,实现了无缝轮播. 一款是蘑菇街的,没有实现无缝轮播. 我自己重写一个,类似天猫. 1.页面代码 <!DOCTYPE html ...
随机推荐
- 121. 122. 123. 188. Best Time to Buy and Sell Stock *HARD* 309. Best Time to Buy and Sell Stock with Cooldown -- 买卖股票
121. Say you have an array for which the ith element is the price of a given stock on day i. If you ...
- C# string 数组 每个元素 加上单引号,每一个都被包含在单引号内
在拼接SQL的时候经常会遇到此类问题,尤其是in查询的时候,内容是一段 单引号的 字符的时候 strWhere += " a.EC101_WRYBH IN (" + string ...
- easyui 中重复加载两次url
之前一直在使用easyui中,忽视了官网上的小细节,类似于datagrid.combobox 等组件在使用的时候,它的数据加载方式分为两种: 官网中: ①在html中,比如: <table id ...
- jar转dll
IKVM http://www.cnblogs.com/luckeryin/archive/2012/03/28/2421274.html
- 转:Linux 安装 Mysql
前段时间安装了Mysql,但是有些问题,就想把他卸载了,重新安装一个,但是没想到在Linux卸载软件是一个很痛苦的事情. 我的Mysql是用命令的方式安装的,就是上一篇文章用到的那个命令(sudo ...
- 使用Linq快速的操作XML
开始内容之前先分享一段话 有时候,当你知道要做什么的时候就做的很快,比如你要实现个功能,码字的活儿不算很难,做个检索也不会有什么难倒你的.但是,做着做着,你发现好像世界上的工作都在重复,于是你有种心要 ...
- Flume Hello World!
Flume 是 Cloudera 公司开源出来的一套日志收集系统.模型如下所示: 图中Source,Sink分别代表数据源和数据目的地,channel表示Source和Sink之间的通道.配置文件为/ ...
- 2015.11.16JQuery 隐藏,显示按钮.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- bzoj 1926: [Sdoi2010]粟粟的书架
#include<cstdio> #include<iostream> #define N 201 #define M 500008 using namespace std; ...
- NSDate的运算
NSDate存储的是世界标准时(UTC),输出时需要根据时区转换为本地时间 Dates NSDate类提供了创建date,比较date以及计算两个date之间间隔的功能.Date对象是不可改变的. 如 ...