2016-05-15 219 views
4

我已經編寫了一個面向對象的JavaScript自定義滑塊,如下所示。我在這裏把模塊放在了小提琴https://jsfiddle.net/5z29xhzg/7/。在向左或向右滑動之後,幻燈片被克隆並相應地添加,以便用戶可以像想要的那樣循環滑動。有一個單獨的功能用於控制活動選項卡。當單獨使用時,標籤和滑塊完美地工作,但當兩者結合使用時,我遇到了問題。例如,點擊「藍色圍裙」,然後點擊左邊的幻燈片按鈕(它應該帶你到達'012ave穿梭者的幻燈片),你可以進入幸福幻燈片。或者使用標籤點擊上一張幻燈片,然後點擊下一個按鈕不會顯示任何內容。有人能指出我寫下的對象中的缺陷嗎?任何幫助深表感謝!自定義滑塊問題

GiftSlider = { 
    prev: '.slider-container .prev', 
    next: '.slider-container .next', 
    slide: '.slide', 
    slidesContainer: '#slides', 
    navLink: '.gift-nav li a', 
    init: function() { 
     // Initialize nextSlide function when clicking right arrow 
     $(this.next).click(this.nextSlide.bind(this)); 
     $(this.next).click(this.activeTabs.bind(this)); 
     // Initialize prevSlide function when clicking left arrow 
     $(this.prev).click(this.prevSlide.bind(this)); 
     $(this.prev).click(this.activeTabs.bind(this)); 
     // Initialize toggleSlides and activeTab functions when clicking nav links 
     $(this.navLink).click(this.toggleSlides.bind(this)); 
     $(this.navLink).click(this.activeTabs.bind(this)); 
    }, 
    nextSlide: function(e) { 
     // Prevent default anchor click 
     e.preventDefault(); 
     // Set the current slide 
     var currentSlide = $('.slide.current'); 
     // Set the next slide 
     var nextSlide = $('.slide.current').next(); 
     // remove the current class from the current slide 
     currentSlide.removeClass("current"); 
     // Move the current slide to the end so we can cycle through 
     currentSlide.clone().appendTo(this.slidesContainer); 
     // remove the last slide so there is not two instances 
     currentSlide.remove(); 
     // Add current class to next slide to display it 
     nextSlide.addClass("current"); 
    }, 
    prevSlide: function(e) { 
     // Prevent defaulct anchor click 
     e.preventDefault(); 
     // Set the current slide 
     var currentSlide = $('.slide.current'); 
     // Set the last slide 
     var lastSlide = $('.slide').last(); 
     // remove the current class from the current slide 
     currentSlide.removeClass("current"); 
     // Move the last slide to the beginning so we can cycle through 
     lastSlide.clone().prependTo(this.slidesContainer); 
     // remove the last slide so there is not two instances 
     lastSlide.remove(); 
     // Add current class to new first slide 
     $('.slide').first().addClass("current"); 
    }, 
    toggleSlides: function(e) { 
     // Prevent defaulct anchor click 
     e.preventDefault(); 
     var itemData = e.currentTarget.dataset.index; 
     var currentSlide = $('.slide.current'); 
     currentSlide.removeClass("current"); 
     newSlide = $('#slide-' + itemData); 
     // currentSlide.clone().appendTo(this.slidesContainer); 
     newSlide.addClass("current"); 
     // console.log(newSlide); 
    }, 
    activeTabs: function() { 
     // *** This could be much simpler if we didnt need to match the slider buttons 
     // *** up with nav tabs. Because I need to track the slider as well, I have 
     // *** made this its own function to be used in both instances 
     // get the active slide 
     var activeSlide = $('.slide').filter(':visible'); 
     // get the active slide's id 
     var currentId = activeSlide.attr('id'); 
     // grab just the number from the active slide's id 
     var currentNum = currentId.slice(-1); 
     // remove any active gift-nav links 
     $(this.navLink).removeClass("active"); 
     // get the current tab by matching it to the current slide's id 
     var currentTab = $('.gift-nav li a[data-index="' + currentNum + '"]'); 
     // make that active 
     currentTab.addClass("active"); 
    } 
} 

$(document).ready(function(){ 

    // Init our objects 
    GiftSlider.init(); 

}); 
+1

的小提琴是不那清楚了,因爲一些圖像丟失了?並有各種圖片,但只有2個鏈接曾出現在頂端..無論如何,從我的用法...你可以更新鏈接,並可能添加評論重新。預期的行爲? –

+0

@RachelGallen對,對不起。我將更加清楚 – JordanBarber

+0

@RachelGallen滑塊中的圖像都是外部來源,因此您應該能夠看到所有這些圖像。我在這一個https://jsfiddle.net/5z29xhzg/7/中刪除了無關的'logo'圖片。有四個導航鏈接,所以我不知道爲什麼你只會看到兩個。 – JordanBarber

回答

1

該錯誤似乎在toggleSlides

編輯:以下不起作用

頁面加載時,currentSlide是幻燈片1.現在說你點擊第三個標籤。此時,您需要將第三個選項卡即第二個選項卡之前的幻燈片移動到結尾。當你說currentSlide.clone().appendTo(this.slidesContainer);時,你會將第一張幻燈片移動到最後。因此,無論您點擊了哪個標籤,當您點擊上一個按鈕時,它都會轉到第一張幻燈片。

如果你做newSlide.prev().clone().appendTo(this.slidesContainer);代碼似乎工作正常。

toggleSlides: function(e) { 
    // Prevent defaulct anchor click 
    e.preventDefault(); 
    var itemData = e.currentTarget.dataset.index; 
    var currentSlide = $('.slide.current'); 
    currentSlide.removeClass("current"); 
    newSlide = $('#slide-' + itemData); 
    //currentSlide.clone().appendTo(this.slidesContainer); 
    newSlide.prev().clone().appendTo(this.slidesContainer); 
    newSlide.addClass("current"); 
    //console.log("In toggle slide: "+newSlide.next().attr("id")); 
    //console.log("In toggle slide: "+newSlide.prev().attr("id")); 
    //console.log("In toggle slide: "+$('.slide.current').next().attr("id")); 
}, 

這似乎工作。查看https://jsfiddle.net/rfgnm992/1/。您的nextSlidepreviousSlide似乎在開始時保持當前幻燈片。 toggleSlides沒有這樣做。

toggleSlides: function(e) { 
     // Prevent defaulct anchor click 
     e.preventDefault(); 
     var itemData = e.currentTarget.dataset.index; 
     var currentSlide = $('.slide.current'); 
     currentSlide.removeClass("current"); 
     newSlide = $('#slide-' + itemData); 
     //keep new slide at the beginning and move the preceding slides to the end 
     newSlide.nextAll().addBack().prependTo(this.slidesContainer); 
    //console.log("NewSlide.next: "+newSlide.next().attr('id') + "NewSlide.next.next: "+newSlide.next().next().attr('id')+"newSlide.next.next.next: "+newSlide.next().next().next().attr('id')); 
     //currentSlide.clone().appendTo(this.slidesContainer); 
     newSlide.addClass("current"); 
     // console.log(newSlide); 
    }, 
+0

感謝您的幫助,但實際上似乎不起作用。使用上面的toggleSlides功能,點擊「幸福」導航標籤,然後開始使用右側滑塊箭頭。你陷入了最後三張幻燈片的無限循環 – JordanBarber

+1

@JordanBarber抱歉喬丹。現在檢查。我編輯了答案。讓我知道它是否有效。 – Chintan

+1

@JordanBarber更新小提琴鏈接。舊的沒有更新。 – Chintan

1

對不起,我回來了比預期的晚一點。看看那裏。想想整個追加/前置/克隆的事情是過於複雜的事情。

我得到它的工作,但它仍然有一個小錯誤。它循環顯示宏觀,向前和向後,正確的鏈接會突出顯示,但是當您點擊一個隨機鏈接時,它不會立即突出顯示,但是當您點擊下一個/上一個按鈕時,所選圖像中的相關鏈接會突出顯示。這當然是一個進步!我敢肯定,我可以用另一種眼神來展示它,但是在這裏的凌晨2點,我已經看了一個半小時了!

這裏有一個fiddle和片段(只是js的COS我味精太長 - 我只是拿出在內容的終點,沒有CSS改變段落)

GiftSlider = { 
    prev: '.slider-container .prev', 
    next: '.slider-container .next', 
    slide: '.slide', 
    slidesContainer: '#slides', 
    navLink: '.gift-nav li a', 
    init: function() { 
     // Initialize nextSlide function when clicking right arrow 
     $(this.next).click(this.nextSlide.bind(this)); 
     $(this.next).click(this.activeTabs.bind(this)); 
     // Initialize prevSlide function when clicking left arrow 
     $(this.prev).click(this.prevSlide.bind(this)); 
     $(this.prev).click(this.activeTabs.bind(this)); 
     // Initialize toggleSlides and activeTab functions when clicking nav links  
     $(this.navLink).click(this.activeTabs.bind(this)); 
    $(this.navLink).click(this.toggleSlides.bind(this)); 
    }, 
    nextSlide: function(e) { 
     // Prevent default anchor click 
     e.preventDefault(); 
     // Set the current slide 
     var currentSlide = $('.slide.current'); 
     // Set the next slide 
    var currentId = currentSlide.attr('id'); 
    var currNum = (currentId.slice(-1)); 
    var nextNum; 
    var increment = 1; 
    if (currNum == 4){ 
     nextNum = 1; 
    } 
    else 
    { 
     nextNum = parseInt(currNum) + parseInt(increment) ; 
    } 
     var nextSlide = $('#slide-' + nextNum); 
     // remove the current class from the current slide 
     currentSlide.removeClass("current"); 
     // Add current class to next slide to display it 
     nextSlide.addClass("current"); 
    // remove the last slide so there is not two instances 
    }, 
    prevSlide: function(e) { 
     // Prevent default anchor click 
     e.preventDefault(); 
     // Set the current slide 
     var currentSlide = $('.slide.current'); 
     // Set the last slide 
    var currentId = currentSlide.attr('id'); 
    var currNum = (currentId.slice(-1)); 
    var prevNum; 
    var decrement =1; 
    if (currNum == 1){ 
     prevNum = 4; 
    } 
    else 
    { 
     prevNum = parseInt(currNum) - parseInt(decrement) ; 
    } 
     var prevSlide = $('#slide-' + prevNum); 
    // Move the last slide to the beginning so we can cycle through 
     currentSlide.removeClass("current"); 
    // Add current class to new first slide 
     prevSlide.addClass("current"); 
    }, 
    toggleSlides: function(e) { 
     // Prevent defaulct anchor click 
     e.preventDefault();  
    var itemData = e.currentTarget.dataset.index; 
     var currentSlide = $('.slide.current'); 
     currentSlide.removeClass("current"); 
     newSlide = $('#slide-' + itemData); 
     newSlide.addClass("current"); 

    }, 
    activeTabs: function() { 
     var activeSlide = $('.slide').filter('.current'); 
     // get the active slide's id 
     var currentId = activeSlide.attr('id'); 
     // grab just the number from the active slide's id 
     var currentNum = currentId.slice(-1); 
     // remove any active gift-nav links 
     $(this.navLink).removeClass("active"); 
     // get the current tab by matching it to the current slide's id 
     var currentTab = $('.gift-nav li a[data-index="'+ currentNum + '"]'); 
     // make that active 
     currentTab.addClass("active"); 
    } 
} 

$(document).ready(function(){ 

    // Init our objects 
    GiftSlider.init(); 

}); 
+0

......我的天啊。非常感謝!我很抱歉,你必須長期觀察它。感謝您的幫助! – JordanBarber

+0

總是樂於幫助:) –