本篇文章主要為大家講述了用jquery如何寫出一個PC端的輪播圖效果的實例,有興趣的朋友參考下。
最近其他專案不是很忙,被安排為公司的官網專案做一個新的頁面(之前沒接觸公司官網專案),其中有一個用到輪播圖的地方,最開始想直接用swiper.js插件實作就好了,可是發現官網專案里之前都沒有引入過swiper.js,後來想了想,就不引入它了,免得又得增加依次一次網絡請求,項目裡既然已經用到了jQuery ,那就索性用jQuery寫一個輪播圖吧。
現在把自己寫的輪播圖這塊程式碼單獨拿出來,做一個小demo寫在這裡記錄一下(demo中輪播圖的圖片網上隨意找的)
實現的效果:
1、自動輪播(輪播時間間隔在js程式碼中自訂)
2、點擊左右側按鈕,實作手動切換
3、底部小圓點根據切換圖片的位置對應的顯示active狀態
4、滑鼠經過輪播圖區域,停止輪播,離開輪播圖區域開始輪播
程式碼目錄結果如下:
一、index.html
附註:這裡以5張圖片為例,頁面上真正輪播展示給用戶看到的是5張不同的圖片,但是為了輪播效果的連貫性,所以在第一張圖片前面添加上第五張圖片,在第五張圖片後面加上了第一張圖片,所以demo結構裡是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> <p class="layout"> <h2 id="PC-jquery版轮播图">PC-jquery版轮播图</h2> <p class="slide" id="slide"> <p id="outer" class="outer"> <ul id="inner" class="inner"> <li><a href="http://www.baidu.com" 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="/static/imghwm/default1.png" data-src="images/slide-5.jpg" class="lazy" alt="利用jquery如何寫出PC端輪播圖(詳細教學)" ></a></li> <li><a href="http://www.baidu.com" 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="/static/imghwm/default1.png" data-src="images/slide-1.jpg" class="lazy" alt="利用jquery如何寫出PC端輪播圖(詳細教學)" ></a></li> <li><a href="http://www.baidu.com" 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="/static/imghwm/default1.png" data-src="images/slide-2.jpg" class="lazy" alt="利用jquery如何寫出PC端輪播圖(詳細教學)" ></a></li> <li><a href="http://www.baidu.com" 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="/static/imghwm/default1.png" data-src="images/slide-3.jpg" class="lazy" alt="利用jquery如何寫出PC端輪播圖(詳細教學)" ></a></li> <li><a href="http://www.baidu.com" 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="/static/imghwm/default1.png" data-src="images/slide-4.jpg" class="lazy" alt="利用jquery如何寫出PC端輪播圖(詳細教學)" ></a></li> <li><a href="http://www.baidu.com" 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="/static/imghwm/default1.png" data-src="images/slide-5.jpg" class="lazy" alt="利用jquery如何寫出PC端輪播圖(詳細教學)" ></a></li> <li><a href="http://www.baidu.com" 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="/static/imghwm/default1.png" data-src="images/slide-1.jpg" class="lazy" alt="利用jquery如何寫出PC端輪播圖(詳細教學)" ></a></li> </ul> <!--底部小圆点--> <ol class="dot" id="dot"> <li class="active"></li> <li></li> <li></li> <li></li> <li></li> </ol> </p> <!--左右两侧的点击切换按钮--> <p class="arrow-box"> <p class="arrow arrow-l" id="arrow_l">‹</p> <p class="arrow arrow-r" id="arrow_r">›</p> </p> </p> </p> <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程式碼中,每個變數均已給了註解。為了防止快速多次點擊,而出現動畫不停的現象,這裡在每次切換圖片的時候先呼叫stop(false,true)。但注意在向左側滾動的時候,滾動到最後一張圖圖片後,再次切換時就不要用stop(false,true),而是要瞬間定位到第一張圖片(其實是dom結構中的第二張)的位置,同樣,向右側滾動時,當滾動到第一張圖片後,再次切換時就不用stop(false,true),而是要瞬間定位到最後一張圖片(其實是dom結構中的倒數第二張)的位置。
var interval = 3000; //轮播间隔时间 var arrowL = $('#arrow_l'); //左侧箭头 var arrowR = $('#arrow_r'); //右侧箭头 var slideBox = $('#slide'); //轮播图区域 var innerBox = $('#inner'); //内层大盒子 var img = innerBox.children('li'); //每个图片 var dot = $('#dot'); //小圆点盒子 var imgW = $(img[0]).outerWidth(); //每个li标签的宽度 var imgCount = 5; //总图片个数(不同图片的个数)(实际dom上是有7张) var i = 0; //初始化为第0张图片 timer = null; //定时器 //自动轮播 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) //点击右侧箭头,播放下一张 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; } }) //点击左侧箭头,播放上一张 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; } }) //鼠标经过轮播图区域时,清除定时器,停止自动轮播 slideBox.mouseenter(function () { clearInterval(timer); }) //鼠标离开轮播图区域时,重新启动自动轮播 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) })
四、效果圖展示
#上面是我整理給大家的,希望未來會對大家有幫助。
相關文章:
webpack 4.0.0-beta.0版本新功能(詳細教學)
以上是利用jquery如何寫出PC端輪播圖(詳細教學)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Node.js擅長於高效I/O,這在很大程度上要歸功於流。 流媒體匯總處理數據,避免內存過載 - 大型文件,網絡任務和實時應用程序的理想。將流與打字稿的類型安全結合起來創建POWE

Python和JavaScript在性能和效率方面的差異主要體現在:1)Python作為解釋型語言,運行速度較慢,但開發效率高,適合快速原型開發;2)JavaScript在瀏覽器中受限於單線程,但在Node.js中可利用多線程和異步I/O提升性能,兩者在實際項目中各有優勢。

JavaScript起源於1995年,由布蘭登·艾克創造,實現語言為C語言。 1.C語言為JavaScript提供了高性能和系統級編程能力。 2.JavaScript的內存管理和性能優化依賴於C語言。 3.C語言的跨平台特性幫助JavaScript在不同操作系統上高效運行。

JavaScript在瀏覽器和Node.js環境中運行,依賴JavaScript引擎解析和執行代碼。 1)解析階段生成抽象語法樹(AST);2)編譯階段將AST轉換為字節碼或機器碼;3)執行階段執行編譯後的代碼。

Python和JavaScript的未來趨勢包括:1.Python將鞏固在科學計算和AI領域的地位,2.JavaScript將推動Web技術發展,3.跨平台開發將成為熱門,4.性能優化將是重點。兩者都將繼續在各自領域擴展應用場景,並在性能上有更多突破。

Python和JavaScript在開發環境上的選擇都很重要。 1)Python的開發環境包括PyCharm、JupyterNotebook和Anaconda,適合數據科學和快速原型開發。 2)JavaScript的開發環境包括Node.js、VSCode和Webpack,適用於前端和後端開發。根據項目需求選擇合適的工具可以提高開發效率和項目成功率。

是的,JavaScript的引擎核心是用C語言編寫的。 1)C語言提供了高效性能和底層控制,適合JavaScript引擎的開發。 2)以V8引擎為例,其核心用C 編寫,結合了C的效率和麵向對象特性。 3)JavaScript引擎的工作原理包括解析、編譯和執行,C語言在這些過程中發揮關鍵作用。

JavaScript是現代網站的核心,因為它增強了網頁的交互性和動態性。 1)它允許在不刷新頁面的情況下改變內容,2)通過DOMAPI操作網頁,3)支持複雜的交互效果如動畫和拖放,4)優化性能和最佳實踐提高用戶體驗。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

禪工作室 13.0.1
強大的PHP整合開發環境

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境