溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

使用jQuery怎么實現一個輪播圖

發布時間:2021-06-11 17:54:21 來源:億速云 閱讀:170 作者:Leah 欄目:web開發

本篇文章給大家分享的是有關使用jQuery怎么實現一個輪播圖,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

1、html+css+js代碼

<!DOCTYPE html>
<html>
<head>
 <title></title>
 <style type="text/css">
 *{
  margin: 0;
  padding: 0;
  text-decoration: none;
 }
 body{
  padding: 20px;
 }
 #container{
  min-width: 1000px;
  /*width: 1300px;*/
  height: 400px;
  overflow: hidden;
  position: relative;
  margin: 0 auto;
 }
 #list{
  /*width: 9100px;*/
  height: 400px;
  position: absolute;
  z-index: 1;
  top:0;
  left: 0;
  overflow: hidden;
 }
 #list img{
  float: left;
  /*width: 1300px;*/
  height: 400px;
 }
 #buttons{
  position: absolute;
  height: 10px;
  width: 100px;
  z-index: 2;
  bottom: 20px;
  left: 660px;
  text-align: center;
 }
 #buttons span{
  cursor: pointer;
  float: left;
  width: 10px;
  height: 10px;
  margin-right: 9px;
  display: inline-block;
  background-image: url(img/_eb1b95e.png);
  background-position: -1079px -687px;

 }
 #buttons .on{
  background-position: -1049px -687px;
 }
 .arrow{
  cursor: pointer;
  display: none;
  width: 36px;
  height: 76px;
  position: absolute;
  z-index: 2;
  top: 180px;
  background-color: rgba(0,0,0,.3);
  color: #fff;
 }
 #container:hover .arrow{
  display: block;
 }
 #prev{
  background: url(img/_eb1b95e.png);
  background-position: -569px -491px;
  left: 20px;
 }
 #next{
  background: url(img/_eb1b95e.png);
  background-position: -513px -491px;
  right: 20px;
 }
 </style>
</head>
<body>
 <div id="container">
 <div id="list" >
  <img src="img/5.jpg" alt="1"/>
  <img src="img/1.jpg" alt="1"/>
  <img src="img/2.jpg" alt="2"/>
  <img src="img/3.jpg" alt="3"/>
  <img src="img/4.jpg" alt="4"/>
  <img src="img/5.jpg" alt="5"/>
  <img src="img/1.jpg" alt="5"/>
 </div>
 <div id="buttons">
  <span index="1" class="on"></span>
  <span index="2"></span>
  <span index="3"></span>
  <span index="4"></span>
  <span index="5"></span>
 </div>
 <a href="javascript:;" id="prev" class="arrow"></a>
 <a href="javascript:;" id="next" class="arrow"></a>
 </div>

<script type="text/javascript" src="js/jquery.1.10.2.js"></script>
<script type="text/javascript">
 var container = $("#container");
 var list = $("#list");
 var listimg = $("#list img"); 
 var buttons = $("#buttons span");
 var prev = $("#prev");
 var next = $("#next");
 var index = 1;
 var len = 5;
 var num =len+2;
 var interval = 3000;//變換周期
 var timer; 
 var clientwidth=document.documentElement.clientWidth;//屏幕的寬度
 var conwidth = parseInt(clientwidth)-100;//顯示界面的寬度

 $(function(){


 setwidth();//設置container的寬度以及里面元素list和list中img的寬度


 function animate(offset){
  var left = parseInt(list.css("left"))+offset;

  // list.animate({left:left+'px'},'normal');
  list.animate({left:left+'px'},conwidth,function(){
  //第一位規定產生動畫效果的樣式,第二位設置速度,第三位是動畫函數執行完后執行的函數
  if (left > -conwidth) {//如果是第一個元素還向前移,就讓最后一個元素是這個元素
   list.css('left',-conwidth*len);
  }
  if (left < (-conwidth*len)) {//如果是最后一個元素還向后移,就讓第一個元素是這個元素
   list.css('left', -conwidth);
  }
  });
 }

 function showbutton(){//通過操作css來將顯示的圖片代表的下方原點變大,其余變小
  buttons.eq(index-1).addClass('on').siblings().removeClass('on');
 }

 function play(){
  timer = setTimeout(function(){
  next.trigger('click');//trigger()方法觸發被選元素的指定事件類型。
  play();
  },interval);
 }
 function stop(){
  clearTimeout(timer);
 }

 next.bind('click',function(){
  if (list.is(':animated')) {
  return;
  }
  if (index == 5) {
  index = 1;
  }
  else{
  index++;
  }
  animate(-conwidth);
  showbutton();
 });

 prev.bind('click',function(){
  if (list.is(':animated')) {
  return;
  }
  if (index == 1) {
  index = 5;
  }
  else{
  index--;
  }
  animate(conwidth);
  showbutton();
 });

 buttons.each(function(){
  $(this).bind('click',function(){
  if (list.is(':animated') || $(this).attr('class')=='on') {
   return;
  }
  var myindex = parseInt($(this).attr('index'));
  var offset = -conwidth*(myindex - index);

  animate(offset);
  index = myindex;
  showbutton();
  })
 });

 container.hover(stop,play);//鼠標懸停時執行stop()函數,移開時執行play()

 play();

 });

 function setwidth(){//設置container的寬度以及里面元素list和list中img的寬度

  container[0].style.width = conwidth +'px' ;
  list[0].style.width = num*conwidth +'px';
  list[0].style.left = '-'+conwidth +'px';
  for (var i = 0; i < listimg.length; i++) {
  listimg[i].style.width = conwidth + 'px';
  }
 }
</script>
</body>
</html>

2、實現思路

輪播圖的功能可分為:自動循環播放,點擊左邊按鈕顯示前面圖片,點擊后邊顯示后面圖片,點擊下方的小圓點實現跳轉播放。

1.自動播放功能:設置一個定時器,每隔一個周期的時間,觸發一次點擊右邊按鈕的函數功能。
2.點擊左邊按鈕顯示前面圖片:首先我們應該了解到輪播圖的原理。圖解

使用jQuery怎么實現一個輪播圖

大盒子是container,小盒子是list,list里面有很多圖片,沒有間隔的排列在一行,用絕對定位來操縱每次可以看到的圖片,也就是定位在container里面的是可見部分。當點擊左邊的按鈕時,前面的圖片右移,相當于絕對定位中的left值加一個圖片的寬度。

3.點擊右邊按鈕顯示后面圖片:原理和左邊的相同,相當于圖片左移,讓后面的圖片顯示出來。
4.點擊下方的小圓點實現跳轉播放:此時頁面是第二個圖片,要跳轉到第五個,相當于點擊了三次右邊的按鈕,也相當于圖片左移三個圖片的寬度。

3、需要掌握的知識點:

css:

絕對定位

js+jq:

document.documentElement.clientWidth;
obj.animate();
obj.css();
obj.eq()
obj.addClass();
obj.siblings();
obj.removeClass();
setTimeout();
clearTimeout();
obj.trigger();
obj.attr();
obj.bind();

以上就是使用jQuery怎么實現一個輪播圖,小編相信有部分知識點可能是我們日常工作會見到或用到的。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女