功能说明:

鼠标移动到不同图片上,该图片滑动展开,其它图片折叠。

支持IE 6 7 8 firefox chrome

效果预览:

 

实现原理:

当鼠标移动到某张图片,该图片以及该图片前的图片以相同速度向左运动,该图片之后的图片也以相同速度向右运动,形成展开选中图片的效果。

代码分析:

var flow_slide_imgs = function(options) {
this._init();

}

使用构造函数模式,构造函数内部调用私有方法_init();

flow_slide_imgs.prototype = {
_init: function(options) {

var defaults = {
containerId: 'container',
ImgsWidth: 0.6

};
var opts = util.extend(defaults, options);

this.container = util.$(opts.containerId);
this.imgs = this.container.getElementsByTagName('img');
this.imgWidth = parseInt(util.getComputedStyle(this.container).width) * opts.ImgsWidth;
this.containerWidth = parseInt(util.getComputedStyle(this.container).width);
this.containerHeight = parseInt(util.getComputedStyle(this.container).height);
this.aveWidth = this.containerWidth / this.imgs.length;
this.newAveWidth = (this.containerWidth - parseInt(this.imgWidth)) / (this.imgs.length - 1);
this.selectedIndex;
this.movePath = 80;
this.canLeft = true;
this.canRight = true;

this._setContainerStyle();
this._setImgsStyle();
this._bindClickEvent();

},

_init方法中,复制传入的参数对象,并使用extend方法把传入参数与默认参数结合,再以对象属性形式保存。最后调用其他私有函数进行初始化。

_setContainerStyle: function() {
this.container.style.cssText = 'overflow:hidden; position:relative;';

},

设置容器的样式,由于容器内图片使用绝对定位并改变位置来实现图片的展开和折叠,所以容器设置为相对定位,并且隐藏超出容器的图片。

_setImgsStyle: function() {

for (var i = 0; i < this.imgs.length; i++) {
this.imgs[i].style.cssText = 'position:absolute;'
+ 'left:' + i * this.aveWidth + 'px;'
+ 'top:0px;'
+ 'width:' + this.imgWidth + ';'
+ 'height:' + this.containerHeight + 'px;'
+ 'z-index:' + i + ';'
+ 'display:block';

}

},

获取容器内图片,并批量设置图片样式,aveWidth为图片初始的平均宽度,该数值在_init方法中通过容器宽度除以图片数量得出。以每张图片的索引为值设置图片的层叠级别,这样可以保证由最左边开始,一张图片叠在其右边的图片的上面。由于图片为行内元素,所以再设置其display为block。

_getSelectedIndex: function(target) {
for (var i = 0, len = this.imgs.length; i < len; i++) {
if (target == this.imgs[i]) {
if (this.selectedIndex && this.selectedIndex < i) {
this.canLeft = true;
this.canRight = false;
}
if (this.selectedIndex && this.selectedIndex > i) {
this.canLeft = false;
this.canRight = true;
}
break;

}
}
return i;
},

获取所选择图片,并且把该图片的索引和之前已打开的图片的索引进行比较,如果所选图片在已打开的图片左边,则图片组只何以向左移动,否则图片只可以向右移动,这样是为了解决鼠标移动过快造成的左边图片和右边图片作相反方向移动的问题。总之,这里保证的所有图片向同一个方向移动。

_resetImgsSizeAndPos: function(obj, selectedIndex) {

if (typeof obj.selectedIndex == 'undefined') {
if (parseInt(util.getComputedStyle(obj.imgs[1]).left) > obj.newAveWidth) {

for (var i = 1; i < obj.imgs.length; i++) {

if (i <= selectedIndex)
obj.imgs[i].style.left = Math.max(parseInt(obj.imgs[i].style.left) - obj.movePath * i, i * obj.newAveWidth) + 'px';
else
obj.imgs[i].style.left = Math.min(parseInt(obj.imgs[i].style.left) + obj.movePath * (obj.imgs.length - i), obj.containerWidth - (obj.imgs.length - i) * obj.newAveWidth) + 'px';
}

obj.timeId = window.setTimeout(arguments.callee, 100, obj, selectedIndex);
}
if (parseInt(util.getComputedStyle(obj.imgs[1]).left) == obj.newAveWidth || parseInt(util.getComputedStyle(obj.imgs[1]).left) == obj.imgWidth) {
obj.selectedIndex = selectedIndex;
}
}
else if (obj.selectedIndex > selectedIndex) {

if (parseInt(util.getComputedStyle(obj.imgs[selectedIndex + 1]).left) < (selectedIndex + 1) * obj.newAveWidth + (obj.imgWidth - obj.newAveWidth) && obj.canRight) {

for (var j = selectedIndex + 1; j <= obj.selectedIndex; j++) {

obj.imgs[j].style.left = Math.min(parseInt(obj.imgs[j].style.left) + obj.movePath, obj.newAveWidth * (j - 1) + obj.imgWidth) + 'px';
}

obj.timeId = window.setTimeout(arguments.callee, 100, obj, selectedIndex);
}
else {
obj.canLeft = true;

obj.selectedIndex = selectedIndex;

}
}
else if (obj.selectedIndex < selectedIndex) {
if (parseInt(util.getComputedStyle(obj.imgs[selectedIndex]).left) > (selectedIndex) * obj.newAveWidth && obj.canLeft) {

for (var j = selectedIndex; j > obj.selectedIndex; j--) {

obj.imgs[j].style.left = Math.max(parseInt(obj.imgs[j].style.left) - obj.movePath, j * obj.newAveWidth) + 'px';
}

obj.timeId = window.setTimeout(arguments.callee, 100, obj, selectedIndex);
}
else {
obj.canRight = true;

obj.selectedIndex = selectedIndex;

}

}

},

根据 所有图片均未打开(第一次选择图片),已打开图片在所选择图片的前面,已打开图片在所选择图片的后面 这三种情况,图片组作不同方向的运动,通过setTimeout每隔100ms移动一个单位位移。

_bindClickEvent: function() {
util.addEventHandler(this.container, 'mouseover', (function(obj) {
return function(eve) {

eve = eve || window.event;
var target = eve.srcElement || eve.target,
selectedIndex = obj._getSelectedIndex(target);

obj._resetImgsSizeAndPos(obj, selectedIndex);

}

})(this));

}

为容器的mouseover事件绑定事件处理程序,利用事件冒泡在容器的处理程序里产生选中图片时的效果。

这里由于需要为事件处理传递参数,所以利用闭包,使传入的obj对象可访问。鼠标移动到图片上方时,调用上面分析过的resetImgsSizeAndPos方法,根据实际情况让图片作出不同的运动。

new flow_slide_imgs();

最后是调用方法,这里没有传入参数,则控件里面使用默认值。

完整demo代码:

html: 

View Code

<div id="container">
<img src="http://images.cnblogs.com/cnblogs_com/Cson/290336/r_cat1.jpg" />
<img src="http://images.cnblogs.com/cnblogs_com/Cson/290336/r_cat2.jpg" />
<img src="http://images.cnblogs.com/cnblogs_com/Cson/290336/r_cat3.jpg" />
<img src="http://images.cnblogs.com/cnblogs_com/Cson/290336/r_cat4.jpg" />
<img src="http://images.cnblogs.com/cnblogs_com/Cson/290336/r_cat5.jpg" />
</div>

css:

View Code

#container{width:600px; height:300px; background-color:Black;}
#container img{display:none;}

base.js:

View Code

var util = {
$: function(sId) { return document.getElementById(sId); },
addEventHandler: function(elem, type, handler) {
if (elem.addEventListener) {
elem.addEventListener(type, handler, false);
}
else {
elem.attachEvent("on" + type, handler);
}
},
removeEventHandler: function(elem, type, handler) {
if (elem.removeEventListener) {
elem.removeEventListener(type, handler, false);
}
else {
elem.detachEvent("on" + type, handler);
}
},
getComputedStyle: function(elem) {
if (elem.currentStyle)
return elem.currentStyle;
else {
return document.defaultView.getComputedStyle(elem, null);
}
},

getElementsByClassName: function(className, parentElement) {
var elems = (parentElement || document.body).getElementsByTagName("*");
var result = [];
for (i = 0; j = elems[i]; i++) {
if ((" " + j.className + " ").indexOf(" " + className + " ") != -1) {
result.push(j);
}
}
return result;
},
extend: function(destination, source) {
for (var name in source) {
destination[name] = source[name];
}
return destination;

}

};

(function() {
var st = window.setTimeout;
window.setTimeout = function(fn, mDelay) {
var t = new Date().getTime();
if (typeof fn == 'function') {
var args = Array.prototype.slice.call(arguments, 2);
var f = function() {
args.push(new Date().getTime() - t - mDelay);
fn.apply(null, args)
};
return st(f, mDelay);
}
return st(fn, mDelay);
}
})();

flow_slide_imgs.js:

View Code

/*
created by cson
supports ie 6 7 8 firefox and chrome
*/

/*constructor of 'flow images controller' */
var flow_slide_imgs = function(options) {
this._init();

}
flow_slide_imgs.prototype = {
/* init container and images and set object's properties */
_init: function(options) {

var defaults = {
containerId: 'container',
ImgsWidth: 0.6

};
var opts = util.extend(defaults, options);

this.container = util.$(opts.containerId);
this.imgs = this.container.getElementsByTagName('img');
this.imgWidth = parseInt(util.getComputedStyle(this.container).width) * opts.ImgsWidth;
this.containerWidth = parseInt(util.getComputedStyle(this.container).width);
this.containerHeight = parseInt(util.getComputedStyle(this.container).height);
this.aveWidth = this.containerWidth / this.imgs.length;
this.newAveWidth = (this.containerWidth - parseInt(this.imgWidth)) / (this.imgs.length - 1);
this.selectedIndex;
this.movePath = 80;
this.canLeft = true;
this.canRight = true;

this._setContainerStyle();
this._setImgsStyle();
this._bindClickEvent();

},
/* set container's style */
_setContainerStyle: function() {
this.container.style.cssText = 'overflow:hidden; position:relative;';

},
/* set styles of images in container */
_setImgsStyle: function() {

for (var i = 0; i < this.imgs.length; i++) {
this.imgs[i].style.cssText = 'position:absolute;'
+ 'left:' + i * this.aveWidth + 'px;'
+ 'top:0px;'
+ 'width:' + this.imgWidth + ';'
+ 'height:' + this.containerHeight + 'px;'
+ 'z-index:' + i + ';'
+ 'display:block';

}

},
/* get current selected image's index */
_getSelectedIndex: function(target) {
for (var i = 0, len = this.imgs.length; i < len; i++) {
if (target == this.imgs[i]) {
if (this.selectedIndex && this.selectedIndex < i) {
this.canLeft = true;
this.canRight = false;
}
if (this.selectedIndex && this.selectedIndex > i) {
this.canLeft = false;
this.canRight = true;
}
break;

}
}
return i;
},

/* reset images' size and position for three situation */
_resetImgsSizeAndPos: function(obj, selectedIndex) {

if (typeof obj.selectedIndex == 'undefined') {
if (parseInt(util.getComputedStyle(obj.imgs[1]).left) > obj.newAveWidth) {

for (var i = 1; i < obj.imgs.length; i++) {

if (i <= selectedIndex)
obj.imgs[i].style.left = Math.max(parseInt(obj.imgs[i].style.left) - obj.movePath * i, i * obj.newAveWidth) + 'px';
else
obj.imgs[i].style.left = Math.min(parseInt(obj.imgs[i].style.left) + obj.movePath * (obj.imgs.length - i), obj.containerWidth - (obj.imgs.length - i) * obj.newAveWidth) + 'px';
}

obj.timeId = window.setTimeout(arguments.callee, 100, obj, selectedIndex);
}
if (parseInt(util.getComputedStyle(obj.imgs[1]).left) == obj.newAveWidth || parseInt(util.getComputedStyle(obj.imgs[1]).left) == obj.imgWidth) {
obj.selectedIndex = selectedIndex;
}
}
else if (obj.selectedIndex > selectedIndex) {

if (parseInt(util.getComputedStyle(obj.imgs[selectedIndex + 1]).left) < (selectedIndex + 1) * obj.newAveWidth + (obj.imgWidth - obj.newAveWidth) && obj.canRight) {

for (var j = selectedIndex + 1; j <= obj.selectedIndex; j++) {

obj.imgs[j].style.left = Math.min(parseInt(obj.imgs[j].style.left) + obj.movePath, obj.newAveWidth * (j - 1) + obj.imgWidth) + 'px';
}

obj.timeId = window.setTimeout(arguments.callee, 100, obj, selectedIndex);
}
else {
obj.canLeft = true;

obj.selectedIndex = selectedIndex;

}
}
else if (obj.selectedIndex < selectedIndex) {
if (parseInt(util.getComputedStyle(obj.imgs[selectedIndex]).left) > (selectedIndex) * obj.newAveWidth && obj.canLeft) {

for (var j = selectedIndex; j > obj.selectedIndex; j--) {

obj.imgs[j].style.left = Math.max(parseInt(obj.imgs[j].style.left) - obj.movePath, j * obj.newAveWidth) + 'px';
}

obj.timeId = window.setTimeout(arguments.callee, 100, obj, selectedIndex);
}
else {
obj.canRight = true;

obj.selectedIndex = selectedIndex;

}

}

},

/*bind handler for mouseover event of images */
_bindClickEvent: function() {
util.addEventHandler(this.container, 'mouseover', (function(obj) {
return function(eve) {

eve = eve || window.event;
var target = eve.srcElement || eve.target,
selectedIndex = obj._getSelectedIndex(target);

obj._resetImgsSizeAndPos(obj, selectedIndex);

}

})(this));

}

}

欢迎转载,请标明出处:http://www.cnblogs.com/Cson/archive/2011/04/03/2004717.html

转载于:https://www.cnblogs.com/Cson/archive/2011/04/03/2004717.html

【CSON原创】 图片滑动展开效果发布相关推荐

  1. 手机端图片滑动切换效果

    最近公司要求开发wap版本页面,碰到了个图片滑动切换效果,折腾了半天,自己封装了一个比较通用的小控件,在此分享一下. 大概功能:可以自定义是否自动切换,支持单手滑动图片进行切换,支持左右滑动切换.循环 ...

  2. JavaScript 图片滑动展示效果javascript

    javascript 图片滑动展示效果 更新版本:slideview 图片滑动(扩展/收缩)展示效果 看到jquery实例:图片展示效果后,我也想拿来试试,但我不太喜欢用框架,所以自己做了个. 其中的 ...

  3. JavaScript 图片滑动切换效果

    序一(08/07/06) 看到alibaba的一个图片切换效果,感觉不错,想拿来用用.但代码一大堆的,看着昏,还是自己来吧. 由于有了做图片滑动展示效果的经验,做这个就容易得多了. 序二(09/03/ ...

  4. flash特效原理:图片滑动放大效果(2)

    flash特效原理:图片滑动放大效果(1) http://blog.csdn.net/hero82748274/archive/2009/10/22/4715312.aspx 最近看了一些关于动态注册 ...

  5. Android仿今日头条图片滑动退出效果

    资源下载(2C币) 逛CSDN的时候,看到几篇写仿今日头条图片滑动退出效果的文章,闲着无聊便想着也给自己项目加上,实现的思路有很多种,本着就近原则选了一篇与自己思路相近的文章结合自己的实践总结一下. ...

  6. JavaScript html 图片滑动切换效果,幻灯片式切换,新闻展示,滚动新闻

    新闻展示,滚动新闻 程序说明 原理就是通过不断设置滑动对象的left(水平切换)和top(垂直切换)来实现图片切换的动态效果. 首先需要一个容器,程序会自动设置容器overflow为hidden,如果 ...

  7. flash特效原理 图片滑动放大效果 2

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! flas ...

  8. 公众号滑动图代码_公众号怎么制作图片滑动的效果?怎么做可以上下滑动的长图?...

    微信公众号图片怎么制作呢?图片太多又该怎么排版呢?接下来就和小编一起看看,怎么使用壹伴助手这款公众号编辑器,来实现公众号图片的排版吧~ 怎么使用公众号制作多图滑动的效果 在公众号文章中,如果插入的图片 ...

  9. dw在php图片滑动切换效果,超简单的图片左右切换滑动

    网上看过很多图片左右切换滑动的效果,不过大都是使用插件实现.插件虽方便,但是对于新手的学习并不是最好的.本文使用jquery这个由原生的JavaScript封装的库,用最简短的代码实现此功能 效果预览 ...

最新文章

  1. Go 语言并发模型 Context
  2. 简单介绍Java中Comparable和Comparator
  3. showModalDialog模态对话框 的使用及一般问题的解决
  4. php dir变量,[PHP] sys_get_temp_dir()和tempnam()函数报错与环境变量的配置问题
  5. lampp mysql最大连接数_mysql连接数问题备份
  6. 在SQL Server 2017上充分利用Python
  7. 请编写一个c程序确定signed,unsigned的char,short,int和long变量取值范围
  8. elementui 表格格式化
  9. css 修改占位符(placeholder)默认颜色、字体
  10. c编码实现连接redis服务器
  11. java socket第三方库_Java基于Socket实现HTTP下载客户端
  12. 51单片机c语言轻松入门,单片机的C语言轻松入门资料
  13. 英特尔核显自定义分辨率_英特尔核芯显卡设置如何操作【图文】
  14. 在浏览器中输入URL(如www.baidu.com)到显示页面经历哪些过程,涉及到哪些协议?
  15. App打开小程序,小程序打开App
  16. Linux系统的登录方式
  17. 第五章 区块链在非金融行业的应用
  18. IntelliJ IDEA Working directory设置
  19. SpringBoot 实现微信模板消息通知推送提醒
  20. BUAA 编译作业 练习11

热门文章

  1. 校园计算机故障解决方论文法,计算机故障与处理-计算机专业毕业论文.pdf
  2. mysql keepalived双主双活_Keepalived单网络双活双主配置模式
  3. python生成100个随机数_Python_0——100闭区间产生3个随机数,两种方法排序
  4. django如何调用php接口,使用django集成第三方api开发接口注意事项
  5. docker添加新的环境变量_Docker的安装及部署Spring Boot项目操作详解!
  6. 【数学与算法】奇异矩阵、奇异值、奇异值分解、奇异性
  7. 【opencv】4.初始化Mat的方式、访问cv::Mat中的某个元素
  8. 神经网络入门(最通俗的理解神经网络)
  9. 《JAVA与模式》之策略模式
  10. Mysql 多表联合查询效率分析及优化