Gadget:oouiup.js

$( function () {
	const body = document.body,
		html = document.documentElement;
	const innerWidth = window.innerWidth;
	var scrollbarWidth;
	if($(".backToTop").length > 0) { // 已经加载了仿论坛版本
		return;
	}
	switch ( 'scroll' ) {
		case getComputedStyle( body ).overflowY:
			scrollbarWidth = innerWidth - body.clientWidth;
			break;
		case getComputedStyle( html ).overflowY:
			scrollbarWidth = innerWidth - html.clientWidth;
			break;
		default:
			const backup = body.style.overflowY;
			body.style.overflowY = 'scroll';
			scrollbarWidth = innerWidth - body.clientWidth;
			body.style.overflowY = backup;
	}
	const btnObj = new OO.ui.ButtonWidget( {
		icon: 'collapse',
		label: '返回顶部',
		title: '返回顶部',
		invisibleLabel: true,
		classes: [ 'backToTop' ]
	} );
	btnObj.on( 'click', function () {
		$( 'html, body' ).animate( {
			scrollTop: 0
		}, 120 );
	}
	);
	const $btn = btnObj.$element;
	$btn.appendTo( 'body' );
	if ( scrollbarWidth === 0 ) {
		$btn.css( 'right', '20px' );
	}
	window.onscroll = function () {
		if ( document.scrollingElement.scrollTop > 0 ) {
			$btn.fadeIn();
		} else {
			$btn.fadeOut();
		}
	};
	window.onscroll();
} );