http://www.cnblogs.com/cloudgamer/archive/2008/05/13/1194272.html
效果:
代码下载:
test.htm (8.74 K)
程序原理:
设计之初,先不要考虑文本显示那部分,把重心放到滑动效果的实现上,
减少设计负担,这是小小的技巧吧。
总体的思路是通过移动各个滑动对象到指定的位置来实现不同的效果。
效果有两种,分部是:鼠标移出容器时的默认效果和鼠标移到某个滑动对象时的展示效果。
根据效果可以看出,滑动对象有三种宽度一个是默认宽度,最大宽度和最小宽度。
这些宽度都可以在初始化时设置:
看到jQuery实例:图片展示效果后,我也想拿来试试,但我不太喜欢用框架,所以自己做了个。
其中的难点在于怎么设计各个滑动对象进行想要的滑动。
一开始我想的是利用滑动对象的宽度的变化来实现滑动,
但实行起来发现这个只能在理想状态下实现,一般实现起来很困难。
所以还是参照了实例中的方法,利用left的变化来实现滑动。
this._list = oContainer.getElementsByTagName(sTag);
len = this._list.length;
this._count = len;
this._width = parseInt(iWidth / len);
this._width_max = parseInt(iMaxWidth);
this._width_min = parseInt((iWidth - this._width_max) / (len - 1));
我给每个滑动对象添加了一个_target属性来放一个目标值,用来告诉它要滑到哪里。
移动之前先执行Set()程序,给每个对象设置目标值,要分两种情况:
1.鼠标移出容器:这时只要设置滑动对象的目标值为默认宽度*索引值就行;
oList._target = this._width * i;
2.鼠标移到某个滑动对象上:把当前对象和之前的滑动对象的目标值设为最小宽度*索引值,之后的对象设为最小宽度*(索引值-1)加上最大宽度(因为这样会算多一个最小宽度所以要减1个),这样设置就能得到要实现的效果:
oList._target = (i <= index) ? this._width_min * i : this._width_min * (i - 1) + this._width_max;
接下来就是怎么移动到目标值了,这个在Move()程序中实现。
首先移动效果是通过用计时器逐步设置滑动对象的left来实现,减速部分用一个GetStep()程序来实现(相关说明请看JavaScript 图片切换展示效果)。
不断移动,直到所有滑动对象都到达目标值:
clearTimeout(this._timer);
var bFinish = true;
this.Each(function(oList, oText, i){
var nowLeft = parseInt(oList.style.left), iLeftStep = this.GetStep(oList._target, nowLeft);
if (iLeftStep != 0) { bFinish = false; oList.style.left = (nowLeft + iLeftStep) + "px"; }
})
if (!bFinish) { var oThis = this; this._timer = setTimeout(function(){ oThis.Move(); }, this.Time); }
这个程序本身的难度不高,就难在程序设计,例如怎么实现变化的效果(由于有实例难度就没那么高了)。
还有一个思想是把“移动到指定目标”这个任务分派到各个滑动对象,
主程序只要知道是否各个滑动对象都到达指定目标就行了,
这在给滑动对象设置_target属性和Each()程序的应用中能体现出来。
如果每次移动都由主程序来计算(试想想没有这个_target属性),难度会大大增加,
而设计之初很容易会陷入这个死胡同。
还有一点是容器的mouseout事件中,要先判断鼠标是否在容器外(相关说明请看JavaScript 自定义多级联动浮动菜单)。
扩展功能:
可以设置的属性:
Step: 滑动变化率;
Time: 滑动延时;
TextTag: 说明容器tag;
TextHeight: 说明容器高度;
Delay: 延迟值(微秒);
Showtext:是否显示说明文本;
能实现什么功能就看各位的想象力了。
程序测试:
实例化对象:
new GlideView("idGlideView", 1000, "div", 500, { TextTag: "a", TextHeight: 50 });
其中参数分别是容器对象,容器宽度,展示标签,展示宽度,最后是一些设置。
完整代码:
<div class="post">
<div class="postcontent">
<script type="text/javascript">
var $ = function (id) {
return "string" == typeof id ? document.getElementById(id) : id;
};
function Event(e){
var oEvent = document.all ? window.event : e;
if (document.all) {
if(oEvent.type == "mouseout") {
oEvent.relatedTarget = oEvent.toElement;
}else if(oEvent.type == "mouseover") {
oEvent.relatedTarget = oEvent.fromElement;
}
}
return oEvent;
}
function addEventHandler(oTarget, sEventType, fnHandler) {
if (oTarget.addEventListener) {
oTarget.addEventListener(sEventType, fnHandler, false);
} else if (oTarget.attachEvent) {
oTarget.attachEvent("on" + sEventType, fnHandler);
} else {
oTarget["on" + sEventType] = fnHandler;
}
};
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}
Object.extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
}
var GlideView = Class.create();
GlideView.prototype = {
//容器对象 容器宽度 展示标签 展示宽度
initialize: function(obj, iWidth, sTag, iMaxWidth, options) {
var oContainer = $(obj), oThis=this, len = 0;
this.SetOptions(options);
this.Step = Math.abs(this.options.Step);
this.Time = Math.abs(this.options.Time);
this.Showtext = false;
this._list = oContainer.getElementsByTagName(sTag);
len = this._list.length;
this._count = len;
this._width = parseInt(iWidth / len);
this._width_max = parseInt(iMaxWidth);
this._width_min = parseInt((iWidth - this._width_max) / (len - 1));
this._timer = null;
if(this.options.TextTag){
this.Showtext = true;
this._text = oContainer.getElementsByTagName(this.options.TextTag);
this._height_text = -parseInt(this.options.TextHeight);
}
this.Each(function(oList, oText, i){
oList._target = this._width * i;//自定义一个属性放目标left
oList.style.left = oList._target + "px";
oList.style.position = "absolute";
addEventHandler(oList, "mouseover", function(){ oThis.Set.call(oThis, i); });
if(oText){
oText._target = this._height_text;//自定义一个属性放目标bottom
oText.style.bottom = oText._target + "px";
oText.style.position = "absolute";
}
})
oContainer.style.width = iWidth + "px";
oContainer.style.overflow = "hidden";
oContainer.style.position = "relative";
addEventHandler(oContainer, "mouseout", function(e){
//变通防止执行oList的mouseout
var o = Event(e).relatedTarget;
if (oContainer.contains ? !oContainer.contains(o) : oContainer != o && !(oContainer.compareDocumentPosition(o) & 16)) oThis.Set.call(oThis, -1);
})
},
//设置默认属性
SetOptions: function(options) {
this.options = {//默认值
Step: 20,//滑动变化率
Time: 20,//滑动延时
TextTag: "",//说明容器tag
TextHeight: 0//说明容器高度
};
Object.extend(this.options, options || {});
},
//
Set: function(index) {
if (index < 0) {
this.Each(function(oList, oText, i){ oList._target = this._width * i; if(oText){ oText._target = this._height_text; } })
} else {
this.Each(function(oList, oText, i){
oList._target = (i <= index) ? this._width_min * i : this._width_min * (i - 1) + this._width_max;
if(oText){ oText._target = (i == index) ? 0 : this._height_text; }
})
}
this.Move();
},
//
Move: function() {
clearTimeout(this._timer);
var bFinish = true;
this.Each(function(oList, oText, i){
var nowLeft = parseInt(oList.style.left), iLeftStep = this.GetStep(oList._target, nowLeft);
if (iLeftStep != 0) { bFinish = false; oList.style.left = (nowLeft + iLeftStep) + "px"; }
if (oText) {
var nowBottom = parseInt(oText.style.bottom), iBottomStep = this.GetStep(oText._target, nowBottom);
if (iBottomStep != 0) { bFinish = false; oText.style.bottom = (nowBottom + iBottomStep) + "px"; }
}
})
if (!bFinish) { var oThis = this; this._timer = setTimeout(function(){ oThis.Move(); }, this.Time); }
},
//
GetStep: function(iTarget, iNow) {
var iStep = (iTarget - iNow) / this.Step;
if (iStep == 0) return 0;
if (Math.abs(iStep) < 1) return (iStep > 0 ? 1 : -1);
return iStep;
},
Each:function(fun) {
for (var i = 0; i < this._count; i++)
fun.call(this, this._list[i], (this.Showtext ? this._text[i] : null), i);
}
};
</script>
<style type="text/css">
#idGlideView{height:100px;}
#idGlideView div{width:500px;height:100px;}
#idGlideView div a{width:500px;height:50px;filter: alpha(opacity=50);opacity: 0.5; background:#000; color:#fff; text-decoration:none;}
#idGlideView2 {
height:380px;
border:1px solid #000;
}
#idGlideView2 div {
width:500px;
height:380px;
}
</style>
<div id="idGlideView2">
<div><img alt="" src="http://images.cnblogs.com/cnblogs_com/cloudgamer/143727/r_mm1.jpg" /></div>
<div style="border-left: #000 1px solid"><img alt="" src="http://images.cnblogs.com/cnblogs_com/cloudgamer/143727/r_mm2.jpg" /></div>
<div style="border-left: #000 1px solid"><img alt="" src="http://images.cnblogs.com/cnblogs_com/cloudgamer/143727/r_mm3.jpg" /></div>
<div style="border-left: #000 1px solid"><img alt="" src="http://images.cnblogs.com/cnblogs_com/cloudgamer/143727/r_mm4.jpg" /></div>
<div style="border-left: #000 1px solid"><img alt="" src="http://images.cnblogs.com/cnblogs_com/cloudgamer/143727/r_mm5.jpg" /></div>
</div>
<br />
<br />
<div id="idGlideView">
<div style="background-color: #006699"><a href="http://www.cnblogs.com/">1111111</a> </div>
<div style="background-color: #ff9933"><a href="http://www.cnblogs.com/">2222222</a> </div>
<div style="background-color: #9999ff"><a href="http://www.cnblogs.com/">3333333</a> </div>
<div style="background-color: #006699"><a href="http://www.cnblogs.com/">1111111</a> </div>
<div style="background-color: #ff9933"><a href="http://www.cnblogs.com/">2222222</a> </div>
<div style="background-color: #9999ff"><a href="http://www.cnblogs.com/">3333333</a> </div>
</div>
<br />
<br />
<select id="sel">
<option value="-1" selected>不展开</option>
</select> <br />
<br />
<input id="up" type="button" value=" + 加 速 " /> <input id="down" type="button" value=" - 减 速 " /> <br />
<br />
<input id="stop" type="button" value=" 停 止 " /> <input id="start" type="button" value=" 开 始 " /> <br />
<br />
<input id="close" type="button" value=" 关闭滚动 " /> <input id="open" type="button" value=" 恢复滚动 " /> <br />
<br />
<input id="auto" type="button" value=" 自动滑动 " /> <input id="cancel" type="button" value=" 取消自动 " /> <br />
<br />
<input id="hide" type="button" value=" 隐藏说明 " /> <input id="show" type="button" value=" 显示说明 " /> <br />
<br />
<script>new GlideView("idGlideView2", 700, "div", 500);
var gv = new GlideView("idGlideView", 700, "div", 400, { TextTag: "a", TextHeight: 50 });
var oSel = $("sel");
for (var i = 0; i <= gv._count; i++) {
var op = document.createElement("OPTION");
op.value = i; op.innerHTML = "展开第" + (i + 1) + "个";
oSel.appendChild(op);
}
oSel.onchange = function(){ gv.Set(oSel.value); }
$("up").onclick = function(){ gv.Step -= 5; if(gv.Step <= 0) gv.Step = 1; };
$("down").onclick = function(){ gv.Step += 5; if(gv.Step >= 500) gv.Step = 50; };
$("stop").onclick = function(){ clearTimeout(gv._timer); };
$("start").onclick = function(){ gv.Move(); };
$("close").onclick = function(){ gv.Step = 1; };
$("open").onclick = function(){ gv.Step = gv.options.Step; };
$("hide").onclick = function(){ gv.Showtext = false; };
$("show").onclick = function(){ gv.Showtext = true; };
var t = null, i = -1;
$("auto").onclick = function(){ clearInterval(t);t=setInterval(function(){ if(++i>gv._count) i=0; gv.Set(i); }, 1000); };
$("cancel").onclick = function(){ clearInterval(t);gv.Set(-1); };
</script><br />
test.htm 2008-10-04_062401.gif