首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >JavaScript轮播的多个实例

JavaScript轮播的多个实例
EN

Stack Overflow用户
提问于 2014-06-13 17:13:53
回答 2查看 394关注 0票数 1

因此,我编写了以下代码,以便使用Hammer.jsjQuery在JavaScript中构建旋转木马

代码语言:javascript
运行
复制
var hCarousel = {

    container: false,
    panes: false,
    pane_width: 0,
    pane_count: 0,
    current_pane: 0,

    build: function( element ) {

        hCarousel.container = $(element).find('.hcarousel-inner-container');

        hCarousel.panes = $(hCarousel.container).find('> .section');

        hCarousel.pane_width = 0;
        hCarousel.pane_count = hCarousel.panes.length;

        hCarousel.current_pane = 0;

        hCarousel.setPaneDimensions( element );

        $(window).on('load resize orientationchange', function() {

            hCarousel.setPaneDimensions( element );

        });

        $(element).hammer({ drag_lock_to_axis: true })
                    .on('release dragleft dragright swipeleft swiperight', hCarousel.handleHammer);

    },

    setPaneDimensions: function( element ){

        hCarousel.pane_width = $(element).width();

        hCarousel.panes.each(function() {
            $(this).width(hCarousel.pane_width);
        });

        hCarousel.container.width(hCarousel.pane_width*hCarousel.pane_count);

    },

    next: function() {

        return hCarousel.showPane(hCarousel.current_pane+1, true);

    },

    prev: function() {

        return hCarousel.showPane(hCarousel.current_pane-1, true);

    },

    showPane: function( index ) {

        // between the bounds
        index = Math.max(0, Math.min(index, hCarousel.pane_count-1));
        hCarousel.current_pane = index;

        var offset = -((100/hCarousel.pane_count)*hCarousel.current_pane);

        hCarousel.setContainerOffset(offset, true);

    },

    setContainerOffset: function( percent, animate ) {

        hCarousel.container.removeClass("animate");

        if(animate) {
            hCarousel.container.addClass("animate");
        }

        if(Modernizr.csstransforms3d) {
            hCarousel.container.css("transform", "translate3d("+ percent +"%,0,0) scale3d(1,1,1)");
        }
        else if(Modernizr.csstransforms) {
            hCarousel.container.css("transform", "translate("+ percent +"%,0)");
        }
        else {
            var px = ((hCarousel.pane_width*hCarousel.pane_count) / 100) * percent;
            hCarousel.container.css("left", px+"px");
        }

    },

    handleHammer: function( ev ) {

        ev.gesture.preventDefault();

        switch(ev.type) {

            case 'dragright':
            case 'dragleft':
                // stick to the finger
                var pane_offset = -(100/hCarousel.pane_count)*hCarousel.current_pane;
                var drag_offset = ((100/hCarousel.pane_width)*ev.gesture.deltaX) / hCarousel.pane_count;

                // slow down at the first and last pane
                if((hCarousel.current_pane == 0 && ev.gesture.direction == Hammer.DIRECTION_RIGHT) ||
                    (hCarousel.current_pane == hCarousel.pane_count-1 && ev.gesture.direction == Hammer.DIRECTION_LEFT)) {
                    drag_offset *= .4;
                }

                hCarousel.setContainerOffset(drag_offset + pane_offset);

                break;

            case 'swipeleft':
                hCarousel.next();
                ev.gesture.stopDetect();
                break;

            case 'swiperight':
                hCarousel.prev();
                ev.gesture.stopDetect();
                break;

            case 'release':
                // more then 50% moved, navigate
                if(Math.abs(ev.gesture.deltaX) > hCarousel.pane_width/2) {
                    if(ev.gesture.direction == 'right') {
                        hCarousel.prev();
                    } else {
                        hCarousel.next();
                    }
                }
                else {
                    hCarousel.showPane(hCarousel.current_pane, true);
                }
                break;
        }

    }

}

我这样称呼它:

代码语言:javascript
运行
复制
var hSections;

$(document).ready(function(){

    hSections = hCarousel.build('.hcarousel-container');

});

它工作得很好。但我想让它,以便我可以在页面上有多个旋转木马,这再次工作…但是容器的总宽度是不正确的,因为它结合了两个carousel的宽度。

我怎么能运行这样的东西的多个实例,但是代码知道它正在与哪个实例交互,这样事情就不会变得混乱,等等。

EN

Stack Overflow用户

发布于 2014-06-13 17:40:03

我会试着把它变成一个你可以像类一样使用的函数。然后,您可以为您的轮播创建单独的对象。

因此,您将拥有类似于以下内容:

代码语言:javascript
运行
复制
function HCarousel (element) {
    this.element=element;
    this.container= false;
    this.panes= false;
    this.pane_width= 0;
    this.pane_count= 0;
    this.current_pane= 0;
}

然后像这样在类上添加每个方法。

代码语言:javascript
运行
复制
HCarousel.prototype.build = function() {
    this.container = $(element).find('.hcarousel-inner-container');
    this.panes = $(hCarousel.container).find('> .section');
    this.pane_width = 0;
    this.pane_count = hCarousel.panes.length;
    this.current_pane = 0;
    this.setPaneDimensions( element );
    $(window).on('load resize orientationchange', function() {
        this.setPaneDimensions( element );
    });
    $(this.element).hammer({ drag_lock_to_axis: true }).on('release dragleft dragright swipeleft swiperight', hCarousel.handleHammer);
};

等等。这应该会给你一个基本的想法。需要稍微重写一下,然后你就可以创建一个轮播了,就像这样:

代码语言:javascript
运行
复制
var carousel1 = new HCarousel('.hcarousel-container');

希望这能让你走上正轨。

JS中实际上并不存在类,但这是一种使用函数模拟类的方法。这里有一篇关于在JS http://www.phpied.com/3-ways-to-define-a-javascript-class/中使用类的好文章

票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24201731

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档