2017-10-28 49 views
4

我已經設法建立我自己的輪播與循環我正在工作的網站。現在我的下一個目標是在調整大小時使其響應和流暢。我設法使其具有適應性,但我不喜歡它基於瀏覽器維度特別調整大小的方式,而不是流暢地調整大小。這是我一直在研究的代碼示例。試圖讓我的旋轉木馬滑塊響應調整大小

我試圖根據顯示的項目數量來調整項目大小和幻燈片。可以說,如果它是一個768像素的視口,它將顯示3個項目並將輪播幻燈片滑動3,如果其移動設備顯示爲640像素或480像素。它將由1

顯示1項和幻燈片我知道已經有現成的旋轉木馬在那裏,但我更願意嘗試和劃痕

(function ($) { 
 
     'use strict'; 
 

 
     var carousel = { 
 

 
      init : function() { 
 

 
       var carouselWidth = 0, 
 
        itemListContainer = $('.item-list').width(), 
 
        itemList = $('.carousel .item-list ul li'); 
 
       
 

 
       // Set Container Width 
 
       $('.carousel .item-list ul').children().each(function() { 
 
        carouselWidth += $(this).outerWidth(); 
 
        $('.carousel .item-list ul').width(carouselWidth + 1000); 
 
       }); 
 
       
 

 
       // function dynamicItemWidth() { 
 
       //  if ($('body') <= 1024) { 
 
       //   itemWidth = itemList.width(itemListContainer/5); 
 
       //  } 
 

 
       //  if ($('body') <= 480) { 
 
       //   itemWidth = itemList.width(itemListContainer/2); 
 
       //  } 
 

 
       //  if ($('body') <= 320) { 
 
       //   itemWidth = itemList.width(itemListContainer/1); 
 
       //  } 
 

 
       // } 
 

 
       var itemWidth = itemList.width(itemListContainer/5), 
 
        itemSize = itemWidth.width(); 
 

 
       $('.carousel .item-list ul').prepend($('.carousel .item-list ul li:last')); 
 
       $('.carousel .item-list ul').css('left', '-' + itemSize + 'px'); 
 

 

 
       $('.btns .next').click(function(){ 
 

 
        var move = $('.carousel .item-list ul li').outerWidth(); 
 
        var leftIndent = parseInt($('.carousel .item-list ul').css('left')) - move; 
 

 
        function animate(repeat, speed) { 
 
         $('.carousel .item-list ul:not(:animated)').animate({'left' : leftIndent}, speed,function(){ 
 
          $('.carousel .item-list ul').append($('.carousel .item-list ul li:first')); 
 
          $('.carousel .item-list ul').css({'left' : '-' + move + 'px'}); 
 

 
          if (repeat > 1) { 
 
           animate((repeat - 1), speed);      } 
 
         }); 
 
        } 
 

 
        animate(5, 100); 
 

 
       }); 
 

 
       $('.btns .prev').click(function() { 
 

 
        var move = $('.carousel .item-list ul li').outerWidth(); 
 
        var leftIndent = parseInt($('.carousel .item-list ul').css('left')) + move; 
 

 
        function animate(repeat, speed) { 
 
         $('.carousel .item-list ul:not(:animated)').animate({'left' : leftIndent}, speed,function(){ 
 
          $('.carousel .item-list ul').prepend($('.carousel .item-list ul li:last')); 
 
          $('.carousel .item-list ul').css({'left' : '-' + move + 'px'}); 
 

 
          if (repeat > 1) { 
 
           animate((repeat - 1), speed); 
 
          } 
 
         }); 
 
        } 
 

 
        animate(5, 100); 
 

 
       }); 
 

 
      } 
 

 
     } 
 

 
     $(document).ready(function(){ 
 
      carousel.init(); 
 
     }); 
 

 
    })(jQuery);
/* http://meyerweb.com/eric/tools/css/reset/ 
 
    v2.0 | 20110126 
 
    License: none (public domain) 
 
*/ 
 

 
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; 
 
} 
 
* { 
 
    -webkit-box-sizing: border-box; 
 
    -moz-box-sizing: border-box; 
 
    box-sizing: border-box; 
 
} 
 
/* -- #Carousel -- */ 
 

 
.carousel { 
 
    position: relative; 
 
} 
 
.carousel .item-list { 
 
    overflow: hidden; 
 
} 
 
.carousel .item-list ul { 
 
    display: flex; 
 
    flex-wrap: wrap; 
 
    list-style: none; 
 
    margin-bottom: 10px; 
 
    position: relative; 
 
    overflow: hidden; 
 
} 
 
.carousel .btns { 
 
    width: 100%; 
 
    z-index: 10; 
 
    top: 50%; 
 
} 
 
.carousel .btns li { 
 
    display: inline-block; 
 
    cursor: pointer; 
 
} 
 
.carousel.-aim-partners { 
 
    position: relative; 
 
} 
 
.carousel.-aim-partners .item-list { 
 
    max-width: 1204px; 
 
    margin: 0 auto; 
 
} 
 
.carousel.-aim-partners .item-list ul li { 
 
    float: left; 
 
    width: 230px; 
 
    margin-right: 40px; 
 
    height: 110px; 
 
    margin-top: 10px; 
 
    margin-bottom: 10px; 
 
    background-size: contain; 
 
    background-repeat: no-repeat; 
 
    background-position: center; 
 
} 
 
.carousel.-aim-partners .item-list ul li:nth-child(odd) { 
 
    background-color: teal; 
 
} 
 
.carousel.-aim-partners .item-list ul li:nth-child(even) { 
 
    background-color: #ccc; 
 
} 
 
.carousel.-aim-partners .btns { 
 
    width: 110%; 
 
    left: -70px; 
 
} 
 
.carousel.-aim-partners .btns li { 
 
    width: 35px; 
 
    height: 40px; 
 
} 
 
.carousel.-aim-partners .btns li.prev { 
 
    background: url("../images/carousel-icon-arrow.png") no-repeat 15px 0; 
 
} 
 
.carousel.-aim-partners .btns li.next { 
 
    background: url("../images/carousel-icon-arrow.png") no-repeat -20px 0; 
 
} 
 
/* -- End Carousel -- */ 
 
/*# sourceMappingURL=style.css.map */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="wrapper"> 
 
    <div class='carousel -aim-partners'> 
 

 
     <div class='item-list'> 
 
      <ul> 
 
       <li>1</li> 
 
       <li>2</li> 
 
       <li>3</li> 
 
       <li>4</li> 
 
       <li>5</li> 
 
       <li>6</li> 
 
       <li>7</li> 
 
       <li>8</li> 
 
       <li>9</li> 
 
       <li>10</li> 
 
       <li>11</li> 
 
       <li>12</li> 
 
       <li>13</li> 
 
       <li>14</li> 
 
       <li>15</li> 
 
       <li>16</li>    
 
       
 
      </ul> 
 
     </div> 
 

 

 
     <ul class="btns"> 
 
      <li class="prev"> 
 
       <i class="fa fa-angle-left" aria-hidden="true">prev</i> 
 
      </li> 
 
      <li class="next"> 
 
       <i class="fa fa-angle-right" aria-hidden="true">next</i> 
 
      </li> 
 
     </ul> 
 

 
    </div> 
 
</div>

回答

3

爲什麼要浪費時間建立它建立你自己的旋轉木馬?已經有了現成的解決方案來幫助你。嘗試Slick Slider,這是我使用過的最好的之一。 :)

+5

這樣我就可以學會從頭開始構建它,而不必使用現成的旋轉木馬,但是謝謝你提出建議:) – clestcruz

+2

我喜歡從頭開始構建東西,但有時它可以幫助我們提高生產力通過整合現有解決方案:)。不客氣,順便說一句。 – MegaColorBoy

8

您的方法在幾個地方走錯了方向。使用JavaScript調整所有容器的大小可以避免&您可以讓css完成這項工作。如果不重寫整個插件,我無法提交高質量的答案。

如果您需要,我可以編寫一個依賴CSS而不是JavaScript的版本(在大多數情況下,最好是&),並遵循JavaScript插件的「模塊結構」。

請參閱this article瞭解一下代碼的正確結構。

3

與MegaColorBoy

同意

http://kenwheeler.github.io/slick/

這是一個更好的去使用,這也給出了移動設備更好的兼容性。

另外,如果你想從頭開始編寫新的東西:)所有最好的。

+1

貓頭鷹旋轉木馬是另一個很好的! – Phil

+0

旋轉木馬與Slick => Slick將成爲贏家。貓頭鷹? –

+0

[貓頭鷹傳送帶](https://owlcarousel2.github.io/OwlCarousel2/)。檢查出 – Phil