2012-03-12 53 views
0

資料覈實的名單我想有一組div的滑動上一頁和下一頁按鈕

<div><img src="img/car.jpg" /></div> 
<div><img src="img/boat.jpg" /></div> 
<div><img src="img/truck.jpg" /></div> 

我有大約20 div的所有浮動左邊的下一個和上一個按鈕。有沒有用前後兩個按鈕前後滑動?

+9

也許吧。你有什麼嘗試?特別是你有什麼問題? – 2012-03-12 21:14:31

+1

如果你沒有顯示你試過的東西,很難提供幫助 – 2012-03-12 21:22:03

+0

這取決於每個img是多大,就好像它們大小不同,解決方案不會那麼簡單。 – Jake 2012-03-12 21:33:17

回答

1

我已經在過去https://stackoverflow.com/a/8747305/297641

回答了類似的問題,所以我編輯的代碼一點點與你有什麼工作,

DEMO

HTML:

<div class="prev">Previous</div> 
<div class="nav-wrapper clearfix"> 
    <div>Car</div> 
    <div>Boat</div> 
    <div>Truck</div> 
    <div>Van</div> 
    <div>Bi-Cycle</div> 
    <div>Omni</div> 
    <div>Race Car</div> 
</div> 
<div class="next">Next</div> 

JS:

var stPt = 0, elToShow = 5; //showing 5 elements 

var $nav_wrapper = $('.nav-wrapper'); 
var $list = $nav_wrapper.find('div'); //get the list of div's 
var $copy_list = []; 
var copy_lgt = $list.length - elToShow; 

//call to set thumbnails based on what is set 
initNav(); 
function initNav() { 
    var tmp; 
    for (var i = elToShow; i < $list.length; i++) { 
     tmp = $list.eq(i); 
     $copy_list.push(tmp.clone()); 
     tmp.remove(); 
    } 
} 

$('.next').click (function() { 
    $list = $nav_wrapper.find('div'); //get the list of div's 

    //move the 1st element clone to the last position in copy_list 
    $copy_list.splice(copy_lgt, 0, $list.eq(0).clone()); //array.splice(index,howmany,element1,.....,elementX) 

    //kill the 1st element in the div 
    $list.eq(0).remove(); 

    //add to the last 
    $nav_wrapper.append($copy_list.shift()); 
}); 

$('.prev').click (function() { 
    $list = $nav_wrapper.find('div'); //get the list of li's 

    //move the 1st element clone to the last position in copy_li 
    $copy_list.splice(0, 0, $list.eq(elToShow-1).clone()); //array.splice(index,howmany,element1,.....,elementX) 

    //kill the 1st element in the UL 
    $list.eq(elToShow-1).remove(); 

    //add to the last 
    $nav_wrapper.prepend($copy_list.pop()); 

}); 
+0

所以基本上這裏的想法是爲了讓'lastchild'成爲'firstchild',而之前的做法則相反。那是對的嗎? – Jake 2012-03-12 21:42:11

+0

非常感謝,我認爲。這是我所做的。 http://scott-p.dmlive.co.nz/formatives/pf-wp/我想添加下一個和上一個按鈕。 – 2012-03-12 23:59:41

相關問題