Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
654 views
in Technique[技术] by (71.8m points)

jquery - Removing an item from jCarousel plug-in

I have a question regarding the jCarousel plug-in (from sorgalla). How do I remove items from it the right way?

You can see how far I've gone here. Try deleting a few items and then scrolling to the right, you'll see an "emtpy scroll" eventually, and that's what I'm trying to get rid of.

I've tried using the remove(); jQuery function instead of changing the css to display: none; but that produces a weird white blank space where the item used to be. If you look at the jquery.jcarousel.js on line 400 you'll see a remove function, but I'm not sure on how to use it.

Any help is much appreciated. Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're example makes sense, except since you're stepping outside the bounds of the plugin, jCarousel doesn't know to update itself. From the docs, it seems like the remove() method you mentioned would work. However, it my trials I never could get the jCarousel object to actually "do the right thing" and update it's buttons, scroll around, etc.

Because of all that, I wrote an additional method on the jCarousel class that does exactly that. You call removeAndAnimate(1) to remove the first item in the carousel and rebuild everything so that next/prev buttons are enabled/disabled, the works.

Also worth noting, the remove() function jCarousel provides prevents you from removing an item that is currently being shown, which is exactly what we wanted to do (allow the user to remove an image from the carousel by clicking on it, for example).

The implementation for removeAndAnimate(i):

removeAndAnimate: function(i) {

        var e = this.get(i);

        var d = this.dimension(e);

        if (i < this.first) this.list.css(this.lt, $jc.intval(this.list.css(this.lt)) + d + 'px');

        e.remove();
        this.options.size--;

        var di = this.options.visible != null ? Math.ceil(this.clipping() / this.options.visible) : null;
        var li = this.list.children('li');
        var self = this;

        if (li.size() > 0) {
            var wh = 0, i = this.options.offset;
            li.each(function() {
                self.format(this, i++);
                wh += self.dimension(this, di);
            });

            this.list.css(this.wh, wh + 'px');            
        }

        this.scroll(0,true);
        this.buttons();

    }

I recommend placing this directly after the remove() implementation. To access the jCarousel instance itself with jQuery, outside of the callback functions:

var carousel = $("#mycarousel").data("jcarousel");
carousel.removeAndAnimate(1);

That should work!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...