1. 程式人生 > 實用技巧 >jquery: 列表輪播優化

jquery: 列表輪播優化

建立全域性名稱空間

(function () {
    /**
     * FCZX.foo.bar
     */
    let FCZX = {};
    FCZX.globalNamespace = function (ns) {
        var nsParts = ns.split(".");
        var root = window;
        for (var i = 0; i < nsParts.length; i++) {
            if (typeof root[nsParts[i]] == "undefined") {
                root[nsParts[i]] 
= {}; } root = root[nsParts[i]]; } return root; } window.FCZX = FCZX; })()

定義輪播例項

(function ($) {
    FCZX.globalNamespace('FCZX.Switch');

    FCZX.Switch = function (opt) {
        this._init(opt)
    }

    $.extend(FCZX.Switch.prototype, {
        isMoveOver: 
true, //是否完成位移 _init: function (opt) { let _this = this; _this.opt = { listSelector: '', //輪播列表選擇器 itemSelector: '', //輪播列表項選擇器 leftSelector: '', //左輪播按鈕選擇器 rightSelector: '', //右輪播按鈕選擇器 pointItemWidth: 0, //
輪播判斷點 showItemCount: 5, //顯示輪播個數 arrowDisClass: 'arrow-disabled' } $.extend(true, _this.opt, opt || {}); _this._initDomEvent(); return _this; }, _initDomEvent: function () { let _this = this; let opt = this.opt; this.$list = $(opt.listSelector); this.$item = $(opt.itemSelector); this.$left = $(opt.leftSelector); this.$right = $(opt.rightSelector); opt.totalItemCount = this.$item.length; opt.itemWidth = this.$item.outerWidth(true); opt.stepWidth = opt.itemWidth * opt.showItemCount; _this._initListWith(); if (opt.totalItemCount <= opt.showItemCount) { this.$left.addClass(opt.arrowDisClass); this.$right.addClass(opt.arrowDisClass); } else { this.$left.removeClass(opt.arrowDisClass); this.$right.removeClass(opt.arrowDisClass); } //預設整塊輪播 if (opt.pointItemWidth === 0) { opt.pointItemWidth = (1 - this.getTotalPage(opt.totalItemCount)) * opt.stepWidth; } this.$left.off('click.switch').on('click.switch', function () { if (_this.$left.hasClass(opt.arrowDisClass)) return; if (!_this.isMoveOver) return; _this.isMoveOver = false; _this._movePrev(opt); }); this.$right.off('click.switch').on('click.switch', function () { if (_this.$right.hasClass(opt.arrowDisClass)) return; if (!_this.isMoveOver) return; _this.isMoveOver = false; _this._moveNext(opt); }); }, _initListWith: function () { let opt = this.opt; this.$list.css('width', opt.itemWidth * opt.totalItemCount); }, _initListLeft: function () { this.$list.css('left', 0); }, _movePrev: function (opt) { let _this = this; let $list = _this.$list; let itemLeft = parseInt($list.css('left')); if (itemLeft === 0) { $list.animate({ left: `${opt.pointItemWidth}px` }, 300, function () { _this.isMoveOver = true; }); } else { let newItemLeft = itemLeft + opt.stepWidth; $list.animate({ left: `${newItemLeft}px` }, 300, function () { _this.isMoveOver = true; }); } return _this; }, _moveNext: function (opt) { let _this = this; let $list = _this.$list; let itemLeft = parseInt($list.css('left')); if (itemLeft === opt.pointItemWidth) { $list.animate({ left: 0 }, 300, function () { _this.isMoveOver = true; }); } else { let newItemLeft = itemLeft - opt.stepWidth; $list.animate({ left: `${newItemLeft}px` }, 300, function () { _this.isMoveOver = true; }); } return _this; }, getTotalPage: function () { let totalPage = 0; let opt = this.opt; totalPage = Math.ceil(opt.totalItemCount / opt.showItemCount); return totalPage; }, // movePosition: function () { // // 子元素與直接上級元素的距離 // let itemPosition = targetItem.position().left; // //計算當前頁 // let currentPage = Math.floor(itemPosition / stepWidth); // let relativePosition = listContentParent.offset().left - targetItem.offset().left; // // 計算可視範圍內相對偏移量 // if (relativePosition < moveCondition || relativePosition > 0) { // listContent.css('left', `-${currentPage * stepWidth}px`); // } // } }) })(jQuery);