87,924
社区成员
发帖
与我相关
我的任务
分享
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> RevealTrans </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
body{background-color: #CCCCCC;}
#container {width: 525px;text-align: center;margin: 0 auto;}
#context {background-color: #777777;border: 2px solid #555555;width: 500px;}
#context img {border: none;margin: 0px;}
#nav {width: 510px;height: 140px;overflow: hidden;list-style: none;margin-top: 3px;position: relative; padding-left: 0px; margin-left:3px}
#nav li {float: left; margin: 0 7px 4px 0;border: 2px solid #555;}
#nav div {width: 90px;height: 60px; overflow: hidden;}
#nav div img {width: 95px;height: 60px;}
</style>
<script>
/*!
* RevealTrans
* http://www.cnblogs.com/goodness2010/
*
* Copyright (c) 2009 GoodNess2010
* Date: 2009-1-13 (星期三)
*/
var $ = function(id) { return document.getElementById(id) };
var isIE = navigator.userAgent.indexOf('MSIE') != -1 ? true : false;
var $extend = function(a, b) { for(var c in b) { a[c] = b[c]; } return a; };
var forEach = function(array, callback, thisp) {
if(array.forEach){
array.forEach(callback, thisp);
}else {
for(var i = 0, len = array.length; i < len; i++) {
if(i in array) callback.call(thisp, array[i], i, array);
}
}
};
var RevealTrans = function(cId, options) {
this.cId = cId;
this.timer = null;
this.curImg = null;
this.index = 1;
$extend(this, this.setOptions(options));
this.init();
};
RevealTrans.prototype = {
constructor: RevealTrans,
// 初始化函数
init: function() {
this.createStruct();
this.bindEvents();
},
// 设置默认参数
setOptions: function(options) {
this.options = {
auto: true, // 是否自动切换
transition: 23, // 滤镜参数(详见说明)
duration: 1.5, // 滤镜转换所用时间(单位为秒)
minOpa: 40, // 导航图片的初始透明度
maxOpa: 100, // 导航图片的最终透明度
pause: 2000, // 自动切换间隔时间
coll: [], // 图片集合
onEnd: function(){} // 图片滤镜转换结束自定义函数
};
return $extend(this.options, options || {});
},
// 生成HTML结构
createStruct: function() {
var _html = '', _this = this;
forEach(this.coll, function(O) {
_html += '<li><div><img src = ' + O + '></div></li>';
});
$(this.cId).innerHTML = _html;
$('context').innerHTML = '<img src=' + this.coll[0] + '>';
this.bindEvents();
},
// 设置透明度
setOpacity: function(O, opacity) {
if(!!isIE) O.style.filter = "alpha(opacity=" + opacity + ")";
else O.style.opacity = opacity / 100;
},
// 绑定相关事件
bindEvents: function() {
var imgs = $(this.cId).getElementsByTagName('img'), _this = this;
forEach(imgs, function(O, index) {
index > 0 ? _this.setOpacity(O, _this.minOpa) : _this.curImg = O;
O.onmouseover = function() { this.style.cursor = 'pointer'; };
O._index = index;
O.onclick = function() { _this.onStart(this); _this.index = this._index;};
});
// 默认演示第一个图片
this.onStart(imgs[0]);
},
// 开始滤镜切换
onStart: function(O) {
var _this = this, context = $('context').getElementsByTagName('img')[0];
_this.onStop();
_this.setOpacity(_this.curImg, _this.minOpa);_this.setOpacity(O, _this.maxOpa);
_this.curImg = O;
if(isIE) {
context.style.filter = "revealTrans()";
_this.transFx(context);
}
context.setAttribute('src', O.getAttribute('src'));
// 判断是否自动切换
if(!!this.auto) {
var len = this.coll.length;
_this.timer = setTimeout(function(){
_this.index < len ? _this.index++ : _this.index = 1;
_this.onStart($(_this.cId).getElementsByTagName('img')[_this.index - 1]);
}, this.pause);
}
},
// 滤镜演示
transFx: function(O) {
with(O.filters.revealTrans) {
Transition = parseInt(this.transition, 10); Duration = parseFloat(this.duration); apply(); play();
}
},
// 清除时间戳
onStop: function() {
clearInterval(this.timer);
}
};
</script>
</head>
<body>
<div id="container">
<div id="context"></div>
<ul id="nav"></ul>
</div>
<script>
new RevealTrans('nav', {coll:['1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg', '7.jpg', '8.jpg', '9.jpg', '10.jpg']});
</script>
</body>
</html>