jquery+css實現(xiàn)簡單的圖片輪播效果
開發(fā)過程中需要用到圖片輪播的插件,在網(wǎng)上找了幾個插件之后還是決定自己碼一個,比較簡潔的功能,以后說不定還會有用。
ps:
功能比較簡單,整個框并不能根據(jù)圖片的大小自動調(diào)節(jié),這里所用的圖片是1170*500的,如果需要改成其他大小的圖片,自行修改.pic-list下img的寬度。
.pic-list中的寬度為整個橫幅的寬度,如果需要輪播的圖片數(shù)量很多,.pic-list的寬度應(yīng)大于數(shù)量*單張寬度,
html
<div class="banner">
<!--第一張圖片為最后一張,用來做輪播連接使用,所以一開始顯示的第一章,應(yīng)是第二張圖片,這里圖片的寬度為1170px,所以一開始left為-1170px,同理最后一張圖也為第一張圖。-->
<div class="pic-list" style="left: -1170px">
<img src="/static/img/4.jpg" alt="">
<img src="/static/img/1.jpg" alt="">
<img src="/static/img/2.jpg" alt="">
<img src="/static/img/3.jpg" alt="">
<img src="/static/img/4.jpg" alt="">
<img src="/static/img/1.jpg" alt="">
</div>
<div id="buttons">
<!-- 確保span的數(shù)量跟img數(shù)量一樣多,不包括第一張img和最后一張img-->
<span class='on'></span>
<span></span>
<span></span>
<span></span>
</div>
<a href="javascript:;" class="arrow" id="prev"><</a>
<a href="javascript:;" class="arrow" id="next">></a>
</div>
css
.banner{
width: 100%;
height: 500px;
overflow: hidden;
position: relative;
}
.banner a{
text-decoration: none;
}
.banner .pic-list{
width: 10000px;
height: 500px;
position: absolute;
z-index: 1;
}
.banner .pic-list img{
width: 1170px;
float: left;
}
#buttons{
position: absolute;
z-index: 2;
height: 10px;
bottom: 20px;
left: 550px;
}
#buttons span{
cursor: pointer;
float: left;
border: 1px solid #fff;
width: 10px;
height: 10px;
border-radius: 50%;
background: #333;
margin-right: 5px;
}
#buttons .on{
background: orange;
}
.arrow{
cursor: pointer;
line-height: 36px;
text-align: center;
font-size: 20px;
font-weight: bold;
width: 40px;
height: 40px;
position: absolute;
z-index: 2;
top: 200px;
background: rgba(0,0,0,0.5);
color: #fff;
display: none;
}
.banner:hover .arrow{display: block;}
#prev{
left: 20px;
}
#next{
right:20px;
}
js
$(document).ready(function(){
var picNum = 4;//圖片數(shù)量,根據(jù)實際修改
var picWidth = 1170;//圖片的寬度,根據(jù)實際修改
var picMaxWidth = -1 * picNum * picWidth;
var currentPic = 1;
var next = $('#next');
var prev = $('#prev');
var flag = false;
prev.on('click',function(){
if(!flag){
calPx(1170);
currentPic--;
if (currentPic < 1) {
currentPic = picNum;
}
$('#buttons span').eq(currentPic-1).addClass('on').siblings().removeClass('on');
}
});
next.on('click',function(){
if(!flag){
calPx(-1170);
currentPic++;
if (currentPic > picNum) {
currentPic = 1;
}
$('#buttons span').eq(currentPic-1).addClass('on').siblings().removeClass('on');
}
});
$('.banner').on('mouseover',function(){
stop();
}).on('mouseout',function(){
play();
})
function nextClick(){
next.click();
}
function play(){
setInt = setInterval(nextClick,2000);
}
function stop(){
clearInterval(setInt);
}
function calPx(leftPx){
flag = true;
var left = parseInt($('.pic-list').css('left'));
var newLeft = left+leftPx;
var time = 300;
var interval = 10;
var speed = leftPx/(time/interval);
function go(){
var left = parseInt($('.pic-list').css('left'));
if((speed < 0 && left > newLeft) || (speed > 0 && left < newLeft)){
$('.pic-list').css('left', (left + speed) + 'px');
setTimeout(go,interval);
}else{
flag = false;
if( newLeft > -1170){
newLeft = picMaxWidth;
}else if (newLeft < picMaxWidth ) {
newLeft = -1170;
}
$('.pic-list').css('left',newLeft + 'px');
}
}
go();
}
play();
});
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jquery.messager.js插件導(dǎo)致頁面抖動的解決方法
這盤文章介紹了jquery.messager.js插件導(dǎo)致頁面抖動的解決方法,有需要的朋友可以參考一下2013-07-07
ajax與json 獲取數(shù)據(jù)并在前臺使用簡單實例
這篇文章主要介紹了ajax與json 獲取數(shù)據(jù)并在前臺使用簡單實例的相關(guān)資料,需要的朋友可以參考下2017-01-01
jquery.validate 自定義驗證方法及validate相關(guān)參數(shù)
jquery.validate是一個基于jquery的非常優(yōu)秀的驗證框架,我們可以通過它迅速驗證一些常見的輸入,并且可以自己擴充自己的驗證方法,而且對國際化也有非常好的支持,通過本文給大家介紹jquery.validate 自定義驗證方法及validate相關(guān)參數(shù),需要的朋友一起學(xué)習(xí)吧2016-01-01
jQuery根據(jù)用戶電腦是mac還是pc加載對應(yīng)樣式的方法
這篇文章主要介紹了jQuery根據(jù)用戶電腦是mac還是pc加載對應(yīng)樣式的方法,涉及jQuery針對客戶端信息的獲取及樣式的調(diào)用技巧,需要的朋友可以參考下2015-06-06
淺談jquery fullpage 插件增加頭部和版權(quán)的方法
下面小編就為大家分享一篇淺談jquery fullpage 插件增加頭部和版權(quán)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看2018-03-03
jQuery EasyUI API 中文文檔 - TimeSpinner時間微調(diào)器
jQuery EasyUI API 中文文檔 - TimeSpinner時間微調(diào)器介紹,需要的朋友可以參考下。2011-10-10
jQuery插件學(xué)習(xí)教程之SlidesJs輪播+Validation驗證
這篇文章主要介紹了jQuery插件學(xué)習(xí)教程之SlidesJs輪播+Validation驗證的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07
jQuery對話框插件ArtDialog在雙擊遮罩層時出現(xiàn)關(guān)閉現(xiàn)象的解決方法
這篇文章主要介紹了jQuery對話框插件ArtDialog在雙擊遮罩層時出現(xiàn)關(guān)閉現(xiàn)象的解決方法,涉及針對插件源碼的修改,需要的朋友可以參考下2016-08-08

