简体   繁体   English

在调整大小时尝试使我的旋转木马滑块响应

[英]Trying to make my carousel slider responsive when resizing

I've managed to build my own carousel with loop for a website I'm working. 我已经设法为我正在工作的网站建立了我自己的旋转木马循环。 Right now my next objective is to make it responsive and fluid when resizing. 现在我的下一个目标是在调整大小时使其具有响应性和流畅性。 I've managed to make it adaptive but I just do not like the way how it resizes specifically based on the browser dimension instead of having resize fluidly. 我已经设法让它自适应但我不喜欢它根据浏览器维度专门调整大小的方式,而不是流畅地调整大小。 Here is a sample of the code that I've been working on. 这是我一直在研究的代码示例。

I'm trying to make the items resize and slide based on the number of items shown. 我正在尝试根据显示的项目数量调整项目大小和幻灯片。 Lets say if it's a 768px viewport it will show 3 items and slide the carousel by 3 and if its mobile say 640px or 480px. 让我们说如果它是一个768px的视口,它将显示3个项目并将旋转木马滑动3,如果它的移动设备说640px或480px。 It will show 1 item and slide by 1 它将显示1项并按1滑动

I know there are already ready made carousel out there but I would prefer to try and build it by scratch 我知道那里已经有现成的旋转木马,但我更愿意尝试从头开始构建它

 (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> 

Your approach has gone in wrong directions at several places. 你的方法在几个地方走错了方向。 Resizing all the containers using JavaScript can be avoided & you can let css do the work. 可以避免使用JavaScript调整所有容器的大小,并且可以让css完成工作。 I can't submit a quality answer to this without rewriting the whole plugin. 如果不重写整个插件,我无法提交高质量的答案。

If you need, I can write a version that relies on CSS than JavaScript (which is preferable & adaptable in most cases) and follows the "Module Structure" of JavaScript plugins. 如果需要,我可以编写一个依赖CSS而不是JavaScript的版本(在大多数情况下这是优选的和适应性的)并遵循JavaScript插件的“模块结构”。

Please refer this article to learn a bit about the proper structuring of code. 请参阅本文以了解有关正确构造代码的一些信息。

Why waste time building your own carousel? 为什么要浪费时间建立自己的旋转木马? There are already ready made solutions to help you with. 已有现成的解决方案可以帮助您。 Try Slick Slider , it's one of the best I've used. 试试Slick Slider ,这是我用过的最好的之一。 :) :)

Agree with MegaColorBoy 同意MegaColorBoy

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

This is a better one to go with, also this gives better compatibility over the mobile devices. 这是一个更好的选择,这也提供了更好的移动设备兼容性。

Also if you want to write from scratch than write something new :) all the best. 另外,如果你想从头开始写,而不是写一些新东西:)最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM