JavaScript实现图片滑动切换的代码示例分享


Posted in Javascript onMarch 06, 2016

假设我们这里有1到5五张bmp图片,那么控制图片切换显示的核心代码可以为:

<script>
 var i=1;
 var img = new Array();
 img[0] = "1.bmp";
 img[1] = "2.bmp";
 img[2] = "3.bmp";
 img[3] = "4.bmp";
 img[4] = "5.bmp";
 function playImg(){
 i=i+1;
 var imgs = document.getElementById("img");
 imgs.src =img[i];
 if(i>=4){
 i=1;
 }
 }
 window.onload = function(){
 document.getElementById("img").src="1.bmp";
 }
</script>
<img id="img" src="" width="500" hieght="500" onclick="playImg()" style="cursor:pointer;">

主要体现了一个图片的切换控制思路,可以用于各种场景,那么下面我们来看一个复杂一些的实现,能够控制滑动和停顿事件等具体的实现图片的滑动切换效果的例子:

var $$ = function (id) {
return "string" == typeof id ? document.getElementById(id) : id;
};
var Extend = function(destination, source) {
for (var property in source) {
 destination[property] = source[property];
}
return destination;
}
var CurrentStyle = function(element){
return element.currentStyle || document.defaultView.getComputedStyle(element, null);
}
var Bind = function(object, fun) {
var args = Array.prototype.slice.call(arguments).slice(2);
return function() {
 return fun.apply(object, args.concat(Array.prototype.slice.call(arguments)));
}
}
var forEach = function(array, callback, thisObject){
if(array.forEach){
 array.forEach(callback, thisObject);
}else{
 for (var i = 0, len = array.length; i < len; i++) { callback.call(thisObject, array[i], i, array); }
}
}
var Tween = {
Quart: {
 easeOut: function(t,b,c,d){
 return -c * ((t=t/d-1)*t*t*t - 1) + b;
 }
},
Back: {
 easeOut: function(t,b,c,d,s){
 if (s == undefined) s = 1.70158;
 return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
 }
},
Bounce: {
 easeOut: function(t,b,c,d){
 if ((t/=d) < (1/2.75)) {
 return c*(7.5625*t*t) + b;
 } else if (t < (2/2.75)) {
 return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
 } else if (t < (2.5/2.75)) {
 return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
 } else {
 return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
 }
 }
}
}

//容器对象,滑动对象,切换数量
var SlideTrans = function(container, slider, count, options) {
this._slider =
(slider);this.container=
(container);//容器对象
this._timer = null;//定时器
this._count = Math.abs(count);//切换数量
this._target = 0;//目标值
this._t = this._b = this._c = 0;//tween参数

this.Index = 0;//当前索引

this.SetOptions(options);

this.Auto = !!this.options.Auto;
this.Duration = Math.abs(this.options.Duration);
this.Time = Math.abs(this.options.Time);
this.Pause = Math.abs(this.options.Pause);
this.Tween = this.options.Tween;
this.onStart = this.options.onStart;
this.onFinish = this.options.onFinish;

var bVertical = !!this.options.Vertical;
this._css = bVertical ? "top" : "left";//方向

//样式设置
var p = CurrentStyle(this._container).position;
p == "relative" || p == "absolute" || (this._container.style.position = "relative");
this._container.style.overflow = "hidden";
this._slider.style.position = "absolute";

this.Change = this.options.Change ? this.options.Change :
 this._slider[bVertical ? "offsetHeight" : "offsetWidth"] / this._count;
};
SlideTrans.prototype = {
 //设置默认属性
 SetOptions: function(options) {
this.options = {//默认值
 Vertical: true,//是否垂直方向(方向不能改)
 Auto: true,//是否自动
 Change: 0,//改变量
 Duration: 30,//滑动持续时间
 Time: 10,//滑动延时
 Pause: 3000,//停顿时间(Auto为true时有效)
 onStart: function(){},//开始转换时执行
 onFinish: function(){},//完成转换时执行
 Tween: Tween.Quart.easeOut//tween算子
};
Extend(this.options, options || {});
 },
 //开始切换
 Run: function(index) {
//修正index
index == undefined && (index = this.Index);
index < 0 && (index = this._count - 1) || index >= this._count && (index = 0);
//设置参数
this._target = -Math.abs(this.Change) * (this.Index = index);
this._t = 0;
this._b = parseInt(CurrentStyle(this._slider)[this.options.Vertical ? "top" : "left"]);
this._c = this._target - this._b;

this.onStart();
this.Move();
 },
 //移动
 Move: function() {
clearTimeout(this._timer);
//未到达目标继续移动否则进行下一次滑动
if (this._c && this._t < this.Duration) {
 this.MoveTo(Math.round(this.Tween(this._t++, this._b, this._c, this.Duration)));
 this._timer = setTimeout(Bind(this, this.Move), this.Time);
}else{
 this.MoveTo(this._target);
 this.Auto && (this._timer = setTimeout(Bind(this, this.Next), this.Pause));
}
 },
 //移动到
 MoveTo: function(i) {
this._slider.style[this._css] = i + "px";
 },
 //下一个
 Next: function() {
this.Run(++this.Index);
 },
 //上一个
 Previous: function() {
this.Run(--this.Index);
 },
 //停止
 Stop: function() {
clearTimeout(this._timer); this.MoveTo(this._target);
 }
};
Javascript 相关文章推荐
jquery 必填项判断表单是否为空的方法
Sep 14 Javascript
javascript 密码强度验证规则、打分、验证(给出前端代码,后端代码可根据强度规则翻译)
May 18 Javascript
jquery 查找select ,并触发事件的实现代码
Mar 30 Javascript
jquery动态加载图片数据练习代码
Aug 04 Javascript
JQuery获取浏览器窗口内容部分高度的代码
Feb 24 Javascript
js 控制页面跳转的5种方法
Sep 09 Javascript
史上最全JavaScript常用的简写技巧(推荐)
Aug 17 Javascript
解决select2在bootstrap modal中不能正常使用的问题
Aug 09 Javascript
JS实现倒计时图文效果
Nov 17 Javascript
VUE中使用MUI方法
Feb 12 Javascript
JavaScript Math对象和调试程序的方法分析
May 13 Javascript
JavaScript表格隔行变色和Tab标签页特效示例【附jQuery版】
Jul 11 jQuery
使用jQuery或者原生js实现鼠标滚动加载页面新数据
Mar 06 #Javascript
AngularJS页面访问时出现页面闪烁问题的解决
Mar 06 #Javascript
JavaScript模拟数组合并concat
Mar 06 #Javascript
JavaScript模拟push
Mar 06 #Javascript
JavaScript中利用jQuery绑定事件的几种方式小结
Mar 06 #Javascript
Node.js模块封装及使用方法
Mar 06 #Javascript
JavaScript中三种异步上传文件方式
Mar 06 #Javascript
You might like
本地计算机无法启动Apache故障处理
2014/08/08 PHP
ThinkPHP查询返回简单字段数组的方法
2014/08/25 PHP
php获取本周开始日期和结束日期的方法
2015/03/09 PHP
摘自织梦CMS的HTTP文件下载类
2015/08/08 PHP
PHP 匿名函数与注意事项详细介绍
2016/11/26 PHP
源码分析 Laravel 重复执行同一个队列任务的原因
2017/12/25 PHP
php使用curl伪造浏览器访问操作示例
2019/09/30 PHP
jquery 简单导航实现代码
2009/09/11 Javascript
Extjs在exlipse中设置自动提示的方法
2010/04/07 Javascript
在jQuery1.5中使用deferred对象 着放大镜看Promise
2011/03/12 Javascript
javascript 系统文件夹文件操作及参数介绍
2013/01/08 Javascript
JS JSON对象转为字符串的简单实现方法
2013/11/18 Javascript
node.js中的events.emitter.removeAllListeners方法使用说明
2014/12/10 Javascript
asp.net+js实现金额格式化
2015/02/27 Javascript
AngularJS模块详解及示例代码
2016/08/17 Javascript
响应式框架Bootstrap栅格系统的实例
2017/12/19 Javascript
Angularjs Ng_repeat中实现复选框选中并显示不同的样式方法
2018/09/12 Javascript
[04:40]DOTA2-DPC中国联赛1月26日Recap集锦
2021/03/11 DOTA
Python使用PDFMiner解析PDF代码实例
2017/03/27 Python
Python与R语言的简要对比
2017/11/14 Python
python Matplotlib画图之调整字体大小的示例
2017/11/20 Python
动态规划之矩阵连乘问题Python实现方法
2017/11/27 Python
python定向爬取淘宝商品价格
2018/02/27 Python
python实现读取大文件并逐行写入另外一个文件
2018/04/19 Python
利用Python将每日一句定时推送至微信的实现方法
2018/08/13 Python
python用opencv批量截取图像指定区域的方法
2019/01/24 Python
Python批量安装卸载1000个apk的方法
2020/04/10 Python
Python pip install之SSL异常处理操作
2020/09/03 Python
python 实现单例模式的5种方法
2020/09/23 Python
ECOSUSI官网:女式皮革背包
2019/09/27 全球购物
环境科学毕业生自荐信
2013/11/21 职场文书
自我鉴定写作要点
2014/01/17 职场文书
哈弗商学院毕业生求职信
2014/02/26 职场文书
学习党的群众路线教育实践活动心得体会范文
2014/11/03 职场文书
2014年加油站工作总结
2014/12/04 职场文书
2015教师个人德育工作总结
2015/07/22 职场文书