~~~~作为编写组件的一个参考吧,在js输出组件样式的问题上 探讨一下 尽量简化组件的调用

function iceSlider(element,options) {
/*
功能:广告翻转切换控制
参数:
w:(int) 广告的宽度
h:(int) 广告的高度,默认为200px
autoTime:(int)切换停留时间,默认为1500ms
ad:(json) 为广告图片路径,链接,弹出目标等数据,如:ad:[{src:'1.jpg',href:'http://163.com',alt:'banner1'}
src:(str)图片src路径
href:(str)图片点击的链接
alt:(str)图片alt描述
up:(boolean) 滑动方向 选项: prototype使用示例:
<div class="productsBanner" id="idTransformView"></div>
document.observe('dom:loaded',function() //dom元素加载后执行
{
$('productsBanner').iceSlider({
ad:[
{src:'1.jpg',href:'http://www.baidu.com', target:'_blank', alt:'1.jpg'},
{src:'2.jpg',href:'http://www.baidu.com', target:'_blank', alt:'2.jpg'},
{src:'3.jpg',href:'http://www.baidu.com', target:'_blank', alt:'3.jpg'}
]
});
}); 原生js调用示例:
<div id="banner"></div>
iceSlider('banner',{
ad:[
{src:'1.jpg',href:'http://www.baidu.com', target:'_blank', alt:'1.jpg'},
{src:'2.jpg',href:'http://www.baidu.com', target:'_blank', alt:'2.jpg'},
{src:'3.jpg',href:'http://www.baidu.com', target:'_blank', alt:'3.jpg'}
],
up:false
}); */
// 输出组件相关的样式 首先检测浏览器
var _IE = (function(){
var v=3, div = document.createElement('div'), all = div.getElementsByTagName('i');
while( div.innerHTML = '<!--[if gt IE '+ (++v) + ']> <i></i><![endif]-->', all[0]);
return v>4?v:false;
})(); // ~~ 判断$!==jQuery ~~可以基于Prototype 或 原生Js
if(window.$){
if(window.$ == jQuery){
var $ = function(id){return typeof id ==='string' ? document.getElementById(id) : id; }
}
}else{
var $ = function(id){return typeof id ==='string' ? document.getElementById(id) : id; }
} /*--------- 获取参数 ---------*/
var h,w; //切换广告的高度宽度,默认为200
var count; //切换广告的数据,根据数据传入的数量自动设定
var ad;
var autoTime;
if(options!=undefined){ h=options.h;
ad=options.ad;
count=options.count || options.ad.length;
autoTime=options.autoTime;
} autoTime=autoTime||1000;
h=h||200;
w=options.w || 200; if(! window.sliderStyleStr){
window.sliderStyleStr = 'ul,li{list-style:none; margin:0; padding:0;}'+
(!options.up ?'.iceSlider{width: '+count * w+'px; height:'+h+'px} .iceSlider li{float:left;}':'') +
'.iceSlider li a{display:inline-block;}'+
'.iceSlider li a img{border:0; width:'+w+'px; height:'+h+'px; margin-bottom:-3px; margin-bottom:0 \9;}'+
'.rotatorNumber{height:20px; position:absolute; right:5px; bottom:10px;}'+
'.rotatorNumber li{float:left; width:20px; height:100%; margin:0 5px; color:#333; background:#fff; border:1px solid #aaa; text-align:center; cursor:default;}'+
'.rotatorNumber .on{border:1px solid hotpink;}';
if(_IE){ document.createStyleSheet("javascript:sliderStyleStr");
}else{
var oStyle =document.createElement('style');
oStyle.innerHTML = window.sliderStyleStr;
document.getElementsByTagName('head')[0].appendChild(oStyle);
}
} /*----------- 生成html --------*/
var li="";
var li2="";
var alt="";
for(var i=0;i<ad.length;i++){ var target=ad[i].target||'_self';
var href=ad[i].href||"javascript:void(0);";
li+='<li><a href="'+href+'" target="'+target+'"><img src="'+ad[i].src+'" alt="'+ad[i].alt+'"/></a></li>';
var cls="";
if(i==0)cls=' class="on"';
li2+='<li'+cls+'>'+parseInt(i+1)+'</li>'; } var html='<div class="viewPort"><ul class="iceSlider" id="iceSlider">'+li+'</ul><ul class="rotatorNumber">'+li2+'</ul></div><span class="bannerTitle" id="bannerTitle"></span>';
$(element).innerHTML=html;
var _target=ad[0].target!='_blank'?'_self':'_blank';
//初始化标题
$('bannerTitle').innerHTML='<a href="'+ad[0].href+'" target="'+_target+'">'+ad[0].alt+'</a>'; /*----------- 生成html end --------*/ var element=$(element);
var viewPort = element.getElementsByTagName('div')[0]; //顶层容器 视口
viewPort.style.height = h +'px';
viewPort.style.width = w +'px';
var count=$('iceSlider').getElementsByTagName('li').length; //内容页数
var a=$('iceSlider').getElementsByTagName('a'); //包裹图片的a function each(list, fun){//用于遍历的功能函数
for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); }
}; // var objs = $(element).select('ul')[1].select("li");
var objs = $(element).getElementsByTagName('ul')[1].getElementsByTagName("li"); //切换的数字
//实例化轮播组件
var tv = new TransformView(
viewPort,
$(element).getElementsByTagName('ul')[0],
h,
count,
{
//当前切换数字 on
Up:options.up,
onStart : function(){ each(objs, function(o, i){ o.className = tv.Index == i ? "on" : ""; }) },
Pause:autoTime
}
); tv.Start(); //播放 each(objs, function(o, i){ //鼠标悬停 数字序号上 切换内容页
o.onmouseover = function(){
o.className = "on";
tv.Auto = false;//暂停自动播放 设置索引 然后播放
tv.Index = i;
tv.Start();
//切换到当前内容页的图片说明
var _target=a[i].target!='_blank'?'_self':'_blank';
$('bannerTitle').innerHTML='<a href="'+a[i].href+'" target="'+_target+'">'+a[i].getElementsByTagName('img')[0].alt+'</a>';
};
o.onmouseout = function(){//鼠标从数字序号离开 恢复自动播放
o.className = "";
tv.Auto = true;
tv.Start();
}
}); } //lihuoSlider end var iceSliderClass = {
create: function() {//通过执行create方法 返回构造函数
return function() {
this.$=(window.$ && window.$!=jQuery) ? window.$ : function(id){return typeof id ==='string' ? document.getElementById(id) : id; } ;
this.initialize.apply(this, arguments);//构造函数的参数直接传入初始化方法
}
}
} Object.extend = function(destination, source) {//浅拷贝方式扩展对象
for (var property in source) {
destination[property] = source[property];
}
return destination;
} var TransformView = iceSliderClass.create(); //create方法返回构造函数
TransformView.prototype = {
//容器对象,滑动对象,切换参数,切换数量,选项对象
initialize: function(ViewPort, sliderCon, psize, count, options) {
if(psize <= 0 || count <= 0) return;
var oViewPort = this.$(ViewPort), oSliderCon = this.$(sliderCon), oThis = this; //this为轮播组件对象 this.Index = 0;//当前索引 this._timer = null;//定时器
this._sliderCon = oSliderCon;//滑动对象
this._psize = psize;//切换参数
this._count = count || 0;//切换数量
this._target = 0;//目标参数 this.SetOptions(options); this.Up = !!this.options.Up;
this.Step = Math.abs(this.options.Step); //步长
this.Time = Math.abs(this.options.Time);
this.Auto = !!this.options.Auto;
this.Pause = Math.abs(this.options.Pause);
this.onStart = this.options.onStart;
this.onFinish = this.options.onFinish; oViewPort.style.overflow = "hidden"; //容器 相对定位 溢出隐藏
oViewPort.style.position = "relative"; oSliderCon.style.position = "absolute"; //内容 绝对定位在容器左上角
oSliderCon.style.top = oSliderCon.style.left = 0;
},
//设置默认属性 SetOptions: function(options) {
this.options = {//默认值
Up: true,//是否向上(否则向左)
Step: 5,//滑动变化率
Time: 10,//滑动延时
Auto: true,//是否自动转换
Pause: 2000,//停顿时间(Auto为true时有效)
onStart: function(){},//开始转换时执行
onFinish: function(){}//完成转换时执行
};
Object.extend(this.options, options || {});
}, //开始切换设置
Start: function() {
if(this.Index < 0){//Index越界则修正
this.Index = this._count - 1;
} else if (this.Index >= this._count){ this.Index = 0; } this._target = -1 * this._psize * this.Index;
this.onStart();
this.Move();
}, //移动
Move: function() {
// var _img=this.$('ul.iceSlider')[0].select('img');
clearTimeout(this._timer);
var oThis = this,
style = this.Up ? "top" : "left",
iNow = parseInt(this._sliderCon.style[style]) || 0,
iStep = this.GetStep(this._target, iNow);//步长 非匀速的 剩余距离的1/5步进 if (iStep != 0) {
this._sliderCon.style[style] = (iNow + iStep) + "px";
this._timer = setTimeout(function(){ oThis.Move(); }, this.Time);
} else {//步长为0 表示到达终点 若为自动播放 则继续start()
this._sliderCon.style[style] = this._target + "px";
this.onFinish(); if (this.Auto) {
this._timer = setTimeout(function(){
oThis.Index++;
oThis.Start();
var curA = oThis.$('iceSlider').getElementsByTagName('img')[oThis.Index].parentNode;
oThis.$('bannerTitle').innerHTML='<a href="'+curA.href+'" target="'+curA.target+'">'+curA.getElementsByTagName('img')[0].alt+'</a>';
}, this.Pause );
}
}
}, //获取步长
GetStep: function(iTarget, iNow) {
var iStep = (iTarget - iNow) / this.Step; //剩余距离1/5步长
if (iStep == 0) return 0;
if (Math.abs(iStep) < 1) return (iStep > 0 ? 1 : -1); //最小步长1px
return iStep; //不一定能整除吧?iStep=1.5也可以?
}, //停止
Stop: function(iTarget, iNow) {
clearTimeout(this._timer);
this._sliderCon.style[this.Up ? "top" : "left"] = this._target + "px";
}
};

轮播组件iceSlider的更多相关文章

  1. 一分钟搞定AlloyTouch图片轮播组件

    轮播图也涉及到触摸和触摸反馈,同时,AlloyTouch可以把惯性运动打开或者关闭,并且设置min和max为运动区域,超出会自动回弹. 除了一般的竖向滚动,AlloyTouch也可以支持横向滚动,甚至 ...

  2. 基于移动端Reactive Native轮播组件的应用与开发详解

    总结下这段时间学习reactive native的一些东西,我们来认识一下,被炒得这么火的rn,究竟是个什么东西,以及如何去搭建自己的demo. reactive  native是什么 由facebo ...

  3. 移动端Reactive Native轮播组件

    移动端Reactive Native轮播组件 总结下这段时间学习reactive native的一些东西,我们来认识一下,被炒得这么火的rn,究竟是个什么东西,以及如何去搭建自己的demo. reac ...

  4. bootstrap轮播组件,大屏幕图片居中效果

    在慕课网学习bootstrap轮播组件的时候,了解到轮播的图片都放在了类名为item下的img中 视频中老师对图片自适应采用给图片img设置width=100%完成,然而这样自适应处理图片在不同屏幕中 ...

  5. C-Swipe Mobile 一个适用于Vue2.x的移动端轮播组件

    近期在做的一个Vue2项目里需要一个可以滑动的轮播组件,但是又因为现有的传统轮播库功能过于繁琐和笨重.因此自己写了一个针对于Vue2.x的轻型轮播组件. 项目GitHub链接:C-Swipe Mobi ...

  6. Angular2组件与指令的小实践——实现一个图片轮播组件

    如果说模块系统是Angular2的灵魂,那其组件体系就是其躯体,在模块的支持下渲染出所有用户直接看得见的东西,一个项目最表层的东西就是组件呈现的视图.而除了直接看的见的躯体之外,一个完整的" ...

  7. bootstrap轮播组件之“如何关闭自动轮播”

    在一个页面里使用多个bootstrap轮播组件的时候,如果还让所有轮播图都自动轮播的话,整个画面都在动,会给用户一种很不好的体验感受.所以,需要关闭轮播图的自动轮播. 关闭方法:去除如下属性即可: d ...

  8. React-Native之轮播组件looped-carousel的介绍与使用

    React-Native之轮播组件looped-carousel的介绍与使用 一,关于react-native轮播组件的介绍与对比 1,react-native-swiper在动态使用网页图片,多张图 ...

  9. MUI组件四:选择器、滚动条、单选框、区域滚动和轮播组件

    目录(?)[+]   1.picker(选择器) mui框架扩展了pipcker组件,可用于弹出选择器,在各平台上都有统一表现.poppicker和dtpicker是对picker的具体实现.*pop ...

随机推荐

  1. Linux特殊权限:SUID、SGID、SBIT

    SUID:      只对二进制程序有效      执行者对于程序需要有x权限      在程序运行过程中,执行者拥有程序拥有者的权限      例如:      普通用户执行passwd命令.   ...

  2. Codeforces Round #242 (Div. 2) &lt;A-D&gt;

    CF424 A. Squats 题目意思: 有n(n为偶数)个x和X,求最少的变换次数,使得X的个数为n/2,输出变换后的序列. 解题思路: 统计X的个数ans,和n/2比較,少了的话,须要把n/2- ...

  3. 非线性规划问题的matlab求解

    函数:[x, fval] = fmincon(FUN, X0, A, B, Aeq, Beq, LB, UB, NONLCON) 返回的x:是一个向量——在取得目标函数最小时各个xi的取值: 返回的f ...

  4. jquery 插件 validate 学习

    jquery是十分方便的对于现在来说. 首先应该明白一个问题: <p> <label  for="password">Password</label& ...

  5. Spring_database_Template

    配置applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...

  6. C++的常量折叠(三)

    背景知识 在开始之前先说一下符号表,这个编译器中的东西.下面看一下百度百科中的描述: 符号表是一种用于语言翻译器中的数据结构.在符号表中,程序源代码中的每个标识符都和它的声明或使用信息绑定在一起,比如 ...

  7. centos7 ops

    默认使用firewall防火墙,不在使用iptables 特点:可以动态加载新设置的规则,而不用重启服务 scp操作: scp localfile user@host:remotedir mysql. ...

  8. jmeter cookie管理器 使用方法---新手学习记录1

    首先得抓包: 我已post方法为例: POST /api/datasources/lemontest/jaql HTTP/1.1 Host: 192.168.1.107:8081 Content-Le ...

  9. Windows下Python中的中文路径和中文输出问题

    这几天有个项目需要写一点类似于脚本的小程序,就用Python写了,涉及到中文路径和中文输出的问题,整理一下. 有一个问题我觉得需要先强调一下,在写Python程序的时候,一定保证编码是utf-8,然后 ...

  10. js获取浏览器窗口的大小

    在我本地测试当中: 在IE.FireFox.Opera下都可以使用 document.body.clientWidth document.body.clientHeight 即可获得,很简单,很方便. ...