forked from mythsman/hexo-douban
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
179 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<script> | ||
/** | ||
* Created by myths on 18-2-8. | ||
*/ | ||
// ======== index ======== | ||
Element.prototype.siblings = function () { | ||
let siblingElement = []; | ||
let sibs = this.parentNode.children; | ||
for (let i = 0; i < sibs.length; i++) { | ||
if (sibs[i] !== this) { | ||
siblingElement.push(sibs[i]); | ||
} | ||
} | ||
return siblingElement; | ||
}; | ||
function tabClick() { | ||
//修改标签样式 | ||
this.classList.add('hexo-douban-active'); | ||
let sibs = this.siblings(); | ||
for (let j = 0; j < sibs.length; j++) { | ||
sibs[j].classList.remove('hexo-douban-active'); | ||
} | ||
//显示对应板块 | ||
let itemId = this.id.replace('tab', 'item'); | ||
let target = document.getElementById(itemId); | ||
target.classList.remove('hexo-douban-hide'); | ||
target.classList.add('hexo-douban-show'); | ||
sibs = document.getElementById(itemId).siblings(); | ||
for (let k = 0; k < sibs.length; k++) { | ||
sibs[k].classList.remove('hexo-douban-show'); | ||
sibs[k].classList.add('hexo-douban-hide'); | ||
} | ||
} | ||
function initIndex() { | ||
let tabs = document.getElementsByClassName("hexo-douban-tab"); | ||
for (let i = 0; i < tabs.length; i++) { | ||
tabs[i].onclick = tabClick; | ||
tabs[i].onclick.apply(tabs[i]); | ||
} | ||
tabs[2].click(); | ||
} | ||
initIndex(); | ||
// ======== pagination ======== | ||
let item_per_page = <%= item_per_page %>; | ||
function makePageNum(num, arr) { | ||
return (num + 1) + ' / ' + (Math.ceil(arr.length / item_per_page === 0 ? 1 : Math.ceil(arr.length / item_per_page))); | ||
} | ||
function firstBtn() { | ||
let sibs = this.parentNode.siblings(); | ||
displayPage(sibs, 0); | ||
this.parentNode.getElementsByClassName('hexo-douban-pagenum')[0].innerText = makePageNum(0, sibs); | ||
} | ||
function previousBtn() { | ||
let sibs = this.parentNode.siblings(); | ||
let currNum = this.parentNode.getElementsByClassName('hexo-douban-pagenum')[0].innerText; | ||
currNum = currNum.substr(0, currNum.indexOf('/') - 1); | ||
currNum = parseInt(currNum, 10) - 1; | ||
if (currNum > 0) { | ||
currNum--; | ||
} | ||
displayPage(sibs, currNum); | ||
this.parentNode.getElementsByClassName('hexo-douban-pagenum')[0].innerText = makePageNum(currNum, sibs); | ||
} | ||
function nextBtn() { | ||
let sibs = this.parentNode.siblings(); | ||
let currNum = this.parentNode.getElementsByClassName('hexo-douban-pagenum')[0].innerText; | ||
currNum = currNum.substr(0, currNum.indexOf('/') - 1); | ||
currNum = parseInt(currNum, 10) - 1; | ||
if (currNum < Math.ceil(sibs.length / item_per_page) - 1) { | ||
currNum++; | ||
} | ||
displayPage(sibs, currNum); | ||
this.parentNode.getElementsByClassName('hexo-douban-pagenum')[0].innerText = makePageNum(currNum, sibs); | ||
} | ||
function lastBtn() { | ||
let sibs = this.parentNode.siblings(); | ||
displayPage(sibs, Math.ceil(sibs.length / item_per_page) - 1); | ||
this.parentNode.getElementsByClassName('hexo-douban-pagenum')[0].innerText = makePageNum(Math.ceil(sibs.length / item_per_page) - 1 === -1 ? 0 : Math.ceil(sibs.length / item_per_page) - 1, sibs); | ||
} | ||
function displayPage(arr, num) { | ||
for (let i = 0; i < arr.length; i++) { | ||
if (Math.floor(i / item_per_page) === num) { | ||
arr[i].classList.remove('hexo-douban-hide'); | ||
} else { | ||
arr[i].classList.add('hexo-douban-hide'); | ||
} | ||
} | ||
} | ||
function initPagination() { | ||
let firstpages = document.getElementsByClassName("hexo-douban-firstpage"); | ||
let previouspages = document.getElementsByClassName("hexo-douban-previouspage"); | ||
let nextpages = document.getElementsByClassName("hexo-douban-nextpage"); | ||
let lastpages = document.getElementsByClassName("hexo-douban-lastpage"); | ||
let pagenums = document.getElementsByClassName("hexo-douban-pagenum"); | ||
for (let i = 0; i < firstpages.length; i++) { | ||
//add listener | ||
firstpages[i].onclick = firstBtn; | ||
previouspages[i].onclick = previousBtn; | ||
nextpages[i].onclick = nextBtn; | ||
lastpages[i].onclick = lastBtn; | ||
//set page num | ||
let size = pagenums[i].parentNode.siblings().length; | ||
pagenums[i].innerText = '1 / ' + (Math.ceil(size / item_per_page) === 0 ? 1 : Math.ceil(size / item_per_page)); | ||
firstpages[i].click(); | ||
} | ||
} | ||
initPagination(); | ||
</script> |
Oops, something went wrong.