0

我将 jcarousellite 用于简单的 JQuery 轮播,但我想对其进行一些更改,以便隐藏最左边和最右边的项目的图像,但显示在中间项目上。

要做到这一点,我需要能够随时找到哪些列表项显示在视口中。我已经尝试在源代码中挖掘并使用包含的回调,但我无法获得与显示的项目相符的索引号。

有没有人有这方面的经验或关于如何解决它的想法?

编辑

OK 在一定程度上解决了这个问题,但它仍然无法正常工作。afterEnd 函数内置在插件中,并提供当前可见项目的对象。我用它来提供列表元素的 ID,并应用一些转换。

问题是这都是插件的“外部”,所以如果我启用自动滚动,所有这些代码都会被忽略。我需要某种方式将此代码插入到插件中的函数中,这就是我害怕的地方。

    $(".ccvi-carousel").jCarouselLite({
    btnNext: ".next",
    btnPrev: ".prev",
    speed:  800,
    //auto: 2000,
    afterEnd: function(a) { 
        if (start=true) {
            $('li#1 .cf-img').hide();
        }
        left = $(a[0]).attr("id");
        mid = $(a[1]).attr("id");
        right = $(a[2]).attr("id");
        $('.prev').click(function() {
            $('li#' + left + ' .cf-img').fadeIn();
            $('li#' + mid + ' .cf-img').hide();
            $('li#' + right + ' .cf-img').hide();
        });
        $('.next').click(function() {
            $('li#' + left + ' .cf-img').hide();
            $('li#' + mid + ' .cf-img').hide();
            $('li#' + right + ' .cf-img').fadeIn();
        });             
        //alert("Left:" + left + "Middle:" + mid + "Right:" + right + "");
    }
});

我认为这是插件中的函数,我需要将代码放入其中,但我看不到它是如何工作的。

        function go(to) {
        if(!running) {

            if(o.beforeStart)
                o.beforeStart.call(this, vis());

            if(o.circular) {            // If circular we are in first or last, then goto the other end
                if(to<=o.start-v-1) {           // If first, then goto last
                    ul.css(animCss, -((itemLength-(v*2))*liSize)+"px");
                    // If "scroll" > 1, then the "to" might not be equal to the condition; it can be lesser depending on the number of elements.
                    curr = to==o.start-v-1 ? itemLength-(v*2)-1 : itemLength-(v*2)-o.scroll;
                    //alert (curr);
                } else if(to>=itemLength-v+1) { // If last, then goto first
                    ul.css(animCss, -( (v) * liSize ) + "px" );
                    // If "scroll" > 1, then the "to" might not be equal to the condition; it can be greater depending on the number of elements.
                    curr = to==itemLength-v+1 ? v+1 : v+o.scroll;
                    //alert (curr);
                } else {
                    curr = to;
                    //alert (curr);
                }
            } else {                    // If non-circular and to points to first or last, we just return.
                if(to<0 || to>itemLength-v) return;
                else curr = to;
            }                           // If neither overrides it, the curr will still be "to" and we can proceed.

            running = true;

            ul.animate(
                animCss == "left" ? { left: -(curr*liSize) } : { top: -(curr*liSize) } , o.speed, o.easing,
                function() {
                    //alert (curr-2);
                    if(o.afterEnd)
                        o.afterEnd.call(this, vis());
                    running = false;
                }
            );
            // Disable buttons when the carousel reaches the last/first, and enable when not
            if(!o.circular) {
                $(o.btnPrev + "," + o.btnNext).removeClass("disabled");
                $( (curr-o.scroll<0 && o.btnPrev)
                    ||
                   (curr+o.scroll > itemLength-v && o.btnNext)
                    ||
                   []
                 ).addClass("disabled");
            }

        }
        return false;
    };
4

1 回答 1

0

我认为这会帮助你:

如何使用 jCarousel Lite 获取当前索引?

除了上述文章之外,我还将第 241 行设置为以下内容

div.css({position: "relative", "z-index": "2", left: "-1144px"}); //changed the left from 0 to -1144px to allow the carousel circle to repeat offscreen

这会将整个旋转木马向左移动。在我的情况下,我将旋转木马移动 1144 像素,因为我设置了宽度幻灯片,但您可以轻松地动态计算左侧偏移量。

现在我有 2 张离开屏幕的幻灯片,我设置了 .jCarouselLite({ visible: 6 }); 这给了我 2 张可见的中间幻灯片和另外两张屏幕外的幻灯片。

接下来将以下代码放在 ul.animate(); 之后的任何位置;

li.eq(curr+1).addClass("prev");
li.eq(curr+2).addClass("current");
li.eq(curr+3).addClass("next");

最后确保将正确的索引幻灯片设置为当前。按照链接文章的指导方针,我实际上是在索引 8 上开始我的轮播

$(".jCarouselLite li").eq(7).addClass("prev");
$(".jCarouselLite li").eq(8).addClass("current");
$(".jCarouselLite li").eq(9).addClass("next");

要尝试更多地了解您上面概述的 jCarousel Lite 脚本,请尝试放置 alert(curr); 或 console.log(curr); 在每个“if(o。”区域之后。如果一切顺利,您将获得当前幻灯片的索引。

于 2012-02-28T06:45:08.093 回答