zoukankan      html  css  js  c++  java
  • jquery: 列表轮播完善(整块轮播&单个轮播)

    // 列表整块轮播
    (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: '', //右轮播按钮选择器
                    showItemCount: 5, //显示轮播个数
                    arrowDisClass: '', // 置灰按钮,不可点击
                }
                $.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); // 必须设置item的width样式才生效
                opt.stepWidth = opt.itemWidth * opt.showItemCount;
                opt.moveCondition = opt.itemWidth * (1 - opt.showItemCount);
                opt.pointItemWidth = (1 - this.getTotalPage()) * opt.stepWidth;
    
                _this._initListWith();
    
                if (opt.arrowDisClass) {
                    this._setArrowClass(); // 按钮不隐藏,通过disable按钮控制
                } else {
                    this._showArrow(); // 当没有足够轮播项不显示按钮
                }
    
                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);
            },
            _setArrowClass() {
                let opt = this.opt;
                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);
                }
            },
            _showArrow() {
                let opt = this.opt;
                if (opt.totalItemCount > opt.showItemCount) {
                    this.$left.show();
                    this.$right.show();
                } else {
                    this.$left.hide();
                    this.$right.hide();
                }
            },
            _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;
            }
        })
    })(jQuery);
    
    // 列表单个轮播
    (function ($) {
        FCZX.globalNamespace('FCZX.SwitchUnit');
    
        FCZX.SwitchUnit = function (opt) {
            this._init(opt)
        }
    
        $.extend(FCZX.SwitchUnit.prototype, {
            _init: function (opt) {
                let _this = this;
                _this.opt = {
                    listSelector: '', //轮播列表选择器
                    itemSelector: '', //轮播列表项选择器
                    leftSelector: '', //左轮播按钮选择器
                    rightSelector: '', //右轮播按钮选择器
                    showItemCount: 5, //显示轮播个数
                    arrowDisClass: '', // 置灰按钮,不可点击
                }
                $.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.$leftArrow = $(_opt.leftSelector);
                this.$rightArrow = $(_opt.rightSelector);
    
                _opt.totalItemCount = this.$item.length;
                _opt.itemWidth = this.$item.outerWidth(true);
                this.index = 0;
                this.movableLen = _opt.totalItemCount - _opt.showItemCount
    
                this.$leftArrow.off('click.switch').on('click.switch', function () {
                    _this._movePrev();
                });
    
                this.$rightArrow.off('click.switch').on('click.switch', function () {
                    _this._moveNext();
                });
            },
            _movePrev: function () {
                let _this = this;
                let _opt = _this.opt;
                if (_this.index > 0) {
                    _this.index--;
                    let itemLeft = parseInt(_this.$list.css('left'));
                    let newItemLeft = itemLeft + _opt.itemWidth;
                    _this.$list.animate({ left: `${newItemLeft}px` }, 300);
                } else {
                    _this.index = _this.movableLen
                    _this.$list.animate({ left: -`${_this.movableLen * _opt.itemWidth}` }, 300);
                }
            },
            _moveNext: function () {
                let _this = this;
                let _opt = _this.opt;
                if (_this.index < _this.movableLen) {
                    _this.index++;
                    let itemLeft = parseInt(_this.$list.css('left'));
                    let newItemLeft = itemLeft - _opt.itemWidth;
                    _this.$list.animate({ left: `${newItemLeft}px` }, 300);
                } else {
                    _this.index = 0
                    _this.$list.animate({ left: 0 }, 300);
                }
            }
        })
    })(jQuery);

     页面结构:

    <div class="home-agent box-content mt30">
            <div class="center-header">
                <div class="center-line"></div>
                <div class="center-title">
                    <h2>经纪人</h2>
                    <p class="label">REAL ESTATE RANKING</p>
                </div>
            </div>
            <div class="agent-show mt10">
                <a href="javascript:;" class="iconfont left" id="arrowLeft">&#xe685;</a>
                <div class="agent-content">
                    <ul class="agent-list clearfix">
                        <li class="agent-item">
                            <a href="">
                                <p class="agent-image">
                                    <img src="" alt="">
                                </p>
                                <p class="agent-name">德邻地产瑞达名郡店</p>
                                <p class="agent-user">郭经理start</p>
                            </a>
                        </li>
                        <li class="agent-item">
                            <a href="">
                                <p class="agent-image">
                                    <img src="" alt="">
                                </p>
                                <p class="agent-name">德邻地产瑞达名郡店</p>
                                <p class="agent-user">郭经理</p>
                            </a>
                        </li>
                        <li class="agent-item">
                            <a href="">
                                <p class="agent-image">
                                    <img src="" alt="">
                                </p>
                                <p class="agent-name">德邻地产瑞达名郡店</p>
                                <p class="agent-user">郭经理</p>
                            </a>
                        </li>
                        <li class="agent-item">
                            <a href="">
                                <p class="agent-image">
                                    <img src="" alt="">
                                </p>
                                <p class="agent-name">德邻地产瑞达名郡店</p>
                                <p class="agent-user">郭经理</p>
                            </a>
                        </li>
                        <li class="agent-item">
                            <a href="">
                                <p class="agent-image">
                                    <img src="" alt="">
                                </p>
                                <p class="agent-name">德邻地产瑞达名郡店</p>
                                <p class="agent-user">郭经理</p>
                            </a>
                        </li>
                        <li class="agent-item">
                            <a href="">
                                <p class="agent-image">
                                    <img src="" alt="">
                                </p>
                                <p class="agent-name">德邻地产瑞达名郡店</p>
                                <p class="agent-user">郭经理</p>
                            </a>
                        </li>
                        
                    </ul>
                </div>
                <a href="javascript:;" class="iconfont right" id="arrowRight">&#xe688;</a>
            </div>
        </div>

    content: 定长,相对定位

    list:绝对定位,计算长度

  • 相关阅读:
    简单布局2
    面试问题之操作系统:Linux下进程的内存结构
    面试问题之C++语言:说一说C++中四种cast转换
    面试问题之C++语言:说一下static关键字的作用
    面试问题之操作系统:动态链接库和静态链接库的区别
    面试问题之数据结构与算法:简述深度优先遍历和广度优先遍历
    面试问题之C++语言:C与C++的区别
    面试问题之C++语言:Overload、Override及Overwirte的区别
    面试问题之计算机网络:TCP三次握手四次挥手
    面试问题之计算机网络:TCP滑动窗口
  • 原文地址:https://www.cnblogs.com/Nyan-Workflow-FC/p/13938117.html
Copyright © 2011-2022 走看看