slider.js 5.75 KB
/*  
    detail页缩略图滚动插件
    author:liuyue
    date:2015-05-12
*/

define('plugins/slider', function(require, exports) {
    var $ = require("jquery");

    ;
    (function(global, undefined) {

        var Slider = function(element, options) {
            this.options = options;
            this.$element = $(element);
            this.$lis = this.$element.children();
            this.len = this.$lis.length;
            this.index = 0;
            this.overWidth = 0;
            this.init();
        };


        Slider.DEFAULTS = {
            prevBtn: '.detail-slide-ctrl-prev', //向前控制按钮类
            nextBtn: '.detail-slide-ctrl-next', //向后控制按钮类
            wrapClass: '.detail-slide-ctrl', //包裹运动元素的层,用于计算运动元素的left
            showNum: 6 //可视元素个数
        };


        Slider.prototype = {
            init: function() {
                this.$lis.eq(0).addClass('current');
                this._setWidth();
                this._bindEvent();
            },

            _setWidth: function() {
                this.$element.width(this.$lis.eq(1).outerWidth() * this.len);
            },

            _bindEvent: function() {
                var that = this,
                    timer;
                //网页改变大小时重新计算宽度
                $(window).resize(function() {
                    that._setWidth();
                }).resize();

                this.$lis.on('click', function() {
                    if (that.$element.is(':animated') || $(this).attr('class') == 'current') return;
                    that.slideTo($(this).index());
                })

                if (this.len >= this.options.showNum) {
                    $(that.options.prevBtn).show().on('click.prev', function() {
                        that._prev();
                    });

                    $(that.options.nextBtn).show().on('click.next', function() {
                        that._next();
                    });
                }


            },

            _animate: function(offset) {
                if (this.$element.is(':animated')) return;
                this.$element.stop().animate({
                    'left': offset
                }, 300);
            },

            slideTo: function(index) {
                var that = this,
                    left = Math.abs(parseInt(this.$element.css('left')));
                this.$lis.eq(index).addClass('current').siblings().removeClass('current');
                if (this.len < this.options.showNum) return;
                if (this.$element.is(':animated') || $('.detail-slide-piclist .box').is(':animated')) return;

                //console.log(index);
                if (index === that.options.showNum - 1 + parseInt(left / that.$lis.eq(1).outerWidth()) && left < this.$element.outerWidth() - $(this.options.wrapClass).outerWidth() - this.overWidth) {
                    $(that.options.nextBtn).trigger("click.next");
                } else if (index === 0 && left !== 0) {
                    $(that.options.nextBtn).trigger("click.next");
                } else if (index === parseInt(left / that.$lis.eq(1).outerWidth()) && left > 0) {
                    $(that.options.prevBtn).trigger("click.prev");
                } else if (index === that.len - 1 && left === 0) {
                    console.log(left);
                    $(that.options.prevBtn).trigger("click.prev");
                }
            },

            _prev: function() {
                var offset;
                if (this.$element.is(':animated')) return;
                this.overWidth = this.$lis.eq(1).outerWidth() - this.$lis.eq(1).width();
                this.index--;
                offset = -this.$lis.eq(1).outerWidth() * this.index;
                if (offset > 0) {
                    offset = -(this.$element.outerWidth() - $(this.options.wrapClass).outerWidth() - this.overWidth);
                    if (this.len === this.options.showNum) {
                        this.index = 1
                    } else {
                        this.index = this.len - this.options.showNum;
                    }

                }

                this._animate(offset);
            },

            _next: function() {
                var offset;
                if (this.$element.is(':animated')) return;
                this.overWidth = this.$lis.eq(1).outerWidth() - this.$lis.eq(1).width();
                this.index++;
                offset = -this.$lis.eq(1).outerWidth() * this.index;
                if (this.index >= this.len - this.options.showNum) {
                    offset = -(this.$element.outerWidth() - $(this.options.wrapClass).outerWidth() - this.overWidth);
                }
                if (parseInt(this.$element.css('left')) === -(this.$element.outerWidth() - $(this.options.wrapClass).outerWidth() - this.overWidth)) {
                    offset = 0;
                    this.index = 0;
                }


                this._animate(offset);
            }

        }


        function Plugin(option, _relatedTarget) {
            return this.each(function() {
                var $this = $(this);
                var data = $this.data('yoho.Slider');
                var options = $.extend({}, Slider.DEFAULTS, $this.data(), typeof option == 'object' && option);
                // 如果没有初始化,则初始化该对象,并且把该对象绑定到此元素的yoho.PicSilder属性下。
                if (!data) $this.data('yoho.Slider', (data = new Slider(this, options)));

                // 如果传递的options是一个字符串,则表示调用改对象的原型方法。
                if (typeof option == 'string') data[option](_relatedTarget);
            });
        };


        $.fn.slider = Plugin;
        $.fn.slider.Constructor = Slider;

    })(this);
});