简体   繁体   English

如何将幻灯片与 swiper 滑块对齐?

[英]How can I align the slides to the swiper slider?

Good afternoon.下午好。 Please tell me how I can align the slides so that when you select the first or last slide on the left or right, there are the same number of next and previous slides.请告诉我如何对齐幻灯片,以便在您选择左侧或右侧的第一张或最后一张幻灯片时,下一张和上一张幻灯片的数量相同。

In other words when selecting the first or last slide it should be like this: In other words when selecting the first or last slide it should be like this:

在此处输入图片说明

but at the moment it looks like this:但目前它看起来像这样: 在此处输入图片说明

In other words, if the first slide is active, then there is only the last slide to the left of it and the penultimate slide is missing.换句话说,如果第一张幻灯片处于活动状态,那么它左边只有最后一张幻灯片,倒数第二张幻灯片丢失。

 var sliderSelector = '.swiper-container', options = { init: true, initialSlide: 1, loop: true, speed: 1200, slidesPerView: 1.6848, // or 'auto' spaceBetween: 0, centeredSlides: true, mousewheelControl: false, lazyLoading: true, slideToClickedSlide: true, effect: 'coverflow', // 'cube', 'fade', 'coverflow', autoplay: { delay: 10000, disableOnInteraction: false, }, coverflowEffect: { rotate: 0, // Slide rotate in degrees stretch: 400, // Stretch space between slides (in px) depth: 380, // Depth offset in px (slides translate in Z axis) modifier: 1, // Effect multipler slideShadows: false, // Enables slides shadows }, grabCursor: true, parallax: true, pagination: { el: '.swiper-pagination', clickable: true, renderBullet: function(index, className) { return `<span class="dot swiper-pagination-bullet">${index +1}</span>`; }, }, navigation: { nextEl: 0, prevEl: 0, }, breakpoints: { 1023: { slidesPerView: 5, spaceBetween: 0 } }, // Events on: { imagesReady: function() { this.el.classList.remove('loading'); } } }; var mySwiper = new Swiper(sliderSelector, options); // Initialize slider mySwiper.init();
 [class^="swiper-button-"], .swiper-container-horizontal>.swiper-pagination-bullets .swiper-pagination-bullet, .swiper-container-horizontal>.swiper-pagination-bullets .swiper-pagination-bullet::before { -webkit-transition: all 0.3s ease; transition: all 0.3s ease; } *, *:before, *:after { box-sizing: border-box; margin: 0; padding: 0; } .wrapper { width: 100%; display: flex; justify-content: center; background: #E5E5E5; } .container { width: 1200px; } .swiper-slide-active img { outline: 10px solid rgba(255, 255, 255, 0.5); outline-offset: -10px; } .swiper-slide img { outline: 10px solid rgba(255, 255, 255, 0.5); outline-offset: -10px; } .swiper-slide img { position: relative; } .swiper-container { width: 100%; min-height: 470px; height: auto; -webkit-transition: opacity 1s ease; transition: opacity 1s ease; } .swiper-slide img { display: block; max-width: 100%; height: auto; } .swiper-container.swiper-container-coverflow { padding-top: 2%; } .swiper-container.loading { opacity: 0; visibility: hidden; transition: all 0.5s ease-in; } .swiper-container:hover .swiper-button-prev, .swiper-container:hover .swiper-button-next { -webkit-transform: translateX(0); transform: translateX(0); opacity: 1; visibility: visible; } .swiper-slide { background-position: center; background-size: cover; } .swiper-slide .entity-img { display: none; } .swiper-slide .content { position: absolute; top: 40%; left: 0; width: 50%; padding-left: 5%; color: #fff; } .swiper-slide .content .title { font-size: 2.6em; font-weight: bold; margin-bottom: 30px; } .swiper-slide img { filter: brightness(20%); transition: all 0.5s ease-in; position: relative; z-index: 2; } .swiper-slide-prev img { filter: brightness(40%); transition: all 0.5s ease-in; } .swiper-slide-next img { filter: brightness(40%); transition: all 0.5s ease-in; } .swiper-slide { outline: 10px solid #ffffff; outline-offset: -10px; position: relative; z-index: 5; } swiper-slide-next img { filter: brightness(50%); transition: all 0.5s ease-in; } .swiper-slide-active img { filter: brightness(100%); transition: all 0.5s ease-in; } .swiper-slide .content .caption { display: block; font-size: 13px; line-height: 1.4; } [class^="swiper-button-"] { width: 44px; opacity: 0; visibility: hidden; } .swiper-button-prev { -webkit-transform: translateX(50px); transform: translateX(50px); } .swiper-button-next { -webkit-transform: translateX(-50px); transform: translateX(-50px); } .swiper-container-horizontal>.swiper-pagination-bullets .swiper-pagination-bullet { margin: 0 9px; position: relative; width: 12px; height: 12px; background-color: #fff; opacity: 0.4; } .swiper-container-horizontal>.swiper-pagination-bullets .swiper-pagination-bullet::before { content: ""; position: absolute; top: 50%; left: 50%; width: 18px; height: 18px; -webkit-transform: translate(-50%, -50%); transform: translate(-50%, -50%); border: 0px solid #fff; border-radius: 50%; } .swiper-container-horizontal>.swiper-pagination-bullets .swiper-pagination-bullet:hover, .swiper-container-horizontal>.swiper-pagination-bullets .swiper-pagination-bullet.swiper-pagination-bullet-active { opacity: 1; transition: all 5s ease-in; } .swiper-container-horizontal>.swiper-pagination-bullets .swiper-pagination-bullet.swiper-pagination-bullet-active::before { border-width: 1px; } @media (max-width: 1180px) { .swiper-slide .content .title { font-size: 25px; } .swiper-slide .content .caption { font-size: 12px; } } @media (max-width: 1023px) { .swiper-container { height: 40vw; } .swiper-container.swiper-container-coverflow { padding-top: 0; } }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.0.7/css/swiper.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.4.1/js/swiper.min.js"></script> <div class="wrapper"> <div class="container"> <section class="swiper-container loading"> <div class="swiper-wrapper"> <div class="swiper-slide"> <img src="https://i.ytimg.com/vi/954FKvpQN2g/maxresdefault.jpg"> </div> <div class="swiper-slide"> <img src="https://i.ytimg.com/vi/954FKvpQN2g/maxresdefault.jpg"> </div> <div class="swiper-slide"> <img src="https://i.ytimg.com/vi/954FKvpQN2g/maxresdefault.jpg"> </div> <div class="swiper-slide"> <img src="https://i.ytimg.com/vi/954FKvpQN2g/maxresdefault.jpg"> </div> <div class="swiper-slide"> <img src="https://i.ytimg.com/vi/954FKvpQN2g/maxresdefault.jpg"> </div> <div class="swiper-slide"> <img src="https://i.ytimg.com/vi/954FKvpQN2g/maxresdefault.jpg"> </div> <div class="swiper-slide"> <img src="https://i.ytimg.com/vi/954FKvpQN2g/maxresdefault.jpg"> </div> </div> <!-- If we need pagination --> <div class="swiper-pagination"></div> <!-- If we need navigation buttons --> <div class="swiper-button-prev swiper-button-white"></div> <div class="swiper-button-next swiper-button-white"></div> </section> </div> </div>

Swiper seems to work by creating duplicate slides if needed (so as to have some 'spares' for showing if it 'runs out' when cycling round.如果需要,Swiper 似乎可以通过创建重复的幻灯片来工作(以便有一些“备用”来显示它在循环时是否“用完”。

You have 7 slides and want 5 showing at once so I feel it ought to have enough without creating any spares - ie there is always an extra 2 slides waiting in the wings to put into view.您有 7 张幻灯片,并且希望一次显示 5 张,所以我觉得它应该足够了,而无需创建任何备用 - 即总是有额外的 2 张幻灯片等待在机翼中放入视图。 However, there must be something in the Swiper arithmetic which makes this not work and it 'runs out' in the case you have highlighted - eg when slide 1 is the main slide.但是,Swiper 算法中必须有一些东西使这不起作用,并且在您突出显示的情况下它“用完了” - 例如,当幻灯片 1 是主幻灯片时。

If you add如果添加

loopAdditionalSlides: 10,//(10 is a generous guess, it'll be worth trying lower number) loopAdditionalSlides: 10,//(10 是一个慷慨的猜测,值得尝试更低的数字)

to options then with到选项然后用

loop: true, //as you have now loop: true, //就像你现在一样

and take out the breakpoints option (the documentation at https://swiperjs.com/api/ says breakpoints don't work with loop) then the right number of slides show on the screen as it loops round automatically or with user clicking.取出断点选项( https://swiperjs.com/api/ 上的文档说断点不适用于循环)然后屏幕上会显示正确数量的幻灯片,因为它会自动循环或用户单击。

So, this solves the immediate question.所以,这解决了眼前的问题。

However, I noticed that the slider doesn't seem very responsive - as the viewport is narrowed the slides' widths don't narrow and the slidesPerView parameter appears to have no effect.但是,我注意到滑块似乎响应速度不快 - 随着视口变窄,幻灯片的宽度不会变窄,并且 slidesPerView 参数似乎没有效果。 I guess the hard coded container width at 1200px coupled with the slidesPerView: 1.6848 accounts for that - Swiper assuming it has 1200px to play with regardless of the actual width of the viewport.我猜 1200 像素的硬编码容器宽度加上 slidesPerView: 1.6848 说明了这一点 - Swiper 假设它有 1200 像素可以玩,而不管视口的实际宽度。 Is there a way of making Swiper shrink the width of slides proportional to the viewport width so that the same sort of view (5 slides, 4 being partially hidden) is seen on all devices/window widths?有没有办法让 Swiper 缩小与视口宽度成比例的幻灯片宽度,以便在所有设备/窗口宽度上看到相同类型的视图(5 张幻灯片,4 张部分隐藏)?

As you had a breakpoint at 1023px I imagine you wanted to show 5 whole slides if the viewport is wider than that.由于您在 1023px 处有一个断点,我想如果视口比这更宽,您想显示 5 张完整的幻灯片。 It appears this is not possible given it says breakpoint doesn't work with loop.看起来这是不可能的,因为它说断点不适用于循环。 That is strange as looping and how many slides there are fully viewable on the screen would not seem to have anything to do with each other.这很奇怪,因为循环播放和屏幕上完全可见的幻灯片似乎彼此没有任何关系。 May be worth raising on github if it is important for your application?如果它对您的应用程序很重要,可能值得在 github 上提出吗?

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

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