# 使用思维导图，优雅的完成自己的代码

2017-12-16 10:10:19来源:https://segmentfault.com/a/1190000012435669作者:SegmentFault人点击

function showButton() {
var
\$numb = \$('.numb'),
min = (btn_cur-1)*5 + 1,
max = 0;
if (btn_cur == btn_acount) {
max = page_acount + 1;
} else if (btn_cur < btn_acount) {
max = (btn_cur*5) + 1;
};
\$numb.hide();
for(var i=min; i<max; i++) {
!function() {
\$numb.eq(i%5 - 1).show();
\$numb.eq(i%5 - 1).attr('data-list', i);
\$numb.eq(i%5 - 1).html(i);
}();
}
}

function showButton() {
var \$numb = \$('.numb');
if (btn_acount == 1) {
\$numb.hide();
\$('.more').hide();
\$('.last').hide();
for(var i=0; i<acountPage; i++) {
\$numb.eq(i).show();
\$numb.eq(i).attr('data-list', i);
}
};
var min = (btn_cur-1)*5 + 1;
var max = 0;
if (btn_cur == btn_acount) {
max = page_acount+1;
} else if (btn_cur < btn_acount) {
max = btn_cur*5+1;
};
\$numb.hide();
for( var i=min; i<max; i++ ) {
\$numb.eq(i%5 - 1).show();
\$numb.eq(i%5 - 1).attr('data-list', i);
\$numb.eq(i%5 - 1).html(i);
}
}

\$('.pos_page').on('click', function(e) {
// e.preventDefault();
var \$target = \$(e.target);
var className = \$target.attr('class').split(' ')[0];
\$target.on('selectstart', function() {
return false;
});
switch(className) {
case 'prev_page':
if (index!=0) {
index -= 1;
page_cur -= 1;
} else if (index == 0) {
if (btn_cur > 1 ) {
index = 4;
btn_cur -= 1;
page_cur -= 1;
} else if (btn_cur == 1) {
return;
}
};
showPage(page_cur, page_every);
showButton();
setFocus();
break;
case 'next_page':
if (btn_cur == btn_acount) {
if (index == page_acount%5 - 1) {
return;
} else if( index < page_acount%5 - 1) {
index ++;
page_cur ++;
}
} else if (btn_cur < btn_acount) {
if (index == 4) {
index = 0;
btn_cur += 1;
page_cur += 1;
} else if (index < 4) {
index ++;
page_cur++;
};
};
showPage(page_cur, page_every);
showButton();
setFocus();
break;
case 'numb':
page_cur = \$target.attr('data-list');
index = page_cur%5-1;
console.log(page_cur);
showPage(page_cur, page_every);
showButton();
setFocus();
break;
case 'more':
if (btn_cur < btn_acount) {
btn_cur += 1;
index = 0;
showButton();
setFocus();
page_cur = \$('.numb').eq(0).html();
showPage(page_cur, page_every);
};
break;
case 'last':
if (btn_cur != btn_acount) {
btn_cur = btn_acount;
index = 0;
page_cur = (btn_cur - 1)*5 +1;
showPage(page_cur, page_every);
showButton();
setFocus();
};
default:
break;
}
});