原生js实现——轮播图效果

实现点击左右箭头时滚动一张图片,当图片滚动到末尾时,响应的箭头变为不可点击状态,并实现图片随着鼠标滚动进行响应的滚动。

<!DOCTYPE HTML>
<html lang="en">
<head>
    <title>图片轮播展示</title>
    <meta charset="utf-8">
    <style type="text/css">
      *{
      	padding:0;
      	margin:0;
      }
      #slide{
      	width:900px;
      	height:200px;
      	position:relative;
      	border:1px solid #ccc;
      	margin:20px auto;
      }
      .content{
      	position: absolute;
      	width:740px;
      	height:180px;
      	border:1px solid #ccc;
      	overflow: hidden;
        margin:auto;
      	left:0;
      	right:0;
      	top:0;
      	bottom:0;
      }
      ul li{
      	list-style: none;
      	width:100px;
      	height:160px;
      	float:left;
      	margin-left: 20px;
      	margin-top:10px;
      }
      ul li img{
      	width:100px;
      	height:160px;      	
      }
      #slide ul{ 
        position:absolute;    
        -webkit-transition:all .27s ease-in;
        -moz-transition:all .27s ease-in;
        -o-transition:all .27s ease-in;
        transition:all .27s ease-in;
      }
      .move{
      	width:50px;
      	height:100px;
      	cursor: pointer;
      	margin-top: 40px;
      }
      #prev{
      	float:left;
      }
      #next{
      	float:right;
      }
    </style>
    
</head>
<body>
  <div id="slide" >
    <div >
      <img id="prev"class="move"src="images/prev.png"href="javascript:;">
    </div>
    <div class="content">
      <ul>
      	<li><img src="images/1.jpg" alt=""></li>
      	<li><img src="images/2.jpg" alt=""></li>
      	<li><img src="images/3.jpg" alt=""></li>
      	<li><img src="images/4.jpg" alt=""></li>
      	<li><img src="images/5.jpg" alt=""></li>
      	<li><img src="images/6.jpg" alt=""></li>
      	<li><img src="images/7.jpg" alt=""></li>
      	<li><img src="images/8.jpg" alt=""></li>
      	<li><img src="images/9.jpg" alt=""></li>
      </ul>
    </div>
    <div >
      <img id="next" class="move"src="images/next.png" href="javascript:;" >
    </div>
  </div>
  <script type="text/javascript">
    var obj = document.getElementById('slide');
    var next = document.getElementById('next'),
        prev = document.getElementById('prev'),
        ul = obj.getElementsByTagName('ul')[0],
        liArr = obj.getElementsByTagName('li'),
        li_width = liArr[0].offsetWidth + 20,
        li_length = liArr.length,
        show_length = 6,
        index = 0;
    function slide(){
        ul.style.width = li_width * li_length + 'px';
        ul.style.left = 0;
    }
    function animation(){
        if(index <= 0){
            prev.style.cursor = 'default';
            next.style.cursor = 'pointer';
            index = 0;
        }else if(index >= (li_length - show_length)){
            prev.style.cursor = 'pointer';
            next.style.cursor = 'default';
            index = (li_length -show_length);
        }
        var goal = li_width * -index;
        ul.style.left = goal + 'px';
    }
    obj.onmousewheel = function(e){
        var d = parseInt(e.wheelDelta)> 0 ? -1:1;  //判断滚动方向
        index += 1*d;
        animation();
    }
    next.onclick = function(e){
        var target = e.target;
        prev.style.cursor = 'pointer';
        index += 1;
        animation();
    }
    prev.onclick = function(e){
        var target = e.target;
        next.style.cursor = 'pointer';
        index -= 1;
        animation();
    }
    slide();
    </script>
</body>
</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值