王朝网络
分享
 
 
 

原生javascript-图片按钮切换

王朝学院·作者佚名  2016-05-05  
宽屏版  字体: |||超大  

即上次被分配写原生JS相册弹窗后,这次又接到了写原生JS,图片按钮切换,真激情:

个人在线实例:http://www.lgyweb.com/picSwitch/

以下为详细代码:

复制代码

function LGY_picSwitch(option){

this.oWrap = this.getId(option.wrapID); //最外层元素

this.olistWrap = this.getNodeByClassname(this.oWrap,'gy_picSwitch_listWrap')[0];

this.oUl = this.olistWrap.getElementsByTagName('ul')[0];

this.oBtnPRev = this.getNodeByClassname(this.oWrap,'gy_picSwitch_prev')[0];

this.oBtnNext = this.getNodeByClassname(this.oWrap,'gy_picSwitch_next')[0];

this.nLen = this.oUl.getElementsByTagName('li').length; //图片总数

this.nScollCount = option.scrollCount; //每次滚动的数量

this.nScollLen = Math.ceil(this.nLen/option.scrollCount); // 切换判断的最大值

this.nSwitchWidth = 0; //每次切换移动的距离,在代码里面动态获取值

this.nIndex = 0; //切换图片的当前索引

this.timer = null; //切换图片的引值

this.int();

}

LGY_picSwitch.prototype = {

getId:function(id){

return document.getElementById(id);

},

getNodeByClassname:function(parent,classname){

var classElements = new Array();

var els = parent.getElementsByTagName('*');

var elsLen = els.length;

var pattern = new RegExp("(^|\\s)"+classname+"(\\s|$)");

for (i = 0, j = 0; i < elsLen; i++) {

if ( pattern.test(els[i].className) ) {

classElements[j] = els[i];

j++;

}

}

return classElements;

},

getCSS:function(node,value)

{

return node.currentStyle?node.currentStyle[value]:getComputedStyle(node,null)[value];

},

setCss:function(node,val){

for(var v in val){

node.style.cssText += ';'+ v +':'+val[v];

}

},

moveFn:function(node,value,targetValue,callback){

var _that = this;

clearInterval(this.timer);

this.timer = setInterval(function()

{

var val = parseFloat(_that.getCss(node,value));

var speed = ( targetValue- val )/8;

speed = speed>0?Math.ceil(speed):Math.floor(speed);

if(speed ==0)

{

clearInterval(_that.timer);

callback&&callback();

}

else

{

node.style[value] = ( val + speed ) +'px';

}

},20);

},

picChange:function(){

this.moveFn(this.oUl,'marginLeft',-this.nIndex*this.nSwitchWidth);

},

cancelBubble:function(e){

e.stopPropagation?e.stopPropagation():e.cancelBubble = true;

},

btnIsShow:function(){

this.setCss(this.oBtnNext,{'display':'block'});

this.setCss(this.oBtnPrev,{'display':'block'});

if( this.nIndex == 0 ) this.setCss(this.oBtnPrev,{'display':'none'});

if( this.nIndex ==(this.nScollLen-1) ) this.setCss(this.oBtnNext,{'display':'none'});

},

btnPrev:function(){

var _that = this;

this.oBtnPrev.onclick = function(e){

var e = e || window.event;

_that.cancelBubble(e);

if(_that.nIndex != 0 ) {

_that.nIndex--;

_that.picChange();

_that.btnIsShow();

}

}

},

btnNext:function(){

var _that = this;

this.oBtnNext.onclick = function(e){

var e = e || window.event;

_that.cancelBubble(e);

if(_that.nIndex != (_that.nScollLen-1) ) {

_that.nIndex++;

_that.picChange();

_that.btnIsShow();

}

}

},

int:function(){

//动态获取移动的宽度

var oLi = this.oUl.getElementsByTagName('li')[0],

oLi_w = oLi.offsetWidth + parseInt(this.getCss(oLi,'marginLeft')) + parseInt(this.getCss(oLi,'marginRight'));

this.nSwitchWidth = oLi_w*this.nScollCount;

//按钮显示初始化

this.btnIsShow();

//左右切换

this.btnPrev();

this.btnNext();

}

}

复制代码

复制代码

/*

* HTML结构必需是以下:外层ID名,自己传入 如下面的:id="gy_picSwitch02" ,ID名,自己随便给

但,里面的结构必需一样,包括类名classname

<div id="gy_picSwitch02">

<span class="gy_picSwitch_prev"></span>

<span class="gy_picSwitch_next"></span>

<div class="gy_picSwitch_listWrap">

<ul>

<li><img src="images/pic01.jpg" alt=""></li>

<li><img src="images/pic02.jpg" alt=""></li>

<li><img src="images/pic03.jpg" alt=""></li>

<li><img src="images/pic04.jpg" alt=""></li>

<li><img src="images/pic05.jpg" alt=""></li>

<li><img src="images/pic06.jpg" alt=""></li>

<li><img src="images/pic07.jpg" alt=""></li>

<li><img src="images/pic08.jpg" alt=""></li>

</ul>

</div>

</div>

参数:'wrapID':'xxxx',最外层的ID名

'scrollCount':5,滚动的数量

*

*/

//实例化

new LGY_picSwitch({'wrapID':'gy_picSwitch','scrollCount':5});

复制代码

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有