溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

js實(shí)現(xiàn)橫向輪播圖的詳細(xì)代碼分享

發(fā)布時(shí)間:2020-04-22 10:41:31 來(lái)源:億速云 閱讀:260 作者:小新 欄目:web開(kāi)發(fā)

今天小編給大家分享的是js實(shí)現(xiàn)橫向輪播圖的詳細(xì)代碼,相信很多人都不太了解,為了讓大家更加了解js實(shí)現(xiàn)橫向輪播圖的詳細(xì)代碼,所以給大家總結(jié)了以下內(nèi)容,一起往下看吧。一定會(huì)有所收獲的哦。

js實(shí)現(xiàn)橫向輪播圖的詳細(xì)代碼分享

描述:

輪播圖,初級(jí),橫向buton或者底部數(shù)字控制輪播,可以實(shí)現(xiàn)自動(dòng)輪播(注釋了,使用的話(huà)將其注釋消掉),核心知識(shí)是數(shù)據(jù)驅(qū)動(dòng)圖像的位移達(dá)到效果。

js代碼:

/*
* 工廠模式
* */
  
var Method=(function () {
 return {
 loadImage:function (arr,callback) {
  var img=new Image();
  img.arr=arr;
  img.list=[];
  img.num=0;
  img.callback=callback;
//  如果DOM對(duì)象下的事件偵聽(tīng)沒(méi)有被刪除掉,將會(huì)常駐堆中
//  一旦觸發(fā)了這個(gè)事件需要的條件,就會(huì)繼續(xù)執(zhí)行事件函數(shù)
  img.addEventListener("load",this.loadHandler);
  img.self=this;
  img.src=arr[img.num];
 },
  
 loadHandler:function (e) {
  this.list.push(this.cloneNode(false));
  this.num++;
  if(this.num>this.arr.length-1){
  this.removeEventListener("load",this.self.loadHandler);
  this.callback(this.list);
  return;
  }
  this.src=this.arr[this.num];
 },
  
 $c:function (type,parent,style) {
  var elem=document.createElement(type);
  if(parent) parent.appendChild(elem);
  for(var key in style){
  elem.style[key]=style[key];
  }
  return elem;
 }
 }
})();

()

html代碼:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <style>
 *{
  margin: 0;
  padding: 0;
 }
 #carousel,#imgCon img{
  width: 1200px;
  height: 400px;
 }
 #carousel
 {
  position: relative;
  margin: auto;
  overflow: hidden;
 }
 #imgCon{
  width: 6000px;
  height: 400px;
  position: absolute;
  left: 0;
  font-size: 0;
  transition: all 1s;
 }
 #leftBn,#rightBn
 {
  position: absolute;
  top:170px;
 }
 #leftBn{
  left: 20px;
 }
 #rightBn
 {
  right: 20px;
 }
 ul{
  position: absolute;
  bottom: 20px;
  list-style: none;
  margin: auto;
  left: 45%;
 }
 li
 {
  width: 20px;
  height: 20px;
  border: 1px solid red;
  border-radius: 10px;
  float: left;
  text-align: center;
  color: white;
  cursor: default;
  line-height:20px;
  font-size: 12px;
  margin-left: 8px;
 }
 </style>
</head>
<body>
 <div id="carousel">
 <div id="imgCon">
  <img src="img/a.jpeg">
  <img src="img/b.jpeg">
  <img src="img/c.jpeg">
  <img src="img/d.jpeg">
  <img src="img/e.jpeg">
 </div>
 <ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
 </ul>
 <img src="img/left.png" id="leftBn">
 <img src="img/right.png" id="rightBn">
 </div>
<script>
 /*
 *
 * 數(shù)據(jù)驅(qū)動(dòng)顯示
 *
 * */
 var imgCon,leftBn,rightBn,lis,ul,prevLi;
 var position=0;//圖像的標(biāo)記 第一張0 第二張1...
 init();
 function init() {
 imgCon=document.getElementById("imgCon");//圖
 leftBn=document.getElementById("leftBn");//左按鈕
 rightBn=document.getElementById("rightBn");//右按鈕
 lis=document.getElementsByTagName("li");//下方數(shù)字右按鈕
 ul=document.getElementsByTagName("ul")[0];
 leftBn.addEventListener("click",clickHandler);
 rightBn.addEventListener("click",clickHandler);
 for(var i=0;i<lis.length;i++){//為每隔小Li 也就是底部數(shù)字賦值
  lis[i].num=i;
  lis[i].addEventListener("click",liClickHandler);
 }
 changeLi();
 }
  
// setInterval(autoImg,3000);可以實(shí)現(xiàn)自動(dòng)
  
 function autoImg() {//自動(dòng)輪播
 position++;
 if(position>4) position=0;
 changeImg();
 }
  
 function clickHandler(e) {
 e= e || window.event;
 if(this===leftBn){
  position--;
  if(position<0) position=4;
 }else if(this===rightBn){
  position++;
  if(position>4) position=0;
 }
 changeImg();
 }
  
 function liClickHandler(e) {
 e= e || window.event;
 position=this.num;
 changeImg();
 }
 function changeImg() {//圖片的轉(zhuǎn)換效果 唯一
 imgCon.style.left=-position*1200+"px";//一次一張圖片的位移
 changeLi();
 }
  
 function changeLi() {//底部數(shù)字的轉(zhuǎn)換效果
 if(prevLi){
  prevLi.style.backgroundColor="rgba(255,0,0,0)";
 }
 prevLi=lis[position];
 prevLi.style.backgroundColor="rgba(255,0,0,0.5)";
 }
</script>
</body>
</html>

以上就是js實(shí)現(xiàn)橫向輪播圖的詳細(xì)代碼的詳細(xì)內(nèi)容了,看完之后是否有所收獲呢?如果想了解更多相關(guān)內(nèi)容,歡迎來(lái)億速云行業(yè)資訊

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

js
AI