看到一個小說站是這樣防UC、夸克轉碼的,他的下一章鏈接是空的“<a href="">下一章</a>”。然后利用js的滾動事件修改 href 屬性。
js代碼如下:
在html頁面宣告一下幾個變數。
var shortid = "xx", articleid = "xx", next_chapterid = "xx";//xx填寫你自己的ID
//下一章的url判斷
if (next_chapterid !== "") {//判斷是否有下一章
var person = {
tid: shortid,
eid: articleid,
nextid: next_chapterid,
Namepageid: function() {
return "/" + this.tid + "/" + this.eid + "/" + this.nextid + ".html"
}
}
} else {//沒有直接回目錄
var person = {
tid: shortid,
eid: articleid,
Namepageid: function() {
return "/" + this.tid + "/" + this.eid + "/"
}
}
}
//下一章的url判斷end
//滾動事件修改 href 屬性
var url = person.Namepageid();
if (window.screen.availHeight >= document.body.clientHeight) {//沒有滾動條直接修改href 屬性
document.getElementById("next").setAttribute("href", ""+ url)
} else {
var tur = true;
function scrollBottomOrTop() {
var clients = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
var wholeHeight = document.documentElement.scrollHeight;
if (clients + scrollTop >= wholeHeight - 500) {//滾動條距離底部500px時執行修改href 屬性
document.getElementById("next").setAttribute("href", ""+ url)
}
if (scrollTop >= 300) {//文章內容向上滾動300px時修改href 屬性
document.getElementById("next").setAttribute("href", ""+ url)
}
tur = true
}
window.onscroll = function() {//500毫秒只執行一次
if (tur) {
setTimeout(scrollBottomOrTop, 500);
tur = false
} else {}
}
}
//滾動事件修改 href 屬性end
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/34627.html
標籤:JavaScript
上一篇:vue模板使用問題
下一篇:幾道misc題
