jquery轻量级自定义分页插件

所属分类: 网页特效-分页&翻页    2023-10-29 11:02:08

jquery轻量级自定义分页插件 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

jquery轻量级自定义分页插件(共5个文件)

    • index.html

使用方法

(function($) {
  var methods;
  methods = {
    init: function(elements, options) {
      var form;
      form = methods.getForm(options.current, options.count, options.name);
      options.radioContainer = elements[0];
      $(elements[0]).html('<div class="simple-pagination"></div>' + form);
      $(elements[0]).find('input[type=radio][name=' + options.name + ']').change(function() {
        var page;
        page = parseInt($(this).filter(':checked').val());
        options.current = page;
        methods.render(elements[0], options);
      });
      methods.render(elements[0], options);
    },
    render: function(element, options) {
      var pagination, template;
      pagination = methods.generate(options.current, options.count);
      template = methods.getTemplate(pagination, options.current, options.count, options.name);
      $(element).find('.simple-pagination').html(template);
      methods.binding(element, options);
      if ($(options.mirrorContainer).length) {
        $(options.mirrorContainer).html('<div class="simple-pagination">' + template + '</div>');
        methods.binding(options.mirrorContainer, options);
      }
    },
    binding: function(element, options) {
      var selectActiveClass, selectClass;
      selectClass = '.simple-pagination__select';
      selectActiveClass = 'simple-pagination__select--active';
      $(element).find('[data-action=pagination-spred]').click(function() {
        var select;
        select = $(this).next();
        if (select.hasClass(selectActiveClass)) {
          select.removeClass(selectActiveClass);
        } else {
          $(selectClass).removeClass(selectActiveClass);
          select.addClass(selectActiveClass);
          select.find('.simple-pagination-select').scrollTop(0);
        }
      });
      $(document).click(function(e) {
        if (!e.target.closest(selectClass) && !e.target.closest('[data-action=pagination-spred]')) {
          $(selectClass).removeClass(selectActiveClass);
        }
      });
      $(element).find('[data-action=pagination-page]').click(function() {
        var page;
        page = parseInt($(this).data('page'));
        if (!$(this).hasClass('simple-pagination__button--disable')) {
          $(options.radioContainer).find('input[value=' + page + ']').prop('checked', true).change();
          options.current = page;
        }
      });
    },
    getForm: function(currentPage, countPages, name) {
      var checked, form, i;
      form = '<div class="simple-pagination-radio">';
      i = 1;
      while (i <= countPages) {
        checked = i === currentPage ? ' checked' : '';
        form += '<input type="radio" name="' + name + '" value="' + i + '"' + checked + '>';
        i++;
      }
      return form;
    },
    generate: function(currentPage, countPages) {
      var i, j, len, pagination, previous, range, sides;
      sides = 1;
      range = [];
      pagination = [];
      previous;
      i = 1;
      while (i <= countPages) {
        if (i === 1 || i === countPages || i >= currentPage - sides && i < currentPage + sides + 1) {
          range.push(i);
        }
        i++;
      }
      for (j = 0, len = range.length; j < len; j++) {
        i = range[j];
        if (previous) {
          if (i - previous === 2) {
            pagination.push(previous + 1);
          } else if (i - previous !== 1) {
            pagination.push('...');
          }
        }
        pagination.push(i);
        previous = i;
      }
      return pagination;
    },
    getTemplate: function(pagination, currentPage, countPages, paginationName) {
      var i, template;
      template = methods.getNavigationTemplate('left', currentPage, countPages);
      i = 0;
      while (i < pagination.length) {
        if (pagination[i] !== '...') {
          template += methods.getButtonTemplate(paginationName, pagination[i], pagination[i] === currentPage);
        } else {
          template += methods.getSpredTemplate(pagination[i - 1], pagination[i + 1], paginationName);
        }
        i++;
      }
      template += methods.getNavigationTemplate('right', currentPage, countPages);
      return template;
    },
    getNavigationTemplate: function(direction, currentPage, countPages) {
      var disableClass, page, text;
      page = currentPage;
      disableClass = '';
      text = '';
      if ((direction === 'left' && page <= 1) || (direction === 'right' && page >= countPages)) {
        disableClass = ' simple-pagination__button--disable';
      }
      if (direction === 'left') {
        if (page > 1) {
          page = page - 1;
        }
        text = '&larr;';
      }
      if (direction === 'right') {
        if (page < countPages) {
          page = page + 1;
        }
        text = '&rarr;';
      }
      return '<button class="simple-pagination__button' + disableClass + '" type="button" data-action="pagination-page" data-page="' + page + '">' + text + '</button>';
    },
    getButtonTemplate: function(name, value, active) {
      var activeClass;
      activeClass = active ? ' simple-pagination__button--disable' : '';
      return '<button class="simple-pagination__button' + activeClass + '" type="button" data-action="pagination-page" data-page="' + value + '">' + value + '</button>';
    },
    getSpredTemplate: function(left, right, paginationName) {
      var i, spred;
      spred = '<div class="simple-pagination__spred"><button class="simple-pagination__button" type="button" data-action="pagination-spred">...</button><div class="simple-pagination__select"><div class="simple-pagination-select">';
      i = left;
      while (i <= right) {
        if (i !== left && i !== right) {
          spred += '<div class="simple-pagination-select__item"><button class="simple-pagination__button" type="button" data-action="pagination-page" data-page="' + i + '">' + i + '</button></div>';
        }
        i++;
      }
      spred += '</div></div></div>';
      return spred;
    }
  };
  jQuery.fn.simplePagination = function(options) {
    var el;
    el = this;
    options = $.extend({
      current: 1,
      count: 14,
      name: 'pagination',
      mirrorContainer: ''
    }, options);
    methods.init(this, options);
    return {
      setNextPage: function() {
        if (options.current < options.count) {
          options.current = options.current + 1;
          methods.init(el, options);
        }
      },
      setPrevPage: function() {
        if (options.current > 1) {
          options.current = options.current - 1;
          methods.init(el, options);
        }
      },
      setPage: function(page) {
        if (page >= 1 && page <= options.count) {
          options.current = page;
          methods.init(el, options);
        }
      }
    };
  };
})(jQuery);

站长提示:
1. 苦力吧素材官方QQ群:950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励2K币
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 分页&翻页

jquery支持拖拽触屏左右滑动的分页切换插件

一款带滑屏和上一页下一页按钮的分页插件,支持鼠标拖拽切换和移动端滑屏切换页面。
  分页&翻页
 5277  0

jquery轻量级自定义分页插件

一款支持自定义的分页插件,自动生成分页链接,可自定义当前页码数、总页数等。
  分页&翻页
 3256  0

vue响应式布局的分页插件代码

一款带跳转功能的vue.js分页插件代码,功能全齐全的vue分页组件,vue.js使用computed计算属性,数据变动执行对应的函数分页代码。
  分页&翻页
 2332  0

jquery强大实用的无刷新分页插件代码

一款强大实用的jquery无刷新分页插件,刷新页面不变保持在当前页码。
  分页&翻页
 7293  0

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论