Skip to content
This repository has been archived by the owner on Feb 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #187 from BaigeiGu/master
Browse files Browse the repository at this point in the history
update Scroller Gadget
  • Loading branch information
ZoruaFox authored Feb 17, 2024
2 parents b34be36 + 93f181f commit ee22656
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 140 deletions.
2 changes: 1 addition & 1 deletion src/Gadgets/Compatibility/Scroller/.options
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Scroller[ResourceLoader|default|hidden]|Scroller.css|Scroller.js
Scroller[ResourceLoader|default|hidden]|Scroller.js
78 changes: 0 additions & 78 deletions src/Gadgets/Compatibility/Scroller/Scroller.css

This file was deleted.

87 changes: 26 additions & 61 deletions src/Gadgets/Compatibility/Scroller/Scroller.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* SPDX-License-Identifier: CC-BY-SA-4.0
* _addText: '{{Gadget Header|license=CC-BY-SA-4.0|import=no}}'
* @author 白给
* * SPDX-License-Identifier: MIT
* _addText: '{{Gadget Header|license=MIT}}'
*
* @source <https://zh.moegirl.org.cn/MediaWiki:Gadget-jQueryLazyload.js>
*/
/**
* +--------------------------------------------------------+
Expand All @@ -17,62 +18,26 @@
/* <nowiki> */
"use strict";

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var Scroller = function () {
function Scroller() {
var _this = this;

_classCallCheck(this, Scroller);

this.scroller_focus_id = 0;
this.scroller_items = document.getElementsByClassName('scroller-item');
this.scroller_container = document.getElementsByClassName('scroller-container');
this.scroller_item_width = this.scroller_items[0].offsetWidth;
this.scroller_item_length = this.scroller_items.length;

if (this.scroller_container.length == 0) {return 0}

this.e_last_btn = document.getElementById('scroller-last-btn');
this.e_next_Btn = document.getElementById('scroller-next-btn');

this.scroller_items[0].classList.add('scroller-item-now');

this.e_last_btn.addEventListener('click', function () {
_this.scroll_element_intoView(-1);
});
this.e_next_Btn.addEventListener('click', function () {
_this.scroll_element_intoView(1);
});
function Scroller(element){
function wheelHandler(e) {
if (e.deltaY && !e.deltaX) {
e.preventDefault()
requestAnimationFrame(() => {
element.scrollBy(e.deltaY, 0)
})
}

_createClass(Scroller, [{
key: 'scroll_element_intoView',
value: function scroll_element_intoView(jump) {
var target = this.scroller_focus_id + jump;

if (target <= 0) {
target = 0;
}
if (target >= this.scroller_item_length) {
target = this.scroller_item_length - 1;
}

this.scroller_container[0].scrollTo(this.scroller_item_width * target, 0);

try {
this.scroller_items[this.scroller_focus_id].classList.remove('scroller-item-now');
} finally {}
this.scroller_items[target].classList.add('scroller-item-now');

this.scroller_focus_id = target;
}
}]);

return Scroller;
}();

new Scroller();
/* </nowiki> */
}

element.addEventListener('wheel', wheelHandler)
element.style.scrollSnapType = 'none'
return () => {
element.removeEventListener('wheel', wheelHandler)
element.style.scrollSnapType = ''
}
}

const scroller_containers = document.getElementsByClassName('scroller-container')
for (let index = 0; index < scroller_containers.length; index++) {
const element = scroller_containers[index];
Scroller(element)
}

0 comments on commit ee22656

Please sign in to comment.