2012-07-31 70 views
-4

這裏是我的代碼/例如:http://jsfiddle.net/6j4cT/14/自定義jquery滑塊|點擊菜單項更改Silde

滑塊完美的作品,現在都後,我是如果你點擊任何「新聞項」中說的「節點1 「例如相應的圖像將呈現 - 同爲‘節點2’

// News Article Slideshow 
    var periodToChangeSlide = 5000; 
    var pp_slideshow = undefined; 
    var currentPage = 0; 

    $('#news-feature-img-wrap li').css('display','list-item').slice(1).css('display','none'); 
    $('#news-items li:first').addClass('active'); 

    $("#news-feature-wrap #news-items li").click(function() { 
    $(this).parent().addClass('active'); 
    $(this).parent().siblings().removeClass('active'); 

    var index = $(this).parent().index(); 
    var toShow = $("#news-feature-wrap #news-feature-img-wrap li").eq(index); 
    toShow.show(); 
    toShow.siblings().hide(); 
    currentPage = index; 
    $.stopSlideshow(); 
    }); 

    $.startSlideshow = function(){ 
    if(typeof pp_slideshow == 'undefined'){ 
    pp_slideshow = setInterval($.startSlideshow, periodToChangeSlide); 
    } else { 
    $.changePage(); 
    } 
    } 

    $.stopSlideshow = function(){ 
    clearInterval(pp_slideshow); 
    pp_slideshow= undefined; 
    } 
    $.changePage = function(){ 
    var numSlides= $('#news-feature-wrap #news-feature-img-wrap li').length; 
    currentPage = (currentPage + 1) % numSlides; 
    var menu = $('#news-feature-wrap #news-items li').eq(currentPage); 
    menu.addClass('active'); 
    menu.siblings().removeClass('active'); 

    var toShow = $("#news-feature-wrap #news-feature-img-wrap li").eq(currentPage); 
    toShow.show(); 
    toShow.siblings().hide(); 
    } 

    $.startSlideshow(); 
+0

什麼........ ??? – Prashobh 2012-07-31 02:28:52

+0

@prash什麼? ... – Filth 2012-07-31 02:29:42

+0

問題不清楚。 – Prashobh 2012-07-31 02:32:01

回答

2

我修改你的$.changePage功能略有接受內部參數:

$.changePage = function(internal) { //add this optional parameter 
    var numSlides= $('#news-feature-wrap #news-feature-img-wrap li').length; 
    if (typeof internal == 'undefined') currentPage = (currentPage + 1) % numSlides; //add this conditional 
    else currentPage = internal; //and this else 
    ... 

然後,你可以添加一個簡單的事件監聽器:

$('#news-items').on('click', 'li', function() { 
    //stop and restart to reset the interval 
    $.stopSlideshow(); 
    $.changePage($(this).index()); 
    $.startSlideshow(); 
}); 

Fiddle

jQuery的1.4.3 - 1.6.4

$('#news-items').delegate('li', 'click', function() { 
    //stop and restart to reset the interval 
    $.stopSlideshow(); 
    $.changePage($(this).index()); 
    $.startSlideshow(); 
}); 

Fiddle

+0

幾乎所有的伴侶 - 唯一的問題是,「活動」類現在已經從「新聞項目」 – Filth 2012-07-31 02:31:37

+0

@Filth中的第一個列表項中刪除,何時會發生?無論何時點擊改變或等待它自動改變,「活動」類都會遵循相應的圖像。 – 2012-07-31 02:37:15

+1

我爲測試製作了'.active'背景綠色,檢查它是否無法按預期工作:http://jsfiddle.net/ult_combo/6j4cT/17/ – 2012-07-31 02:42:32