Home>Article>Web Front-end> Js image click switching carousel implementation code

Js image click switching carousel implementation code

Guanhui
Guanhui forward
2020-07-27 18:20:16 2697browse

Js image click switching carousel implementation code

Click to switch the picture

       

Achieve the effect

Carousel

        

tools.js

//动画函数 /*参数 * -1.obj 对象 * -2.speed 速度 * -3.target 执行动画的目标 * -4.arrt 要变化的样式 * -5.callback:回调函数 将会在动画执行完后执行 */ function move(obj,speed,target,arrt,callback){ //关闭上一次定时器 clearTimeout(obj.timer); //判断速度的正负值 //如果从0向800移动则为正 //如果从800向0移动则为负 var current=parseInt(getStyle(obj,arrt)); if(current>target){ speed=-speed; } //开启一个定时器 //为obj添加一个timer属性,用来保存它1自己的定时器的标识 obj.timer=setInterval(function(){ //获取原来的left值 var oldvalue=parseInt(getStyle(obj,arrt)); //在旧值的基础上增加 var newvalue=oldvalue+speed; if(speed<0&&newvalue0&&newvalue>target) { newvalue=target; } obj.style[arrt]=newvalue+"px"; //当元素到达target关闭定时器 if(newvalue===target||newvalue===target){ clearTimeout(obj.timer); //动画执行完 执行函数 callback&&callback(); } },30); };

Achieve effect

Recommended tutorial: "PHP"

The above is the detailed content of Js image click switching carousel implementation code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete