Skip to content

Commit

Permalink
分页js使用局部变量,防止污染全局变量
Browse files Browse the repository at this point in the history
  • Loading branch information
HCLonely committed Jan 3, 2022
1 parent 11e2e4c commit 48e5dc0
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 93 deletions.
2 changes: 1 addition & 1 deletion lib/templates/bangumi.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
</div>
<script>
<%- include('index.js') %>
tabs[<%- show %>].click();
document.getElementsByClassName('bangumi-tab')[<%- show %>].click();
<%- include('pagination.js', {__: __}) %>
</script>
4 changes: 2 additions & 2 deletions lib/templates/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions lib/templates/pagination.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hexo-bilibili-bangumi",
"version": "1.6.2",
"version": "1.6.4",
"description": "hexo bilibili番剧页",
"main": "index.js",
"scripts": {
Expand Down
60 changes: 31 additions & 29 deletions src/lib/templates/index.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
Element.prototype.siblings = function () {
const siblingElement = []
const sibs = this.parentNode.children
for (let i = 0; i < sibs.length; i++) {
if (sibs[i] !== this) {
siblingElement.push(sibs[i])
(function () {
Element.prototype.siblings = function () {
const siblingElement = []
const sibs = this.parentNode.children
for (let i = 0; i < sibs.length; i++) {
if (sibs[i] !== this) {
siblingElement.push(sibs[i])
}
}
return siblingElement
}
return siblingElement
}
function tabClick () {
// 修改标签样式
this.classList.add('bangumi-active')
let sibs = this.siblings()
for (let j = 0; j < sibs.length; j++) {
sibs[j].classList.remove('bangumi-active')
function tabClick() {
// 修改标签样式
this.classList.add('bangumi-active')
let sibs = this.siblings()
for (let j = 0; j < sibs.length; j++) {
sibs[j].classList.remove('bangumi-active')
}
// 显示对应板块
const itemId = this.id.replace('tab', 'item')
const target = document.getElementById(itemId)
target.classList.remove('bangumi-hide')
target.classList.add('bangumi-show')
sibs = document.getElementById(itemId).siblings()
for (let k = 0; k < sibs.length; k++) {
sibs[k].classList.remove('bangumi-show')
sibs[k].classList.add('bangumi-hide')
}
}
// 显示对应板块
const itemId = this.id.replace('tab', 'item')
const target = document.getElementById(itemId)
target.classList.remove('bangumi-hide')
target.classList.add('bangumi-show')
sibs = document.getElementById(itemId).siblings()
for (let k = 0; k < sibs.length; k++) {
sibs[k].classList.remove('bangumi-show')
sibs[k].classList.add('bangumi-hide')
const tabs = document.getElementsByClassName('bangumi-tab')
for (let i = 0; i < tabs.length; i++) {
tabs[i].onclick = tabClick
tabs[i].onclick.apply(tabs[i])
}
}
const tabs = document.getElementsByClassName('bangumi-tab')
for (let i = 0; i < tabs.length; i++) {
tabs[i].onclick = tabClick
tabs[i].onclick.apply(tabs[i])
}
})()
118 changes: 60 additions & 58 deletions src/lib/templates/pagination.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,72 @@
var firstpages = document.getElementsByClassName('bangumi-firstpage')
var previouspages = document.getElementsByClassName('bangumi-previouspage')
var nextpages = document.getElementsByClassName('bangumi-nextpage')
var lastpages = document.getElementsByClassName('bangumi-lastpage')
var pagenums = document.getElementsByClassName('bangumi-pagenum')
(function () {
const firstpages = document.getElementsByClassName('bangumi-firstpage')
const previouspages = document.getElementsByClassName('bangumi-previouspage')
const nextpages = document.getElementsByClassName('bangumi-nextpage')
const lastpages = document.getElementsByClassName('bangumi-lastpage')
const pagenums = document.getElementsByClassName('bangumi-pagenum')

function makePageNum (num, arr) {
return (num + 1) + ' / ' + (Math.ceil(arr.length / 10 === 0 ? 1 : Math.ceil(arr.length / 10)))
}
function makePageNum(num, arr) {
return (num + 1) + ' / ' + (Math.ceil(arr.length / 10 === 0 ? 1 : Math.ceil(arr.length / 10)))
}

function firstBtn () {
var sibs = this.parentNode.siblings()
displayPage(sibs, 0)
this.parentNode.getElementsByClassName('bangumi-pagenum')[0].innerText = makePageNum(0, sibs)
}
function firstBtn() {
const sibs = this.parentNode.siblings()
displayPage(sibs, 0)
this.parentNode.getElementsByClassName('bangumi-pagenum')[0].innerText = makePageNum(0, sibs)
}

function previousBtn () {
var sibs = this.parentNode.siblings()
var currNum = this.parentNode.getElementsByClassName('bangumi-pagenum')[0].innerText
currNum = currNum.substr(0, currNum.indexOf('/') - 1)
currNum = parseInt(currNum, 10) - 1
if (currNum > 0) {
currNum--
function previousBtn() {
const sibs = this.parentNode.siblings()
let currNum = this.parentNode.getElementsByClassName('bangumi-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('bangumi-pagenum')[0].innerText = makePageNum(currNum, sibs)
}
displayPage(sibs, currNum)
this.parentNode.getElementsByClassName('bangumi-pagenum')[0].innerText = makePageNum(currNum, sibs)
}

function nextBtn () {
var sibs = this.parentNode.siblings()
var currNum = this.parentNode.getElementsByClassName('bangumi-pagenum')[0].innerText
currNum = currNum.substr(0, currNum.indexOf('/') - 1)
currNum = parseInt(currNum, 10) - 1
if (currNum < Math.ceil(sibs.length / 10) - 1) {
currNum++
function nextBtn() {
const sibs = this.parentNode.siblings()
let currNum = this.parentNode.getElementsByClassName('bangumi-pagenum')[0].innerText
currNum = currNum.substr(0, currNum.indexOf('/') - 1)
currNum = parseInt(currNum, 10) - 1
if (currNum < Math.ceil(sibs.length / 10) - 1) {
currNum++
}
displayPage(sibs, currNum)
this.parentNode.getElementsByClassName('bangumi-pagenum')[0].innerText = makePageNum(currNum, sibs)
}
displayPage(sibs, currNum)
this.parentNode.getElementsByClassName('bangumi-pagenum')[0].innerText = makePageNum(currNum, sibs)
}

function lastBtn () {
var sibs = this.parentNode.siblings()
displayPage(sibs, Math.ceil(sibs.length / 10) - 1)
this.parentNode.getElementsByClassName('bangumi-pagenum')[0].innerText = makePageNum(Math.ceil(sibs.length / 10) - 1 === -1 ? 0 : Math.ceil(sibs.length / 10) - 1, sibs)
}
function lastBtn() {
const sibs = this.parentNode.siblings()
displayPage(sibs, Math.ceil(sibs.length / 10) - 1)
this.parentNode.getElementsByClassName('bangumi-pagenum')[0].innerText = makePageNum(Math.ceil(sibs.length / 10) - 1 === -1 ? 0 : Math.ceil(sibs.length / 10) - 1, sibs)
}

function displayPage (arr, num) {
for (var i = 0; i < arr.length; i++) {
if (Math.floor(i / 10) === num) {
arr[i].classList.remove('bangumi-hide')
var img = arr[i].getElementsByTagName('img')[0]
img.src = img.getAttribute('data-src')
} else {
arr[i].classList.add('bangumi-hide')
function displayPage(arr, num) {
for (let i = 0; i < arr.length; i++) {
if (Math.floor(i / 10) === num) {
arr[i].classList.remove('bangumi-hide')
const img = arr[i].getElementsByTagName('img')[0]
img.src = img.getAttribute('data-src')
} else {
arr[i].classList.add('bangumi-hide')
}
}
}
}

for (var i = 0; i < firstpages.length; i++) {
// add listener
firstpages[i].onclick = firstBtn
previouspages[i].onclick = previousBtn
nextpages[i].onclick = nextBtn
lastpages[i].onclick = lastBtn
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
var size = pagenums[i].parentNode.siblings().length
pagenums[i].innerText = '1 / ' + (Math.ceil(size / 10) === 0 ? 1 : Math.ceil(size / 10))
firstpages[i].click()
}
// set page num
const size = pagenums[i].parentNode.siblings().length
pagenums[i].innerText = '1 / ' + (Math.ceil(size / 10) === 0 ? 1 : Math.ceil(size / 10))
firstpages[i].click()
}
})()

0 comments on commit 48e5dc0

Please sign in to comment.