简体   繁体   English

Cycle2 Carousel活动滑动在中心

[英]Cycle2 Carousel active slide in center

I am using Cycle2 with a carousel pager, in the same way as this demo: http://jquery.malsup.com/cycle2/demo/caro-pager.php 我正在使用带有轮播寻呼机的Cycle2,就像这个演示一样: http//jquery.malsup.com/cycle2/demo/caro-pager.php

Currently the active slide in the demo is on the left unless you are on the last few slides. 目前,演示中的活动幻灯片位于左侧,除非您在最后几张幻灯片中。 What I would like is: 我想要的是:

  1. for the active slide to start on the left, on slide 1 幻灯片1上的活动幻灯片从左侧开始
  2. then when slide 2 is clicked, the thumbnails don't move but the second thumbnail shows as active. 然后,当单击幻灯片2时,缩略图不会移动,但第二个缩略图显示为活动。
  3. When slide 3 is clicked, the thumbnails don't move but the third thumbnail (in the middle) becomes active). 单击幻灯片3时,缩略图不会移动,但第三个缩略图(中间)变为活动状态)。
  4. When slide 4 is clicked, all thumbnails move one to left and fourth thumbnail (now in the middle) is active. 单击幻灯片4时,所有缩略图都会向左移动一个缩略图,而第四个缩略图(现在位于中间)处于活动状态。
  5. Same as above for slides 5, 6, 7. 与上面的幻灯片5,6,7相同。
  6. When slide 8 is clicked, thumbnails don't move but eighth thumbnail becomes active (now second from right) 单击幻灯片8时,缩略图不会移动,但第八个缩略图变为活动状态(现在从右侧第二个)
  7. When slide 9 is clicked, thumbnails don't move but ninth thumbnail become active (the last thumbnail on right). 单击幻灯片9时,缩略图不会移动,但第九个缩略图变为活动状态(右侧的最后一个缩略图)。

See diagram: 见图:

在此输入图像描述

I have copied the demo to a jsfiddle here: http://jsfiddle.net/Qhp2g/1/ but would really appreciate some help as I'm not sure where to start(!) I have tried using data-cycle-carousel-offset="40%" on #cycle-2 as this user tried with a similar problem to me Cycle2: Center Carousel active slide below main slideshow and this does not work because you can't access the last slides and there is space on the left at the beginning. 我已经将这个演示版复制到了一个jsfiddle: http//jsfiddle.net/Qhp2g/1/但是我真的很感激一些帮助,因为我不知道从哪里开始(!)我尝试过使用data-cycle-carousel-offset="40%" #cycle-2上的data-cycle-carousel-offset="40%" ,因为这个用户尝试了类似的问题#cycle-2 :主幻灯片下方的中心轮播活动幻灯片 ,这不起作用,因为你无法访问最后的幻灯片,并且有空间离开了。

I assume I may need to change the plugin carousel script - http://malsup.github.io/jquery.cycle2.carousel.js - for my needs but really not sure where to start! 我想我可能需要更改插件轮播脚本 - http://malsup.github.io/jquery.cycle2.carousel.js - 根据我的需要,但真的不知道从哪里开始! Thank you so much for any help. 非常感谢你的帮助。

The thing you will have to do is edit the jquery.cycle2.carousel.js file, and change the transition function. 您需要做的是编辑jquery.cycle2.carousel.js文件,并更改转换函数。 I hard-coded the offset, but you can probably code it to be based off of the percentage that you give it if you want. 我对偏移量进行了硬编码,但您可以将其编码为基于您提供的百分比(如果需要)。

Here are the main changes: 以下是主要变化:

var offset = 2; //Set the offset of your carousel, for 5 it will be 2.
//Use this offset to calculate the max and min slide locations.
var maxCurr = opts.slideCount - (opts.carouselVisible - offset);
var minCurr = opts.slideCount - (opts.carouselVisible + offset);

...

//Add the following conditions to account for the new minCurr
else if (hops > 0 && opts.nextSlide <= minCurr){
    hops = 0;
}
else if (hops < 0 && opts.currSlide <= minCurr){
    hops = 0;
}
else if (hops > 0 && opts.currSlide < minCurr && opts.nextSlide > minCurr){
    hops = opts.nextSlide - minCurr;   
}
else if (hops < 0 && opts.nextSlide < minCurr){
    hops = opts.nextSlide - minCurr;   
}

See the working fiddle here: http://jsfiddle.net/m66tS/10/ 在这里看到工作小提琴: http//jsfiddle.net/m66tS/10/

I have taken Bryan's wonderful answer above and made some changes. 我已经采取了布莱恩上面的精彩答案并做了一些改变。 There was a bug with his solution if the minCurr was actually less than the offset (sometimes it even goes negative). 如果minCurr实际上小于偏移(有时它甚至变为负数),他的解决方案就会出现错误。 His solution worked for 8+ thumbnails with 5 visible and and offset of 2. However I only had 6 thumbnails with 5 visible and an offset of 2 therefore minCurr = 6 - (5 + 2) = -1 Also if I had 7 thumbnails with 5 visible and an offset of 2 minCurr = 1 and the same problem exists. 他的解决方案适用于8个缩略图,其中5个可见,偏移为2.但是我只有6个缩略图,其中5个可见,偏移量为2因此minCurr = 6 - (5 + 2)= -1此外,如果我有7个缩略图5可见,偏移量为2 minCurr = 1,存在同样的问题。

The solution is to change 解决方案是改变

var minCurr = opts.slideCount - (opts.carouselVisible + offset);

to

var minCurr = opts.slideCount - (opts.carouselVisible + offset);
if(minCurr < offset ){
   var minCurr = offset;
} 

After doing this I also had to make some other adjustments for the case where you clicked forward or backward by over the offset amount near the start of end and the carousel moved too far. 在这样做之后,我还必须对你在前端附近的偏移量向前或向后点击并且旋转木马移动太远的情况做出一些其他调整。

My edited code now looks like this: 我编辑的代码现在看起来像这样:

    var offset = 2; //Number of slides to offset
    // handle all the edge cases for wrapping & non-wrapping
    if ( opts.allowWrap === false ) {
        fwd = hops > 0;
        var currSlide = opts._currSlide;
        var maxCurr = opts.slideCount - (opts.carouselVisible - offset);
        var minCurr = opts.slideCount - (opts.carouselVisible + offset);
        if(minCurr < offset){
            minCurr = offset;
        }
        if(fwd){ // MOVING FORWARDS
            if ( opts.nextSlide > maxCurr && currSlide == maxCurr|| opts.nextSlide <= minCurr ) {
                hops = 0;
            }
            else if (opts.currSlide < minCurr && opts.nextSlide > maxCurr || opts.nextSlide > maxCurr){
                 hops += opts.currSlide - maxCurr;
            }
            else if (opts.currSlide < minCurr && opts.nextSlide > minCurr){
                 hops = opts.nextSlide - minCurr;  
            }
            else {
                currSlide = opts.currSlide;
            }

        } else { // MOVING BACKWARDS
            if ( opts.currSlide > maxCurr && opts.nextSlide > maxCurr || opts.currSlide <= minCurr ) {
                hops = 0;
            }
            else if (hops < -offset && opts.nextSlide < minCurr){
                hops = opts.nextSlide;
            }
            else if ( opts.currSlide > maxCurr) {
                hops += opts.currSlide - maxCurr;
            }
            else if (opts.nextSlide < minCurr){
                hops = opts.nextSlide - minCurr; 
            }
            else {
                currSlide = opts.currSlide; 
            }
        }


        moveBy = this.getScroll( opts, vert, currSlide, hops );
        opts.API.opts()._currSlide = opts.nextSlide > maxCurr ? maxCurr : opts.nextSlide;
    }

You can use JQuery lightSlider, It support active thumbnail slide on pager always center also have custom callback to customization. 您可以使用JQuery lightSlider,它支持在寻呼机上的活动缩略图幻灯片始终中心还具有自定义回调以进行自定义。

http://sachinchoolur.github.io/lightslider/ http://sachinchoolur.github.io/lightslider/

http://sachinchoolur.github.io/lightslider/settings.html http://sachinchoolur.github.io/lightslider/settings.html

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

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