原生js自適應寬度焦點圖切換_自適應焦點圖切換代碼



215 858 287



特效描述:原生js 自適應寬度 焦點圖切換 自適應焦點圖切換,js自適應焦點輪播圖

代碼結構

1. 引入CSS

<link type="text/css" href="css/style.css" rel="stylesheet">

2. HTML代碼

<div class="index_banner" id="banner_tabs">
	<ul>
		<li><a href="http://www.dijiuzhanzhang.com" target="_blank"><img src="images/index_banner1.jpg" width="100%"></a></li>
		<li><a href="http://www.dijiuzhanzhang.com" target="_blank"><img src="images/index_banner2.jpg" width="100%"></a></li>      
		<li><a href="http://www.dijiuzhanzhang.com" target="_blank"><img src="images/index_banner3.jpg" width="100%"></a></li>    
		<li><a href="http://www.dijiuzhanzhang.com" target="_blank"><img src="images/index_banner4.jpg" width="100%"></a></li>
		<li><a href="http://www.dijiuzhanzhang.com" target="_blank"><img src="images/index_banner5.jpg" width="100%"></a></li>
		<li><a href="http://www.dijiuzhanzhang.com" target="_blank"><img src="images/index_banner6.jpg" width="100%"></a></li>
	</ul>
	<!--此處的img是用來占位的,在實際使用中,可以另外制作一張全空的圖片-->
	<img style="visibility:hidden;" src="images/index_banner1.jpg" width="100%">
	<cite>
		<span class="cur">1</span>
		<span>2</span>
		<span>3</span>
		<span>4</span>
		<span>5</span>
		<span>6</span>
	</cite>
	<div class="clear"></div>
</div>
<script type="text/javascript">
(function(){
	if(!Function.prototype.bind){
		Function.prototype.bind = function(obj){
			var owner = this,args = Array.prototype.slice.call(arguments),callobj = Array.prototype.shift.call(args);
			return function(e){e=e||top.window.event||window.event;owner.apply(callobj,args.concat([e]));};
		};
	}
})();
var banner_tabs = function(id){
	this.ctn = document.getElementById(id);
	this.adLis = null;
	this.btns = null;
	this.animStep = 0.2;//動畫速度0.1~0.9
	this.switchSpeed = 6;//自動播放間隔(s)
	this.defOpacity = 1;
	this.tmpOpacity = 1;
	this.crtIndex = 0;
	this.crtLi = null;
	this.adLength = 0;
	this.timerAnim = null;
	this.timerSwitch = null;
	this.init();
};
banner_tabs.prototype = {
	fnAnim:function(toIndex){
		if(this.timerAnim){window.clearTimeout(this.timerAnim);}
		if(this.tmpOpacity <= 0){
			this.crtLi.style.opacity = this.tmpOpacity = this.defOpacity;
			this.crtLi.style.filter = 'Alpha(Opacity=' + this.defOpacity*100 + ')';
			this.crtLi.style.zIndex = 0;
			this.crtIndex = toIndex;
			return;
		}
		this.crtLi.style.opacity = this.tmpOpacity = this.tmpOpacity - this.animStep;
		this.crtLi.style.filter = 'Alpha(Opacity=' + this.tmpOpacity*100 + ')';
		this.timerAnim = window.setTimeout(this.fnAnim.bind(this,toIndex),50);
	},
	fnNextIndex:function(){
		return (this.crtIndex >= this.adLength-1)?0:this.crtIndex+1;
	},
	fnSwitch:function(toIndex){
		if(this.crtIndex==toIndex){return;}
		this.crtLi = this.adLis[this.crtIndex];
		for(var i=0;i<this.adLength;i++){
			this.adLis[i].style.zIndex = 0;
		}
		this.crtLi.style.zIndex = 2;
		this.adLis[toIndex].style.zIndex = 1;
		for(var i=0;i<this.adLength;i++){
			this.btns[i].className = '';
		}
		this.btns[toIndex].className = 'cur'
		this.fnAnim(toIndex);
	},
	fnAutoPlay:function(){
		this.fnSwitch(this.fnNextIndex());
	},
	fnPlay:function(){
		this.timerSwitch = window.setInterval(this.fnAutoPlay.bind(this),this.switchSpeed*1000);
	},
	fnStopPlay:function(){
		window.clearTimeout(this.timerSwitch);
	},
	init:function(){
		this.adLis = this.ctn.getElementsByTagName('li');
		this.btns = this.ctn.getElementsByTagName('cite')[0].getElementsByTagName('span');
		this.adLength = this.adLis.length;
		for(var i=0,l=this.btns.length;i<l;i++){
			with({i:i}){
				this.btns[i].index = i;
				this.btns[i].onclick = this.fnSwitch.bind(this,i);
				this.btns[i].onclick = this.fnSwitch.bind(this,i);
			}
		}
		this.adLis[this.crtIndex].style.zIndex = 2;
		this.fnPlay();
		this.ctn.onmouseover = this.fnStopPlay.bind(this);
		this.ctn.onmouseout = this.fnPlay.bind(this);
	}
};
var player1 = new banner_tabs('banner_tabs');
</script>



用戶評論
大牛,別默默的看了,快登錄幫我點評一下吧!:)      登錄 | 注冊


熱門標簽: flash焦點圖 flash幻燈片 flashbanner flash圖片輪播 滑動選項卡 滑動切換 滾動切換 滾動條切換 滑動手風琴 選項卡切換 選項卡 切換 tab切換 頁面切換 選項卡插件 切換插件 全屏焦點圖 漸隱切換 全屏切換 淡出淡進 淡出 淡進 寬屏全屏 寬屏 全屏 頁面全屏 頁面寬屏 焦點圖 幻燈片 輪播圖 bar焦點圖 圖片淡出淡進 圖片淡出 圖片淡進 圖片全屏 純圖片輪播 圖片輪播 全屏焦點圖 選項卡自動切換 漸隱切換
?
×
×

注冊

官方QQ群

掃描上面二維碼加微信群

官方QQ群

jQuery/js討論群
群號:642649996
Css3+Html5討論群
群號:322131262

加群請備注:從官網了解到

老夫子电子