显示 jCarouselLite 中的位置

发布于 2024-09-15 23:23:38 字数 3669 浏览 11 评论 0原文

我使用 jcarousellite 来做一个简单的 JQuery 轮播,但我想稍微改变一下,以便隐藏最左边和最右边的项目的图像,但显示在中间的项目上。

为此,我需要能够随时找到视图端口中显示的列表项。我尝试深入研究源代码并使用包含的回调,但我无法获得与所显示的项目相符的索引号。

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

编辑

好的在某种程度上解决了这个问题,但它仍然无法正常工作。 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;
    };

I'm using jcarousellite for a simple JQuery carousel, but I would like to alter it a bit so that images are hidden for the items to the far left and the far right, but shown on the middle items.

To do this though I need to be able to find which list items are being displayed in the view port at any one time. I've tried digging around in the source code and using the included callbacks, but I can't ever get index numbers which tally with the item being shown.

Does anyone have any experience of this or ideas on how to solve it?

EDIT

OK solved this to some extent, but it's still not working correctly. The afterEnd function is built into the plugin and provides an object of the currently visible items. I've used this to provide the IDs of the list elements, and apply some transitions.

The problem is that this is all "outside" of the plugin, so if I enable auto scrolling, all this code is ignored. I need some way of inserting this code into the functions within the plugin, and that's where I'm a bit stuck I'm afraid.

    $(".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 + "");
    }
});

I think this is the function within the plugin that I need to get my code into, but I can't see how it works.

        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;
    };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乄_柒ぐ汐 2024-09-22 23:23:38

我认为这会对您有所帮助:

如何获取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

这会将整个轮播向左移动。就我而言,我将轮播移动 1144px,因为我设置了幻灯片宽度,但您可以轻松地动态计算左侧偏移量。

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

接下来将以下代码放置在 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.”区域之后。如果一切顺利,您将获得当前幻灯片的索引。

I think this will help you:

How to get the current index with jCarousel Lite?

In addition to the above article I've set line 241 to the following

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

This shifts the entire carousel left. In my case I'm shifting the carousel 1144px because I have set width slides but you could just as easily calculate the left offset dynamically.

Now that I have 2 slides offscreen left I set the .jCarouselLite({ visible: 6 }); this gives me 2 visible slides in the middle and another two offscreen right.

Next place the following code anywhere after the ul.animate();

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

Lastly be sure to set the proper index slide to current. Following the guidelines of the linked article I'm actually starting my carousel on index 8

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

To try and figure out a little more about the jCarousel Lite script you've outlined above try placing alert(curr); or console.log(curr); after each of the "if(o." areas. If all goes well you'll get the index of the current slide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文