跳转到内容

MediaWiki:Gadget-ScrollUpButton.js:修订间差异

勤求古训,博采众方
删除的内容 添加的内容
修复: 对象展开语法兼容性(改用$.extend) + 图标路径改为动态获取(wgServer)
标签已被回退
回退到 rev 137(原始版本)
标签撤销
第15行: 第15行:
}
}


var baseCss = {
function makeBtn(id, onClick, extraCss) {
display: 'none',
var css = $.extend({
display: 'none',
position: 'fixed',
position: 'fixed',
right: '18px',
right: '18px',
cursor: 'pointer',
cursor: 'pointer',
opacity: '0.7',
opacity: '0.7',
width: '40px',
width: '40px',
zIndex: 9999,
zIndex: 9999,
'-moz-user-select': 'none',
'-moz-user-select': 'none',
'-webkit-user-select': 'none',
'-webkit-user-select': 'none',
'user-select': 'none',
transition: 'opacity 0.2s'
'user-select': 'none',
};
transition: 'opacity 0.2s'
}, extraCss);


function makeBtn(id, onClick, extraCss) {
var css = $.extend({}, baseCss, extraCss);
return $('<img>')
return $('<img>')
.addClass('noprint')
.addClass('noprint')
第65行: 第66行:


if (isVector2022) {
if (isVector2022) {
try { mql.addEventListener('change', onWidthChange); }
try {
mql.addEventListener('change', onWidthChange);
catch (_) { mql.addListener(onWidthChange); }
} catch (_) {
mql.addListener(onWidthChange);
}
onWidthChange(mql);
onWidthChange(mql);
}
}
第75行: 第73行:
function onScroll() {
function onScroll() {
var y = $window.scrollTop() || 0;
var y = $window.scrollTop() || 0;

if (y > 200 && !visible) {
if (y > 200 && !visible) {
visible = true;
visible = true;
第83行: 第80行:
$elements.fadeOut(300);
$elements.fadeOut(300);
}
}

if (
if (
(mw.config.get('wgGEHelpPanelEnabled') && $('#mw-ge-help-panel-cta-button').length) ||
(mw.config.get('wgGEHelpPanelEnabled') && $('#mw-ge-help-panel-cta-button').length) ||
$('#cat_a_lot').length ||
$('#cat_a_lot').length || $('#proveit').length || $('.wordcount').length
$('#proveit').length ||
$('.wordcount').length
) {
) {
setLeft(10);
setLeft(10);
第99行: 第93行:
$window.on('scroll', function () {
$window.on('scroll', function () {
if (!ticking) {
if (!ticking) {
requestAnimationFrame(function () {
requestAnimationFrame(function () { onScroll(); ticking = false; });
onScroll();
ticking = false;
});
ticking = true;
ticking = true;
}
}
});
});

onScroll();
onScroll();
});
});

2026年6月12日 (五) 20:40的版本

/**
 * Copyright (c) 2021-present, 安忆.
 *
 * @author 安忆 [[zh:U:安忆]]
 * @file scrollUpButton.js
 * @license GPL v3
 */
$(function scrollUpButton() {
  var $window = $(window);
  var icon = mw.config.get('wgServer') + '/w/images/5/59/Font_Awesome_5_regular_arrow-circle-up_blue.svg';
  var visible = false;

  function scrollTo(h) {
    $('html, body').stop(true, true).animate({ scrollTop: h }, 500);
  }

  var baseCss = {
    display: 'none',
    position: 'fixed',
    right: '18px',
    cursor: 'pointer',
    opacity: '0.7',
    width: '40px',
    zIndex: 9999,
    '-moz-user-select': 'none',
    '-webkit-user-select': 'none',
    'user-select': 'none',
    transition: 'opacity 0.2s'
  };

  function makeBtn(id, onClick, extraCss) {
    var css = $.extend({}, baseCss, extraCss);
    return $('<img>')
      .addClass('noprint')
      .attr({ id: id, src: icon, draggable: 'false' })
      .css(css)
      .on('mouseenter', function () { this.style.opacity = '1'; })
      .on('mouseleave', function () { this.style.opacity = '0.7'; })
      .on('click', onClick)
      .appendTo(document.body);
  }

  var $up = makeBtn('scrollUpBtn', function () { scrollTo(0); }, { bottom: '85px' });
  var $down = makeBtn('scrollDownBtn', function () {
    scrollTo(($(document).height() || 0) - ($window.height() || 0));
  }, { bottom: '45px', transform: 'rotate(180deg)' });

  var $elements = $up.add($down);

  function setLeft(px) {
    $elements.css('left', px ? px + 'px' : 'unset');
  }

  function setRight(px) {
    $elements.css('right', px ? px + 'px' : 'unset');
  }

  var isVector2022 = mw.config.get('skin') === 'vector-2022';
  var mql = window.matchMedia('(min-width: 1400px)');

  function onWidthChange(e) {
    if (isVector2022) {
      setRight(e.matches ? 8 : 18);
    }
  }

  if (isVector2022) {
    try { mql.addEventListener('change', onWidthChange); }
    catch (_) { mql.addListener(onWidthChange); }
    onWidthChange(mql);
  }

  function onScroll() {
    var y = $window.scrollTop() || 0;
    if (y > 200 && !visible) {
      visible = true;
      $elements.fadeIn(300);
    } else if (y <= 200 && visible) {
      visible = false;
      $elements.fadeOut(300);
    }
    if (
      (mw.config.get('wgGEHelpPanelEnabled') && $('#mw-ge-help-panel-cta-button').length) ||
      $('#cat_a_lot').length || $('#proveit').length || $('.wordcount').length
    ) {
      setLeft(10);
    } else {
      setLeft();
    }
  }

  var ticking = false;
  $window.on('scroll', function () {
    if (!ticking) {
      requestAnimationFrame(function () { onScroll(); ticking = false; });
      ticking = true;
    }
  });
  onScroll();
});