docsify-quick-page.js 1.1 KB
Newer Older
W
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
document.addEventListener('DOMContentLoaded', function() {	
	var prevBtn = document.createElement("div")
	prevBtn.id = "prev-page-button"
	document.body.appendChild(prevBtn)
	var nextBtn = document.createElement("div");
	nextBtn.id = "next-page-button"
    document.body.appendChild(nextBtn)

    var links = null
	var linkMap = null
	var getCurIdx = function() {
		if (!links) {
			links = Array
				.from(document.querySelectorAll(".sidebar-nav a"))
				.map(x => x.href)
			linkMap = {}
			links.forEach((x, i) => linkMap[x] = i)
		}
		
		var elem = document.querySelector(".active a")
		var curIdx = elem? linkMap[elem.href]: -1
		return curIdx
	}

	prevBtn.addEventListener('click', function () {
		if (!document.body.classList.contains('ready'))
			return
		var curIdx = getCurIdx()
		location.href = curIdx == -1? 
			links[0]: 
			links[(curIdx - 1 + links.length) % links.length]
		document.body.scrollIntoView()
	}, false)
	
	nextBtn.addEventListener('click', function () {
		if (!document.body.classList.contains('ready'))
			return
		var curIdx = getCurIdx()
		location.href = links[(curIdx + 1) % links.length]
		document.body.scrollIntoView()
	}, false)
})