当前位置:编程学习 > JS >>

同一页面调用并显示多个图片幻灯切换效果

同一页面多次调用图片幻灯切换效果,在一个网页上布置三个图片切换特效,虽然不常用,不过也有必要知道是怎么一回事吧。为了演示方便,这里只调用了一幅图片,显得有些单调,实际应用时就不会了。
答案:<!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>同一页面显示调用多个图片切换代码</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<script language="javascript">
var $o = new Object();
$o.base = {
	e:function(a,f){
		for(var i=0,j=a.length;i<j;i++){f.call(a[i],i);}
	}
}
$o. slide = function (id,arg){
	var arg = arg||{},
		t = document.getElementById(id),
		a = t.getElementsByTagName("a"),
		lis = [],
		cl = arg.color||'#f30',
		ctm = arg.time*1000||2000,
		w = t.clientWidth,
		h = t.clientHeight,
		b = ['<ul style="margin:0; padding:0; list-style:none; display:block; position:absolute; bottom:10px; right:10px;">'],
		index = 0,
		$$ = null,
		$ = null;
		function change(i){
			if(!!$$){clearTimeout($$);}
			index = !isNaN(i)?i:index+1;
			if(index >= a.length){index = 0;}
			$o.base.e(lis,function(k){if(k == index){c(1,this);}else{c(0,this)}});
			var to = - index*h;
			if(a[0].offsetTop == to){
				return;
			}else{
				if(!!$){clearInterval($);}
				$ = setInterval(function(){
					var ot = a[0].offsetTop;
					v = Math[to<ot?'floor':'ceil']((to - ot)*0.2);
					if(ot == to){clearInterval($);$=null;st();}
					ot += v;
					a[0].style.marginTop = ot + "px";
				},30)
			};
		}
		function c(b,o){
			o.style.backgroundColor = !!b?cl:"#fff";
			o.style.color = !!b?"#fff":cl;
		}
		function st(){
			if(!!$$)clearTimeout($$);
			$$ = setTimeout(function(){change()},ctm);
		}
		with(t.style){overflow = 'hidden';position = 'relative';}
		$o.base.e(a,function(n){
			this.style.display = "block";
			with(this.firstChild.style){borderWidth = '0';width = w + 'px';height = h + 'px';}
			b.push('<li style="width:20px; height:20px; line-height:20px; display:block; float:left; margin-left:5px; border:1px solid ' + cl + '; background-color:#fff; color:' + cl +'; font-size:14px; text-align:center; cursor:pointer;">' + (n+1) + '</li>');	
		});
		b.push('</ul>');
		t.innerHTML += b.join("");
		lis = t.getElementsByTagName("li");
		$o.base.e(lis,function(n){
			if(n == index){c(1,this)}
			this.onmouseover = function(){
				if(n!=index)change(n);
			}
		});
		st();
}
</script>
</head>
<body>
<div id="aa" style="width:502px;height:258px;">
	<a href="javascript:alert('1')"><img src="/jscss/demoimg/wall7.jpg"></a>
	<a href="javascript:alert('2')"><img src="/jscss/demoimg/wall7.jpg"></a>
	<a href="javascript:alert('3')"><img src="/jscss/demoimg/wall7.jpg"></a>
	<a href="javascript:alert('4')"><img src="/jscss/demoimg/wall7.jpg"></a>
	<a href="javascript:alert('5')"><img src="/jscss/demoimg/wall7.jpg"></a>
	<a href="javascript:alert('6')"><img src="/jscss/demoimg/wall7.jpg"></a>
	<a href="javascript:alert('7')"><img src="/jscss/demoimg/wall7.jpg"></a>
	<a href="javascript:alert('8')"><img src="/jscss/demoimg/wall7.jpg"></a>
	<a href="javascript:alert('9')"><img src="/jscss/demoimg/wall7.jpg"></a>
</div>
<script language="javascript">
new $o.slide("aa");
</script>
<br>
<div id="bb" style="width:402px;height:228px;">
	<a href="###"><img src="/jscss/demoimg/wall7.jpg"></a>
	<a href="###"><img src="/jscss/demoimg/wall7.jpg"></a>
	<a href="###"><img src="/jscss/demoimg/wall7.jpg"></a>
	<a href="###"><img src="/jscss/demoimg/wall7.jpg"></a>
	<a href="###"><img src="/jscss/demoimg/wall7.jpg"></a>
</div>
<script language="javascript">
new $o.slide("bb",{color:'#000',time:0.2});
</script>
<br>
<div id="cc" style="width:402px;height:228px;">
	<a href="###"><img src="/jscss/demoimg/wall7.jpg"></a>
	<a href="###"><img src="/jscss/demoimg/wall7.jpg"></a>
	<a href="###"><img src="/jscss/demoimg/wall7.jpg"></a>
	<a href="###"><img src="/jscss/demoimg/wall7.jpg"></a>
	<a href="###"><img src="/jscss/demoimg/wall7.jpg"></a>
</div>
<script language="javascript">
new $o.slide("cc",{color:'green'});
</script>
</body>
</html>

上一个:jQuery制作的产品展示效果
下一个:Js水平棋盘过渡效果的图片切换

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,