js图片滚动实例

纯js实现的图片滚动的代码

html部分

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
<link href="css/css.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jb51zone.js"></script>
</head>
<body>
<!-- 代码 开始 -->
  <div id="playBox">
    <div id="pre"></div>              //前进后退按钮
    <div id="next"></div>
    <div id="smalltitle">
      <ul>                            //圆点部分
        <li class="thistitle"></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </div>
    <ul id="oUlplay">                //滚动的图片,width=1000px   height=409px
       <li><a href="http://www.jb51.net/" target="_blank"><img src="images/1.jpg"</a></li>
       <li><a href="http://www.jb51.net/" target="_blank"><img src="images/2.jpg"</a></li>
       <li><a href="http://www.jb51.net/" target="_blank"><img src="images/3.jpg"</a></li>
       <li><a href="http://www.jb51.net/" target="_blank"><img src="images/4.jpg"</a></li>
       <li><a href="http://www.jb51.net/" target="_blank"><img src="images/5.jpg"</a></li>
       <li><a href="http://www.jb51.net/" target="_blank"><img src="images/6.jpg"</a></li>
    </ul>
  </div>
<!-- 代码 结束 -->
</body>
</html>


CSS部分

*{ margin:0; padding:0; list-style:none;}
#divSmallBox{overflow:hidden;*display:inline;*zoom:1;width:10px;height:10px;margin:0 5px;border-radius:10px;background:#ffffff;}
#playBox{ width:1000px; height:409px; margin:20px auto; background:#333; position:relative; overflow:hidden;}
#playBox #oUlplay { width:99999px; position:absolute; left:0; top:0;}
#playBox #oUlplay li{ float:left;}
#playBox #pre{ cursor:pointer; width:45px; height:45px; background:url(../images/l.png) no-repeat; position:absolute; top:190px; left:10px; z-index:10;}
#playBox #next{ cursor:pointer; width:45px; height:45px; background:url(../images/r.png) no-repeat; position:absolute; top:190px; right:10px; z-index:10;}
#playBox #smalltitle {width:1000px; height:10px; position:absolute; bottom:15px; z-index:10}
#playBox #smalltitle ul{ width:120px; margin:0 auto;}
#playBox #smalltitle ul li{ width:10px; height:10px; margin:0 5px; border-radius:10px; background:#ffffff; float:left; overflow:hidden;*display:inline;*zoom:1;}
#playBox #smalltitle .thistitle{ background:#69aaec;}



js部分

function getStyle(obj,name)                                   //获取样式值,这里是left的值  兼容IE
{
	if(obj.currentStyle)
	{
		return obj.currentStyle[name]
	}
	else
	{
		return getComputedStyle(obj,false)[name]
	}
}

function startMove(obj,add)                                   //图片减速滚动效果实现
{
	clearInterval(obj.timer)
	obj.timer = setInterval(function(){
	   var cutt = 0 ;
	   cutt = Math.round(parseInt(getStyle(obj,"left")));
	   var speed = (add-cutt)/4;
	   speed = speed>0?Math.ceil(speed):Math.floor(speed);
	   if(cutt==add)
	   {
		   clearInterval(obj.timer)
	   }
	   else
	   {
			obj.style['left'] = cutt+speed+'px';
		   
	   }
	   
	},30)
}

  window.onload = function()
  {
	  var oDiv = document.getElementById('playBox');
	  var oPre =document.getElementById('pre');
	  var oNext =document.getElementById('next');
	  var oUlBig =document.getElementById('oUlplay');
	  var aBigLi = oUlBig.getElementsByTagName('li');
	  var oDivSmall =document.getElementById('smalltitle');smalltitle
	  var aLiSmall = oDivSmall.getElementsByTagName('li');
	  
	  function tab()
	  {
	     for(var i=0; i<aLiSmall.length; i++)
	     {
		    aLiSmall[i].className = '';
	     }
	     aLiSmall[now].className = 'thistitle'
	     startMove(oUlBig,-(now*aBigLi[0].offsetWidth))
	  }
	  var now = 0;
	  for(var i=0; i<aLiSmall.length; i++)
	  {
		  aLiSmall[i].index = i;
		  aLiSmall[i].onclick = function()
		  {
			  now = this.index;
			  tab();
		  }
	 }
	  oPre.onclick = function()
	  {
		  now--
		  if(now ==-1)
			  now = aBigLi.length;
		  tab();
	  }
	   oNext.onclick = function()
	  {
		  now++
		  if(now ==aBigLi.length)
		     now = 0;
		  tab();
	  }
	  var timer = setInterval(oNext.onclick,3000) 
	  oDiv.onmouseover = function()
	  {
		  clearInterval(timer)
	  }
	   oDiv.onmouseout = function()
	  {
		  timer = setInterval(oNext.onclick,3000)
	  }
  }




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值