Gadget:backToTop.js:修订间差异
(// Edit via Wikiplus) |
小 (Litwak913移动页面用户:坑触可/js/backToTop.js至Gadget:backToTop.js,不留重定向) |
||
| (未显示同一用户的1个中间版本) | |||
| 第7行: | 第7行: | ||
var innerWidth = window.innerWidth; | var innerWidth = window.innerWidth; | ||
var scrollbarWidth; | var scrollbarWidth; | ||
if($(".backToTop").length > 0) { // 已经加载了 OOUI 版本 | |||
return; | |||
} | |||
switch ("scroll") { | switch ("scroll") { | ||
case getComputedStyle(body).overflowY: | case getComputedStyle(body).overflowY: | ||
2023年7月30日 (日) 16:28的最新版本
/* 引自萌百,有修改。*/
// <pre>
/* 回到顶部 */
$(function() {
var body = document.body,
html = document.documentElement;
var innerWidth = window.innerWidth;
var scrollbarWidth;
if($(".backToTop").length > 0) { // 已经加载了 OOUI 版本
return;
}
switch ("scroll") {
case getComputedStyle(body).overflowY:
scrollbarWidth = innerWidth - body.clientWidth;
break;
case getComputedStyle(html).overflowY:
scrollbarWidth = innerWidth - html.clientWidth;
break;
default:
var backup = body.style.overflowY;
body.style.overflowY = "scroll";
scrollbarWidth = innerWidth - body.clientWidth;
body.style.overflowY = backup;
}
var btn = $("<div/>", {
"text": "",
"attr": {
"title": "返回顶部",
"class": "backToTop"
},
"css": {
"user-select": "none"
},
"on": {
"click": function() {
$("html, body").animate({
scrollTop: 0
}, 120);
}
}
}).appendTo(document.body);
if (scrollbarWidth === 0) {
btn.css("right", "20px"); // 修复新版 Chrome 的自动隐藏式滚动条导致的按钮被覆盖 chrome://flags/#overlay-scrollbars
}
$(window).on("scroll", function() {
$(document).scrollTop() > 0 ? btn.fadeIn() : btn.fadeOut();
}).scroll();
});
// </pre>