最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jquery寫(xiě)出PC端輪播圖實(shí)例

 更新時(shí)間:2018年01月26日 10:00:56   投稿:laozhang  
本篇文章主要給大家講述了用jquery如何寫(xiě)出一個(gè)PC端的輪播圖效果的實(shí)例,有興趣的朋友參考下。

最近其他項(xiàng)目不是很忙,被安排給公司的官網(wǎng)項(xiàng)目做一個(gè)新的頁(yè)面(之前沒(méi)接觸公司官網(wǎng)項(xiàng)目),其中有一個(gè)用到輪播圖的地方,最開(kāi)始想直接用swiper.js插件實(shí)現(xiàn)就好了,可是發(fā)現(xiàn)官網(wǎng)項(xiàng)目里之前都沒(méi)有引入過(guò)swiper.js,后來(lái)想了想,就不引入它了,免得又得增加依次一次網(wǎng)絡(luò)請(qǐng)求,項(xiàng)目里既然已經(jīng)用到了jQuery,那就索性用jQuery寫(xiě)一個(gè)輪播圖吧。

現(xiàn)在把自己寫(xiě)的輪播圖這塊代碼單獨(dú)拿出來(lái),做一個(gè)小demo寫(xiě)在這里記錄一下(demo中輪播圖的圖片網(wǎng)上隨意找的)

實(shí)現(xiàn)的效果:

1、自動(dòng)輪播(輪播時(shí)間間隔在js代碼中自定義)

2、點(diǎn)擊左右側(cè)按鈕,實(shí)現(xiàn)手動(dòng)切換

3、底部小圓點(diǎn)根據(jù)切換圖片的位置相應(yīng)的顯示active狀態(tài)

4、鼠標(biāo)經(jīng)過(guò)輪播圖區(qū)域,停止輪播,離開(kāi)輪播圖區(qū)域開(kāi)始輪播

代碼目錄結(jié)果如下:

一、index.html

注:這里以5張圖片為例,頁(yè)面上真正輪播展示給用戶(hù)看到的是5張不同的圖片,但是為了輪播效果的連貫性,所以在第一張圖片前面添加上第五張圖片,在第五張圖片后面加上了第一張圖片,所以demo結(jié)構(gòu)里是7張圖片,每張圖片的尺寸必須都是一樣的哦(這里寬高尺寸是720*350px)。

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>PC-jquery版輪播圖</title>
 <link rel="stylesheet" href="css/style.css" rel="external nofollow" >
</head>
<body>
<div class="layout">
 <h2 style="text-align: center;">PC-jquery版輪播圖</h2>
 <div class="slide" id="slide">
  <div id="outer" class="outer">
   <ul id="inner" class="inner">
    <li><a  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>圖片-5</p><img src="images/slide-5.jpg"></a></li>
    <li><a  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>圖片-1</p><img src="images/slide-1.jpg"></a></li>
    <li><a  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>圖片-2</p><img src="images/slide-2.jpg"></a></li>
    <li><a  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>圖片-3</p><img src="images/slide-3.jpg"></a></li>
    <li><a  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>圖片-4</p><img src="images/slide-4.jpg"></a></li>
    <li><a  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>圖片-5</p><img src="images/slide-5.jpg"></a></li>
    <li><a  rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ><p>圖片-1</p><img src="images/slide-1.jpg"></a></li>
   </ul>       <!--底部小圓點(diǎn)-->
   <ol class="dot" id="dot">
    <li class="active"></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
   </ol>
  </div>
     <!--左右兩側(cè)的點(diǎn)擊切換按鈕-->
  <div class="arrow-box">
   <div class="arrow arrow-l" id="arrow_l">‹</div>
   <div class="arrow arrow-r" id="arrow_r">›</div>
  </div>
 </div>
</div>
<script src="js/jquery.min.js"></script>
<script src="js/index.js"></script>
</body>
</html>

二、style.css

* {
 margin: 0;
 padding: 0;
 box-sizing: border-box;
}
.layout {
 width: 1000px;
 margin: 30px auto;
}
ul,ol,li {
 list-style: none;
}
.slide {
 position: relative;
 width: 900px;
 margin:auto;
}
.slide .outer {
 position: relative;
 margin: 30px auto;
 width: 720px;
 height: 400px;
 overflow: hidden;
}
.slide .outer .inner {
 width: 5040px;
 height: 350px;
 position: absolute;
 left: -720px;
 top: 0;
}
.slide .outer .inner li {
 float: left;
 height: 350px;
}
.slide .outer .inner li a {
 display: block;
 position: relative;
 width: 100%;
 height: 100%;
}
.slide .outer .inner li a p {
 position: absolute;
 left: 0;
 bottom: 0;
 color: #fff;
 font-size: 18px;
 width: 720px;
 height: 80px;
 line-height: 80px;
 padding-left: 50px;
 background: linear-gradient(180deg,rgba(0,0,0,0), rgba(0,0,0,0.5));
}
.slide .outer .dot {
 margin-top: 365px;
 text-align: center;
}
.slide .outer .dot li {
 height: 6px;
 width: 6px;
 border-radius: 3px;
 background-color: #d2cbcb;
 display: inline-block;
 margin: 0 3px;
}
.slide .outer .dot li.active {
 background-color: #6e5ca5;
}
.slide .arrow-box {
 position: absolute;
 width: 900px;
 height: 60px;
 top: 150px;
 left: 0;
}
.slide .arrow-box .arrow {
 width: 60px;
 height: 60px;
 line-height: 60px;
 text-align: center;
 border-radius: 30px;
 background-color: #dde2e6;
 font-size: 60px;
 color: #999;
 cursor: pointer;
}
.slide .arrow-box .arrow.arrow-l {
 float: left;
}
.slide .arrow-box .arrow.arrow-r {
 float: right;
}

三、index.js

注:js代碼中,每個(gè)變量均已給了注釋。為了防止快速多次點(diǎn)擊,而出現(xiàn)動(dòng)畫(huà)不停的現(xiàn)象,這里在每次切換圖片的時(shí)候先調(diào)用stop(false,true)。但是注意在向左側(cè)滾動(dòng)的時(shí)候,滾動(dòng)到最后一張圖圖片后,再次切換時(shí)就不要用stop(false,true),而是要瞬間定位到第一張圖片(其實(shí)是dom結(jié)構(gòu)中的第二張)的位置,同樣,向右側(cè)滾動(dòng)時(shí),當(dāng)滾動(dòng)到第一張圖片后,再次切換時(shí)就不用stop(false,true),而是要瞬間定位到最后一張圖片(其實(shí)是dom結(jié)構(gòu)中的倒數(shù)第二張)的位置。

var interval = 3000;    //輪播間隔時(shí)間
var arrowL = $('#arrow_l');   //左側(cè)箭頭
var arrowR = $('#arrow_r');   //右側(cè)箭頭

var slideBox = $('#slide');   //輪播圖區(qū)域
var innerBox = $('#inner');   //內(nèi)層大盒子
var img = innerBox.children('li'); //每個(gè)圖片
var dot = $('#dot');    //小圓點(diǎn)盒子

var imgW = $(img[0]).outerWidth(); //每個(gè)li標(biāo)簽的寬度

var imgCount = 5;     //總圖片個(gè)數(shù)(不同圖片的個(gè)數(shù))(實(shí)際dom上是有7張)
var i = 0;       //初始化為第0張圖片
timer = null;      //定時(shí)器

//自動(dòng)輪播
timer = setInterval(function () {
 i++;
 innerBox.stop(false, true).animate({'left':-i*imgW+'px'},300)
 dot.find('li').removeClass('active').eq(i-1).addClass('active')
 if(i > imgCount){
  innerBox.animate({'left':-1*imgW+'px'},0);
  dot.find('li').removeClass('active').eq(0).addClass('active')
  i = 1;
 }
},interval)

//點(diǎn)擊右側(cè)箭頭,播放下一張
arrowR.click(function () {
 i++;
 innerBox.stop(false, true).animate({'left':-i*imgW+'px'},300)
 dot.find('li').removeClass('active').eq(i-1).addClass('active')
 if(i > imgCount){
  innerBox.animate({'left':-1*imgW+'px'},0);
  dot.find('li').removeClass('active').eq(0).addClass('active')
  i = 1;
 }
})

//點(diǎn)擊左側(cè)箭頭,播放上一張
arrowL.click(function () {
 i--;
 innerBox.stop(false, true).animate({'left':-i*imgW+'px'},300)
 dot.find('li').removeClass('active').eq(i-1).addClass('active')
 if(i < 1){
  innerBox.animate({'left':-imgCount*imgW+'px'},0);
  dot.find('li').removeClass('active').eq(imgCount-1).addClass('active')
  i = imgCount;
 }
})
//鼠標(biāo)經(jīng)過(guò)輪播圖區(qū)域時(shí),清除定時(shí)器,停止自動(dòng)輪播
slideBox.mouseenter(function () {
 clearInterval(timer);
})

//鼠標(biāo)離開(kāi)輪播圖區(qū)域時(shí),重新啟動(dòng)自動(dòng)輪播
slideBox.mouseleave(function () {
 timer = setInterval(function () {
  i++;
  innerBox.stop(false, true).animate({'left':-i*imgW+'px'},300)
  dot.find('li').removeClass('active').eq(i-1).addClass('active')
  if(i > imgCount){
   innerBox.animate({'left':-1*imgW+'px'},0);
   dot.find('li').removeClass('active').eq(0).addClass('active')
   i = 1;
  }
 },interval)
})

四、效果圖展示

相關(guān)文章

  • jQuery監(jiān)聽(tīng)文件上傳實(shí)現(xiàn)進(jìn)度條效果的方法

    jQuery監(jiān)聽(tīng)文件上傳實(shí)現(xiàn)進(jìn)度條效果的方法

    下面小編就為大家?guī)?lái)一篇jQuery監(jiān)聽(tīng)文件上傳實(shí)現(xiàn)進(jìn)度條效果的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-10-10
  • jQuery選擇器源碼解讀(八):addCombinator函數(shù)

    jQuery選擇器源碼解讀(八):addCombinator函數(shù)

    這篇文章主要介紹了jQuery選擇器源碼解讀(八):addCombinator函數(shù),本文用詳細(xì)的注釋解讀了addCombinator函數(shù)的實(shí)現(xiàn)源碼,需要的朋友可以參考下
    2015-03-03
  • Jquery時(shí)間軸特效(三種不同類(lèi)型)

    Jquery時(shí)間軸特效(三種不同類(lèi)型)

    本文給大家收集了三種不同類(lèi)型的jquery時(shí)間軸特效,涉及到j(luò)quer相關(guān)知識(shí),對(duì)jquery時(shí)間軸特效感興趣的朋友可以參考下本文
    2015-11-11
  • 淺析jQuery 3.0中的Data

    淺析jQuery 3.0中的Data

    jQuery 3.0 在6月9日正式發(fā)布了,3.0 也被稱(chēng)為下一代的 jQuery。這篇文章主要介紹了jQuery 3.0中的Data,包括Data在jQuery內(nèi)部的使用和1.x.x 和 2.x.x 的比較的相關(guān)內(nèi)容,需要的朋友可以參考下
    2016-06-06
  • jQuery圖片預(yù)加載 等比縮放實(shí)現(xiàn)代碼

    jQuery圖片預(yù)加載 等比縮放實(shí)現(xiàn)代碼

    jQuery圖片預(yù)加載 等比縮放實(shí)現(xiàn)代碼,需要的朋友可以參考下。
    2011-10-10
  • jQuery Mobile中的button按鈕組件基礎(chǔ)使用教程

    jQuery Mobile中的button按鈕組件基礎(chǔ)使用教程

    jQuery Mobile中的button默認(rèn)提供了很多用于制作移動(dòng)Web頁(yè)面按鈕的屬性,這里我們來(lái)整理一下jQuery Mobile中的button按鈕組件基礎(chǔ)使用教程,需要的朋友可以參考下
    2016-05-05
  • jQuery實(shí)現(xiàn)復(fù)制到粘貼板功能

    jQuery實(shí)現(xiàn)復(fù)制到粘貼板功能

    這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)復(fù)制到粘貼板功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • jQuery實(shí)現(xiàn)元素的插入

    jQuery實(shí)現(xiàn)元素的插入

    本文主要介紹了jQuery實(shí)現(xiàn)元素的插入--insertBefore()和insertAfter()方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
    2017-02-02
  • jquery+html仿翻頁(yè)相冊(cè)功能

    jquery+html仿翻頁(yè)相冊(cè)功能

    這篇文章主要為大家詳細(xì)介紹了jquery+html仿翻頁(yè)相冊(cè)功能,前端實(shí)現(xiàn)的相冊(cè)模仿功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • jQuery 源碼分析筆記(4) Ready函數(shù)

    jQuery 源碼分析筆記(4) Ready函數(shù)

    接下來(lái)回到最開(kāi)始的jQuery.extend函數(shù)(268行)中的ready(fn)的定義。這個(gè)函數(shù)用來(lái)處理DOM加載完成的事件。差不多是jQuery最常用的函數(shù)之一了。
    2011-06-06

最新評(píng)論

辰溪县| 靖江市| 仙居县| 长春市| 巴里| 五台县| 封丘县| 榆树市| 苍山县| 巴东县| 额尔古纳市| 吴江市| 上思县| 论坛| 济宁市| 大埔区| 和硕县| 金门县| 桂阳县| 青阳县| 宁德市| 阳信县| 上饶市| 乐至县| 碌曲县| 江城| 石泉县| 梅河口市| 霍邱县| 常山县| 崇左市| 渝北区| 浮梁县| 承德市| 桂林市| 济南市| 滦南县| 兴山县| 桓仁| 内黄县| 治多县|