2009-11-20 62 views
1

我正在構建一個處理非常長的分頁腳本。爲此,我使用jQuery滑塊。獲取正確的開始位置時,使用jQuery滑動滾動分頁

我使用了here中的一些修改函數。

這是目前爲止的js代碼。唯一缺少的是當腳本加載時分頁div移動到正確的位置(滑動條)。初始化時我只想運行用戶使用滑塊時運行的相同函數。

// Calculate total width 
    var totalWidth = 0; 
    $('.pageItem').each(function() { 
     totalWidth += $(this).outerWidth(true); 
    }); 

    // Total number of pages 
    var totalPages = $('.pageItem').size(); 

    // Calculate current position (starts counting at 0) 
    var currentPage = $('li').index($('.current')) + 1; 

    // Scroll start position 
    var scrollPosition = [currentPage/totalPages] * 100; 

    $('#pagiMoreSlider').slider({ 
     change:   handleSliderChange, 
     slide:   handleSliderChange, 
     max:   100, 
     value:   scrollPosition, 
    }); 
    function handleSliderChange(e, ui){ 
     var maxScroll = $('#pagiMoreContent').attr('scrollWidth') - $('#pagiMoreContent').width(); 
     $('#pagiMoreContent').attr({ 
      scrollLeft: ui.value * (maxScroll/100) 
     }); 
    } 

    // Set width pagiMore 
    $('#pagiMore ul').width(totalWidth); 

謝謝!

回答

0

好吧,我不知道,如果我們使用的是相同的插件,但是這對於移動滑塊代碼:

$('#content-slider').slider("moveTo", "-=1"); 
$('#content-scroll').stop(); 
+0

我使用jQuery UI滑塊http://jqueryui.com/demos/slider/ 。我不明白你的代碼將如何幫助我... – 2009-11-23 08:17:37

+0

嗯,據我所知,你需要將頁面加載滑塊移動到指定的位置,所以你需要使用: $(document).ready(function() {('#content-slider')。slider(「moveTo」,「 - = 1」); $('#content-scroll')。stop(); }); 這個數字1是移動你的滑塊的位置,所以如果你有100頁,你在第40頁,並且滑塊是用100單位(最大值)製作的,你將使用$('#content-slider') .slider(「moveTo」,「+ = 40」); 所以你需要在頁面上添加這個代碼,然後用dinamicaly填充那個+ = 40變量。 – dfilkovi 2009-11-23 09:11:45

+0

忘了告訴你用你的ID-s替換content-slider和content-scroll – dfilkovi 2009-11-23 09:12:48