pages.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. $.fn.pages = function(options){
  2. var defaults = {
  3. ref : 'data-ref',
  4. total : 'data-total',
  5. current : 'data-current',
  6. page : 'data-page'
  7. };
  8. options = $.extend({}, defaults, options);
  9. var ref = $(this).attr(options.ref);
  10. var current = parseInt($(this).attr(options.current));
  11. var total = parseInt($(this).attr(options.total));
  12. $(this).find('[data-page]').bind('click', function(){
  13. var page = $(this).attr(options.page);
  14. if(page == 'next') {
  15. page = current + 1;
  16. }else if(page == 'more'){
  17. page = current + 5;
  18. page = page > total ? total : page;
  19. }else if(page == 'less'){
  20. page = current -5;
  21. page = page <= 0 ? 1 : page;
  22. }else if(page == 'pre'){
  23. page = current - 1;
  24. }else if(page == 'end') {
  25. page = total;
  26. }else if(page == current) {
  27. return;
  28. }
  29. if(page > total || page < 1) return;
  30. var url;
  31. //去掉当前ref中的currentPage参数
  32. ref = ref.replace(/[\?&]currentPage=[^&]*&?$/g, "");
  33. if(ref.indexOf('?') == -1) {
  34. url = ref + '?currentPage=' + page;
  35. }else{
  36. url = ref + '&currentPage=' + page;
  37. }
  38. window.location.href = url;
  39. });
  40. }