JS 實現banner圖片輪播效果(滑鼠事件),

來源:互聯網
上載者:User

JS 實現banner圖片輪播效果(滑鼠事件),

一.要實現的效果

1.點擊左右可切換圖片

2.點擊小圓點 可切換圖片

二.

三.代碼

1.css

<style type="text/css">body,img,span,ul,li{margin: 0;padding: 0;}#div1{width: 600px;height: 350px;margin: 150px auto;border: 10px solid #eee;position: relative;}img{width: 600px;height: 350px;}span{display: inline-block;position: absolute;width: 50px;height: 50px;border-radius: 25px;background:rgba(0,0,0,.3); font-size: 30px;line-height: 50px;text-align: center;cursor: pointer;color: white;}span:hover{background:rgba(255,255,255,.5);color:black;transform:scale(1.3);}span#L{left: 10px;top: 150px;}span#R{right: 10px;top: 150px;}#div1 #ul{position: absolute;bottom: 10px;left: 250px;width: 125px;height: 20px;}#div1 #ul li{list-style: none;float: left;height: 20px;width: 20px;border-radius: 10px;background:rgba(255,255,255,.5); margin-right: 5px;cursor: pointer;}#div1 #ul .active{background:rgba(0,0,0,.5);}</style>

2.html

<body>  <div id="div1">    <img src="" alt="">    <span id="L"><</span>    <span id="R">></span>    <ul id="ul">     <li></li>     <li></li>     <li></li>     <li></li>     <li></li>    </ul>  </div></body>

3.js

<script type="text/javascript">window.onload=function(){  var div1=document.getElementById('div1');  var oImg=div1.getElementsByTagName('img')[0];  var spanL=document.getElementById('L');  var spanR=document.getElementById('R');  var oUl=document.getElementById('ul');  var oLi=oUl.getElementsByTagName('li');  var arrImg=['img2/1.jpg','img2/2.jpg','img2/3.jpg','img2/4.jpg','img2/5.jpg'];  var num=0;  var oldLi=0;  function fn(num) {    oImg.src=arrImg[num];  }  fn(0);  function fnLi(num){    oLi[oldLi].className='';    oLi[num].className='active';    oldLi=num;  }  fnLi(0);  /*點擊左右span圖片切換*/  /*點擊左右span時li切換*/  spanL.onclick=function(){    if (num<1) {      num=arrImg.length-1;      fn(num);      fnLi(num);    }else{      num--;      fnLi(num);      fn(num);    }      }  spanR.onclick=function(){    if (num==arrImg.length-1) {      num=0;      fn(num);      fnLi(num);    }else{      num++;      fnLi(num);      fn(num);    }      }  /*點擊li實現圖片切換*/  for (var i = 0; i < oLi.length; i++) {    oLi[i].index=i;    oLi[i].onclick=function(){      fn(this.index);      fnLi(this.index);    }  }}</script>

總結

以上所述是小編給大家介紹的JS 實現banner圖片輪播效果(滑鼠事件),希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對幫客之家網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.