2017-09-03 165 views
0

我有一個旋轉木馬滑塊,我剛剛創建的滑塊在創建多個傳送帶時滑動不正確。它假設只能滑動一個。例如,如果我有一個旋轉木馬,旋轉木馬就會滑動一個。如果我必須旋轉木馬顯示它滑動兩個旋轉木馬滑塊在創建多個旋轉木馬時滑動不正確

我似乎無法找到我的代碼中的錯誤,當它根據顯示的傳送帶數滑動。以下是我目前正在處理的代碼示例。我的目標是讓它滑動只有1

<div class="faculty-carousel"> 
    <ul class="faculty-items"> 
     <li>Item 1</li> 
     <li>Item 2</li> 
     <li>Item 3</li> 
     <li>Item 4</li> 
     <li>Item 5</li> 
     <li>Item 6</li> 
    </ul> 

    <div class="controls"> 
     <div class="prev"> 
     prev 
     </div> 

     <div class="next"> 
     next 
     </div> 
    </div> 
</div> 


<div class="faculty-carousel"> 
    <ul class="faculty-items"> 
     <li>Item 1</li> 
     <li>Item 2</li> 
     <li>Item 3</li> 
     <li>Item 4</li> 
     <li>Item 5</li> 
     <li>Item 6</li> 
    </ul> 

    <div class="controls"> 
     <div class="prev"> 
     prev 
     </div> 

     <div class="next"> 
     next 
     </div> 
    </div> 
</div> 

<div class="faculty-carousel"> 
    <ul class="faculty-items"> 
     <li>Item 1</li> 
     <li>Item 2</li> 
     <li>Item 3</li> 
     <li>Item 4</li> 
     <li>Item 5</li> 
     <li>Item 6</li> 
    </ul> 

    <div class="controls"> 
     <div class="prev"> 
     prev 
     </div> 

     <div class="next"> 
     next 
     </div> 
    </div> 
</div> 

<script type="text/javascript"> 


var activeSlide = 0; 
$('.faculty-carousel').attr('data-slide', '0'); 



$('.prev').on('click', function(e) { 
    event.stopPropagation(); 

    var carouselWrapper  = $(this).closest('.faculty-carousel'), 
     facultyProfilePanel = carouselWrapper.find('.faculty-items li'), 
     facultyProfileCount = facultyProfilePanel.length, 
     viewPortSize  = $(window).width(), 
     carousel   = carouselWrapper.find('.faculty-items'), 
     position   = 0, 
     currentSlide  = parseInt(carouselWrapper.attr('data-slide')); 

    // Check if data-slide attribute is greater than 0 
    if (currentSlide > 0) { 
     // Decremement current slide 
     currentSlide--; 
     // Assign CSS position to clicked slider 
     var transformPercentage = -1 * currentSlide/facultyProfileCount * 100; 
     carousel.css('transform', 'translateX(' + transformPercentage + '%)'); 

     // Update data-slide attribute 
     carouselWrapper.attr('data-slide', currentSlide); 
     activeSlide = currentSlide; 
    } 

    console.log('prev'); 

}); 

$('.next').on('click', function(e) { 
    event.stopPropagation(); 
    // store variable relevent to clicked slider 

    var carouselWrapper  = $(this).closest('.faculty-carousel'), 
     facultyProfilePanel = carouselWrapper.find('.faculty-items li'), 
     facultyProfileCount = facultyProfilePanel.length, 
     viewPortSize = $(window).width(), 
     carousel = carouselWrapper.find('.faculty-items'), 
     position = 0, 
     currentSlide = parseInt(carouselWrapper.attr('data-slide')); 

    // Check if dataslide is less than the total slides 
    if (currentSlide < facultyProfileCount - 1) { 
     // Increment current slide 
     currentSlide++; 
     // Assign CSS position to clicked slider 
     var transformPercentage = -1 * currentSlide/facultyProfileCount * 100; 
     carousel.css('transform', 'translateX(' + transformPercentage + '%)'); 

     // Update data-slide attribute 
     carouselWrapper.attr('data-slide', currentSlide); 
     activeSlide = currentSlide; 
    } 

    console.log('next'); 


}) 


function fCarousel() { 

    // var activeSlide = 0; 
    // $('.faculty-carousel').attr('data-slide', '0'); 


    var viewPortSize  = $(window).width(), 
     facultyPanel = $('.faculty-carousel .faculty-items li'), 
     profileCount = facultyPanel.length, 
     // activeSlide   = 0, 
     carousel   = $('.faculty-carousel .faculty-items'); 

    // $('.faculty-carousel').attr('data-slide', '0'); 

    //Set Panel Size based on viewport 

    if (viewPortSize <= 1920) { 
     var profilePanelSize = viewPortSize/5 
    } 


    if (viewPortSize < 1024) { 
     var profilePanelSize = viewPortSize/4 
    } 

    if (viewPortSize < 768) { 
     var profilePanelSize = viewPortSize/3 
    } 

    if (viewPortSize < 480) { 
     var profilePanelSize = viewPortSize 
    } 

    carousel.outerWidth(profilePanelSize * profileCount); 
    facultyPanel.outerWidth(profilePanelSize); 
    carousel.css('transform', 'translateX(' + 0 + '%)'); 

} 

$(document).ready(function() { 
    fCarousel(); 
}) 

$(window).on('resize', function(){ 
    fCarousel(); 
}) 

</script> 

html, body, div, span, applet, object, iframe, 
h1, h2, h3, h4, h5, h6, p, blockquote, pre, 
a, abbr, acronym, address, big, cite, code, 
del, dfn, em, img, ins, kbd, q, s, samp, 
small, strike, strong, sub, sup, tt, var, 
b, u, i, center, 
dl, dt, dd, ol, ul, li, 
fieldset, form, label, legend, 
table, caption, tbody, tfoot, thead, tr, th, td, 
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary, 
time, mark, audio, video { 
    margin: 0; 
    padding: 0; 
    border: 0; 
    font-size: 100%; 
    font: inherit; 
    vertical-align: baseline; 
} 
/* HTML5 display-role reset for older browsers */ 
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section { 
    display: block; 
} 
body { 
    line-height: 1; 
} 
ol, ul { 
    list-style: none; 
} 
blockquote, q { 
    quotes: none; 
} 
blockquote:before, blockquote:after, 
q:before, q:after { 
    content: ''; 
    content: none; 
} 
table { 
    border-collapse: collapse; 
    border-spacing: 0; 
} 

.faculty-items li { 
    height : 100px; 
} 

.faculty-items li:nth-child(odd) { 
    background-color : grey; 
} 

.faculty-items li:nth-child(even) { 
    background-color : aqua 
} 


.faculty-items { 
    overflow : hidden; 
    position : relative; 
    right  : 0; 
    display : flex; 

    -webkit-transition: transform 0.3s linear; 
} 

.faculty-carousel .controls { 
    display : block; 
} 
+0

是什麼問題???? – 2017-09-03 05:33:11

+0

@ programmer21更新我的問題抱歉。沒有意識到我錯過了一些細節 – clestcruz

+1

我已經寫了一個[回覆](https://stackoverflow.com/a/38388450/4297364)到一個類似的問題。 –

回答

1

的問題似乎在於在fCarousel函數,其中profileCount計數屬於每個項目旋轉木馬。

facultyPanel = $('.faculty-carousel .faculty-items li'), 
profileCount = facultyPanel.length, 

這意味着,在函數的結束時,每個傳送帶的寬度被設定爲屬於每轉盤項的數目。

carousel.outerWidth(profilePanelSize * profileCount); 
facultyPanel.outerWidth(profilePanelSize); 

這期間prevnext點擊事件試圖move the carousel by a percentage (of its width)時(即,它的寬度是基於頁面上旋轉木馬項目,即使是那些屬於另一個轉盤數設置成爲一個問題)。

carousel.css('transform', 'translateX(' + transformPercentage + '%)'); 

相反,你可以通過每個旋轉臺(carouselWrapper)迭代,並設置基於該有專門的項目數的寬度。

var viewPortSize  = $(window).width(), 
    carouselWrapper = $('.faculty-carousel') 
; 

... 

carouselWrapper.each(function(){ 
    var wrapper = $(this); 
    // wrapper.attr('data-slide', '0'); 

    var facultyPanel = wrapper.find('.faculty-items li'), 
    profileCount = facultyPanel.length, 
    // activeSlide   = 0, 
    carousel   = wrapper.find('.faculty-items'); 

    carousel.outerWidth(profilePanelSize * profileCount); 
    facultyPanel.outerWidth(profilePanelSize); 
    carousel.css('transform', 'translateX(' + 0 + '%)'); 
}); 

var activeSlide = 0; 
 
$('.faculty-carousel').attr('data-slide', '0'); 
 

 

 

 
$('.prev').on('click', function(e) { 
 
    event.stopPropagation(); 
 

 
    var carouselWrapper  = $(this).closest('.faculty-carousel'), 
 
     facultyProfilePanel = carouselWrapper.find('.faculty-items li'), 
 
     facultyProfileCount = facultyProfilePanel.length, 
 
     viewPortSize  = $(window).width(), 
 
     carousel   = carouselWrapper.find('.faculty-items'), 
 
     position   = 0, 
 
     currentSlide  = parseInt(carouselWrapper.attr('data-slide')); 
 

 
    // Check if data-slide attribute is greater than 0 
 
    if (currentSlide > 0) { 
 
     // Decremement current slide 
 
     currentSlide--; 
 
     // Assign CSS position to clicked slider 
 
     var transformPercentage = -1 * currentSlide/facultyProfileCount * 100; 
 
     carousel.css('transform', 'translateX(' + transformPercentage + '%)'); 
 

 
     // Update data-slide attribute 
 
     carouselWrapper.attr('data-slide', currentSlide); 
 
     activeSlide = currentSlide; 
 
    } 
 

 
}); 
 

 
$('.next').on('click', function(e) { 
 
    event.stopPropagation(); 
 
    // store variable relevent to clicked slider 
 

 
    var carouselWrapper  = $(this).closest('.faculty-carousel'), 
 
     facultyProfilePanel = carouselWrapper.find('.faculty-items li'), 
 
     facultyProfileCount = facultyProfilePanel.length, 
 
     viewPortSize = $(window).width(), 
 
     carousel = carouselWrapper.find('.faculty-items'), 
 
     position = 0, 
 
     currentSlide = parseInt(carouselWrapper.attr('data-slide')); 
 

 
    // Check if dataslide is less than the total slides 
 
    if (currentSlide < facultyProfileCount - 1) { 
 
     // Increment current slide 
 
     currentSlide++; 
 
     // Assign CSS position to clicked slider 
 
     var transformPercentage = -1 * currentSlide/facultyProfileCount * 100; 
 
     carousel.css('transform', 'translateX(' + transformPercentage + '%)'); 
 

 
     // Update data-slide attribute 
 
     carouselWrapper.attr('data-slide', currentSlide); 
 
     activeSlide = currentSlide; 
 
    } 
 

 

 
}) 
 

 

 
function fCarousel() { 
 

 
    // var activeSlide = 0; 
 
    // $('.faculty-carousel').attr('data-slide', '0'); 
 

 

 
    var viewPortSize  = $(window).width(), 
 
     carouselWrapper = $('.faculty-carousel') 
 
    ; 
 
    
 
    //Set Panel Size based on viewport 
 

 
    if (viewPortSize <= 1920) { 
 
     var profilePanelSize = viewPortSize/5 
 
    } 
 

 

 
    if (viewPortSize < 1024) { 
 
     var profilePanelSize = viewPortSize/4 
 
    } 
 

 
    if (viewPortSize < 768) { 
 
     var profilePanelSize = viewPortSize/3 
 
    } 
 

 
    if (viewPortSize < 480) { 
 
     var profilePanelSize = viewPortSize 
 
    } 
 
    
 
    carouselWrapper.each(function(){ 
 
     var wrapper = $(this); 
 
     // wrapper.attr('data-slide', '0'); 
 

 
     var facultyPanel = wrapper.find('.faculty-items li'), 
 
     profileCount = facultyPanel.length, 
 
     // activeSlide   = 0, 
 
     carousel   = wrapper.find('.faculty-items'); 
 

 
     carousel.outerWidth(profilePanelSize * profileCount); 
 
     facultyPanel.outerWidth(profilePanelSize); 
 
     carousel.css('transform', 'translateX(' + 0 + '%)'); 
 
    }); 
 

 

 

 
} 
 

 
$(document).ready(function() { 
 
    fCarousel(); 
 
}) 
 

 
$(window).on('resize', function(){ 
 
    fCarousel(); 
 
})
html, body, div, span, applet, object, iframe, 
 
h1, h2, h3, h4, h5, h6, p, blockquote, pre, 
 
a, abbr, acronym, address, big, cite, code, 
 
del, dfn, em, img, ins, kbd, q, s, samp, 
 
small, strike, strong, sub, sup, tt, var, 
 
b, u, i, center, 
 
dl, dt, dd, ol, ul, li, 
 
fieldset, form, label, legend, 
 
table, caption, tbody, tfoot, thead, tr, th, td, 
 
article, aside, canvas, details, embed, 
 
figure, figcaption, footer, header, hgroup, 
 
menu, nav, output, ruby, section, summary, 
 
time, mark, audio, video { 
 
    margin: 0; 
 
    padding: 0; 
 
    border: 0; 
 
    font-size: 100%; 
 
    font: inherit; 
 
    vertical-align: baseline; 
 
} 
 
/* HTML5 display-role reset for older browsers */ 
 
article, aside, details, figcaption, figure, 
 
footer, header, hgroup, menu, nav, section { 
 
    display: block; 
 
} 
 
body { 
 
    line-height: 1; 
 
} 
 
ol, ul { 
 
    list-style: none; 
 
} 
 
blockquote, q { 
 
    quotes: none; 
 
} 
 
blockquote:before, blockquote:after, 
 
q:before, q:after { 
 
    content: ''; 
 
    content: none; 
 
} 
 
table { 
 
    border-collapse: collapse; 
 
    border-spacing: 0; 
 
} 
 

 
.faculty-items li { 
 
    height : 100px; 
 
} 
 

 
.faculty-items li:nth-child(odd) { 
 
    background-color : grey; 
 
} 
 

 
.faculty-items li:nth-child(even) { 
 
    background-color : aqua 
 
} 
 

 

 
.faculty-items { 
 
    overflow : hidden; 
 
    position : relative; 
 
    right  : 0; 
 
    display : flex; 
 

 
    -webkit-transition: transform 0.3s linear; 
 
} 
 

 
.faculty-carousel .controls { 
 
    display : block; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="faculty-carousel"> 
 
    <ul class="faculty-items"> 
 
     <li>Item 1</li> 
 
     <li>Item 2</li> 
 
     <li>Item 3</li> 
 
     <li>Item 4</li> 
 
     <li>Item 5</li> 
 
     <li>Item 6</li> 
 
    </ul> 
 

 
    <div class="controls"> 
 
     <div class="prev"> 
 
     prev 
 
     </div> 
 

 
     <div class="next"> 
 
     next 
 
     </div> 
 
    </div> 
 
</div> 
 

 

 
<div class="faculty-carousel"> 
 
    <ul class="faculty-items"> 
 
     <li>Item 1</li> 
 
     <li>Item 2</li> 
 
     <li>Item 3</li> 
 
     <li>Item 4</li> 
 
     <li>Item 5</li> 
 
     <li>Item 6</li> 
 
    </ul> 
 

 
    <div class="controls"> 
 
     <div class="prev"> 
 
     prev 
 
     </div> 
 

 
     <div class="next"> 
 
     next 
 
     </div> 
 
    </div> 
 
</div> 
 

 
<div class="faculty-carousel"> 
 
    <ul class="faculty-items"> 
 
     <li>Item 1</li> 
 
     <li>Item 2</li> 
 
     <li>Item 3</li> 
 
     <li>Item 4</li> 
 
     <li>Item 5</li> 
 
     <li>Item 6</li> 
 
    </ul> 
 

 
    <div class="controls"> 
 
     <div class="prev"> 
 
     prev 
 
     </div> 
 

 
     <div class="next"> 
 
     next 
 
     </div> 
 
    </div> 
 
</div>