純javaScript實現個性化圖片輪播
創(chuàng)新互聯公司憑借專業(yè)的設計團隊扎實的技術支持、優(yōu)質高效的服務意識和豐厚的資源優(yōu)勢,提供專業(yè)的網站策劃、成都網站制作、網站建設、網站優(yōu)化、軟件開發(fā)、網站改版等服務,在成都10多年的網站建設設計經驗,為成都上千家中小型企業(yè)策劃設計了網站。

輪播原理說明<如上圖所示>:
1. 畫布部分(可視區(qū)域)屬性說明:overflow:hidden使得超出畫布部分隱藏或說不可見。position:relative 會導致自身位置的相對變化,而不會影響其他元素的位置、大小的變化。使得使用了position:absolute 元素相對于畫布位置進行定位;
absolute元素脫離了文檔結構,產生破壞性,導致父元素坍塌,float元素也會脫離文檔結構,absolute元素會懸浮在頁面上方,遮擋其他部分顯示,這點和PhotoShop圖層相似,所以要使用z-index控制出現順序
2.輪播注意點:左右無限滾動
prev-button 第一張圖片的前一張是最后一張圖片,
next-button 最后一張圖片的下一張圖片是第一張,
prev-button、next-button位置的偏移是通過設置prev-img-container、next-img-container的left<相對于畫布>屬性值
click-select-show-button區(qū)域,點擊該區(qū)域小圓圈是通過上一次圖片的所在index,當前點擊myIndex, 計算公式:(myIndex-index)*(-圖片的寬度width)
3.動畫過渡注意點:點擊prev-button、next-button、click-select-show-button小圓圈,判定當前是否處于動畫狀態(tài)中
4.定時器setTimeout()、clearTimeout
Css樣式
/**CSS-style**/
/**畫布大小*/
#container {
margin:0 auto;
width: 600px;
height: 400px;
overflow: hidden;/*超出畫布部分隱藏*/
position: relative;/*相對定位*/
cursor: pointer;
}
/**圖片容器*/
#list {
width: 4200px;
height: 400px;
position: absolute;
z-index:1;
}
#list img { float: left; }
/**輪播選中按鈕樣式*/
#button {
position: absolute;
bottom: 25px;
left: 175px;
width: 250px;
z-index: 2;
}
#button ul li {
list-style: none;
width: 15px;
border-radius: 50%;
padding: 7.5px;
height: 15px;
margin-right: 10px;
background: green;
float: left;
font:15px/15px "microsoft yahei";
text-align: center;
font-weight: bold;
color: white;
cursor: pointer;
}
#button ul li.chos {
background: orange;
}
#container:hover .arrow{
display: block;
}
#pre {
left: 20px;
}
#next {
right: 20px;
}
/**pre next定位*/
.arrow {
position: absolute;
width: 40px;
height: 40px;
background: black;
z-index: 3;
top: 180px;
text-decoration: none;
text-align: center;
line-height: 40px;
font-size: 40px;
color: white;
opacity: 0.3;
filter: alpha(opacity=0.3);
display: none;
}
/**pre next按鈕透明度*/
#container a:hover {
opacity: 0.7;
filter: alpha(opacity=0.7);
}
html代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>純javaScript實現個性化圖片輪播</title> <link rel="stylesheet" type="text/css" href="styles/main.css"> <script type="text/javascript" src="scripts/scroImg.js"></script> </head> <body> <div id="container"> <div id="list" > <img src="images/5.jpg"> <img src="images/1.jpg"> <img src="images/2.jpg"> <img src="images/3.jpg"> <img src="images/4.jpg"> <img src="images/5.jpg"> <img src="images/1.jpg"> </div> <div id="button"> <ul> <li index='1'>1</li> <li index='2'>2</li> <li index='3'>3</li> <li index='4'>4</li> <li index='5'>5</li> </ul> </div> <a href="#" class="arrow" id="prev"><</a> <a href="#" class="arrow" id="next">></a> </div> </body> </html>
一、javaScript實現圖片輪播
window.onload=function(){
var container=document.getElementById('container');
var list=document.getElementById('list');
var buttons=document.getElementById('button').getElementsByTagName('li');
var prev=document.getElementById('prev');
var next=document.getElementById('next');
var index=1;
var interval=1000;
var timer=null;
var animated=false;
//next
next.onclick=function(){
if (!animated) {
animate(-600);
};
index+=1;
if (index>5) {
index=1;
};
showButton();
console.info('next'+index);
}
//prev
prev.onclick=function(){
if(!animated){
animate(600);
}
index-=1;
if(index<1){
index=5;
}
showButton();
console.info('prev'+index);
}
//animate
function animate(offset){
animated=true;
var left=parseInt(list.style.left)+offset;
var animateTime=600;//位移總時間
var interval=10;//時間間隔
var speed=offset/(animateTime/interval);//每次位移量
var go=function(){//animate內部函數
if ((speed<0 && parseInt(list.style.left)>left) || (speed>0 && parseInt(list.style.left)<left)) {//是否位移
list.style.left=parseInt(list.style.left)+speed+'px';
setTimeout(go,interval)
}else{
list.style.left=left+'px';
if (left<-3000) { //最后一張后面
list.style.left=-600+'px'; //顯示前一張
};
if(left>-600){//第一張最前面
list.style.left=-3000+'px';//顯示最后一張
}
animated=false;
};
}
go();
}
//chos
function showButton(){
for (var i = 0; i < buttons.length; i++) {
buttons[i].className='';
};
buttons[index-1].className='chos';
}
//buttons-click
for (var i = 0; i < buttons.length; i++) {
buttons[i].onclick=function(){
if(this.className=='chos'){
return;
}
var myIndex=parseInt(this.getAttribute('index'));
var offset=(myIndex-index)*-600; //偏移量
animate(offset);
index=myIndex;//set Index
showButton();
}
};
function play(){
timer=setTimeout(function(){
next.click();
play();
},interval)
}
function stop(){
clearInterval(timer);
}
play();
container.onmouseover=function(){
stop();
}
container.onmouseout=function(){
play();
}
}
二、jQuery實現圖片輪播
$(function () {
var container = $('#container');
var list = $('#list');
var buttons = $('#container').find('li');
var prev = $('#pre');
var next = $('#next');
var index = 1;
var len = 5;
var interval = 3000;
var timer;
function animate (offset) {
var left = parseInt(list.css('left')) + offset;
if (offset>0) {
offset = '+=' + offset;
}
else {
offset = '-=' + Math.abs(offset);
}
list.animate({'left': offset}, 300, function () {
if(left > -200){
list.css('left', -600 * len);
}
if(left < (-600 * len)) {
list.css('left', -600);
}
});
}
function showButton() {
buttons.eq(index-1).addClass('chos').siblings().removeClass('chos');
}
function play() {
timer = setTimeout(function () {
next.trigger('click');
play();
}, interval);
}
function stop() {
clearTimeout(timer);
}
next.bind('click', function () {
if (list.is(':animated')) {
return;
}
if (index == 5) {
index = 1;
}
else {
index += 1;
}
animate(-600);
showButton();
});
prev.bind('click', function () {
if (list.is(':animated')) {
return;
}
if (index == 1) {
index = 5;
}
else {
index -= 1;
}
animate(600);
showButton();
});
buttons.each(function () {
$(this).bind('click', function () {
if (list.is(':animated') || $(this).attr('class')=='chos') {
return;
}
var myIndex = parseInt($(this).attr('index'));
var offset = -600 * (myIndex - index);
animate(offset);
index = myIndex;
showButton();
})
});
container.hover(stop, play);
play();
});
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持創(chuàng)新互聯!
當前標題:純javaScript、jQuery實現個性化圖片輪播【推薦】
當前網址:http://chinadenli.net/article8/ggpgip.html
成都網站建設公司_創(chuàng)新互聯,為您提供網站設計公司、企業(yè)建站、虛擬主機、移動網站建設、網站策劃、網站收錄
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯