一款比较好看的javascript轮播图代码

本文提供了一段JavaScript实现的轮播图效果的完整代码,包括左右按钮控制、自动播放、图片切换等功能。通过点击小图标或使用导航按钮,可以平滑地在多张图片之间切换,同时底部的文字描述也会相应更新。代码中还包含了一些CSS样式以实现视觉效果。适合前端开发者学习和参考。
摘要由CSDN通过智能技术生成

好看的javascript轮播图效果

完整代码如下:

window.οnlοad=function ()
{
	var oDiv=document.getElementById('box');
	var aPicLi=document.getElementById('pic_list').getElementsByTagName('li');
	var aTxtLi=document.getElementById('text_list').getElementsByTagName('li');
	var oIcoUl=document.getElementById('ico_list').getElementsByTagName('ul')[0];
	var aIcoLi=document.getElementById('ico_list').getElementsByTagName('li');
	var oBtnPrev=document.getElementById('btn_prev');
	var oBtnNext=document.getElementById('btn_next');
	var iNowUlLeft=0;
	var iNow=0;
	var i=0;
	
	oBtnPrev.οnclick=function ()
	{
		if(iNowUlLeft>0)
		{
			iNowUlLeft--;

			fixUlLeft();
		}
	};
	
	function fixUlLeft()
	{
		oBtnPrev.className=iNowUlLeft==0?'btn':'btn showBtn';
		oBtnNext.className=iNowUlLeft==(aIcoLi.length-7)?'btn':'btn showBtn';
		miaovStartMove(oIcoUl, {left: -aIcoLi[0].offsetWidth*iNowUlLeft}, MIAOV_MOVE_TYPE.BUFFER);
	}
	
	oBtnNext.οnclick=function ()
	{
		if(iNowUlLeft<aIcoLi.length-7)
		{
			iNowUlLeft++;
			
			fixUlLeft();
		}
	};
	
	for(i=0;i<aIcoLi.length;i++)
	{
		aIcoLi[i].index=i;
		aIcoLi[i].οnclick=function ()
		{
			if(iNow==this.index)
			{
				return;
			}
			
			iNow=this.index;
			
			tab();
		};
	}
	
	function tab()
	{
		for(i=0;i<aIcoLi.length;i++)
		{
			aIcoLi[i].className='';
			aTxtLi[i].getElementsByTagName('h2')[0].className='';
			aPicLi[i].style.filter='alpha(opacity:0)';
			aPicLi[i].style.opacity=0;
			miaovStopMove(aPicLi[i]);
		}
		aIcoLi[iNow].className='active';
		aTxtLi[iNow].getElementsByTagName('h2')[0].className='show';
		miaovStartMove(aPicLi[iNow], {opacity: 100}, MIAOV_MOVE_TYPE.BUFFER);
	}
	
	function autoPlay()
	{
		iNow++;
		
		if(iNow>=aIcoLi.length)
		{
			iNow=0;
		}
		
		if(iNow<iNowUlLeft)
		{
			iNowUlLeft=iNow;
		}
		else if(iNow>=iNowUlLeft+7)
		{
			iNowUlLeft=iNow-6;
		}
		
		fixUlLeft();
		tab();
	}
	
	var timer=setInterval(autoPlay, 3000);
	
	oDiv.οnmοuseοver=function ()
	{
		clearInterval(timer);
	};
	
	oDiv.οnmοuseοut=function ()
	{
		timer=setInterval(autoPlay, 3000);
	};
};
body,ul,h2{margin:0;padding:0; font:12px/20px Tahoma,"hiragino sans gb",Helvetica,Arial;}
img{border:none;}
li{list-style:none;}
body{background:#101010;}
#box{width:660px;height:330px;position:relative;overflow:hidden;margin:60px auto 0;}
#pic_list{position:relative;z-index:1;}
#pic_list li{position:absolute;left:0;top:0;opacity:0;fliter:alpha(opacity=0);z-index:1;}
.mark{height:90px;width:660px;background:#000;position:absolute;left:0;bottom:0;opacity:0.3;filter:alpha(opacity=30);}
#text_list{position:absolute;bottom:60px;left:50px;position:absolute;z-index:5;height:20px;overflow:hidden;}
#text_list h2{display:none;}
#text_list .show{display:block;}
#text_list a{color: #FFFFFF;font-family: "Microsoft YaHei";font-size: 18px;font-weight: normal;text-decoration:none;}
#ico_list{position:absolute;bottom:10px;left:12px;width:525px;overflow:hidden;height:46px;z-index:3;}
#ico_list ul{width:1050px;position:absolute;left:0;top:0;}
#ico_list li{width:75px;float:left;}
#ico_list li a{width:68px;padding-top:6px;display:block;}
#ico_list li a img{border: 2px solid #DFE8E4; height:36px;width:64px;background:#040303;opacity:0.7;filter:alpha(opacity=70);}
#ico_list .active{background:url(../img/a_hover.gif) no-repeat center 0;}
#ico_list .active img{opacity:1;filter:alpha(opacity=100);border: 3px solid #fff; height:34px;width:62px;}
.btn{background:url(../img/btn.gif) no-repeat;height:38px;width:38px;position: absolute;bottom:11px; opacity:0.5;filter:alpha(opacity=50);cursor:default;z-index:3;}
.showBtn{ opacity:1;filter:alpha(opacity=100);cursor:pointer;z-index:4;}
#btn_prev{right:56px;}
#btn_next{right:20px;background-position:right 0;}

学习资料地址: 幻.zip - 蓝奏云

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的JavaScript轮播图代码,你可以根据需要进行修改: HTML: ```html <div class="slideshow-container"> <div class="slide fade"> <img src="slide1.jpg"> </div> <div class="slide fade"> <img src="slide2.jpg"> </div> <div class="slide fade"> <img src="slide3.jpg"> </div> </div> <div class="dots-container"> <span class="dot"></span> <span class="dot"></span> <span class="dot"></span> </div> ``` CSS: ```css .slideshow-container { max-width: 1000px; position: relative; margin: auto; } .fade { -webkit-animation-name: fade; -webkit-animation-duration: 1s; animation-name: fade; animation-duration: 1s; } @-webkit-keyframes fade { from { opacity: .4 } to { opacity: 1 } } @keyframes fade { from { opacity: .4 } to { opacity: 1 } } .dots-container { text-align: center; margin-top: 20px; } .dot { height: 15px; width: 15px; margin: 0 5px; border-radius: 50%; display: inline-block; background-color: #bbb; } .active { background-color: #717171; } ``` JavaScript: ```js var slideIndex = 1; showSlides(slideIndex); // Next/previous controls function plusSlides(n) { showSlides(slideIndex += n); } // Thumbnail image controls function currentSlide(n) { showSlides(slideIndex = n); } function showSlides(n) { var i; var slides = document.getElementsByClassName("slide"); var dots = document.getElementsByClassName("dot"); if (n > slides.length) {slideIndex = 1} if (n < 1) {slideIndex = slides.length} for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } for (i = 0; i < dots.length; i++) { dots[i].className = dots[i].className.replace(" active", ""); } slides[slideIndex-1].style.display = "block"; dots[slideIndex-1].className += " active"; } ``` 这个代码创建了一个基本的轮播图,其中包含三张片和三个圆点,用户可以点击圆点或箭头来切换片。你可以根据需要进行修改,例如改变片数量,调整样式等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值