|
|
@@ -95,3 +95,52 @@
|
|
|
if (mobileQuery.addEventListener) mobileQuery.addEventListener('change', handleViewportChange);
|
|
|
else if (mobileQuery.addListener) mobileQuery.addListener(handleViewportChange);
|
|
|
})();
|
|
|
+
|
|
|
+(function () {
|
|
|
+ 'use strict';
|
|
|
+
|
|
|
+ var tabBars = Array.prototype.slice.call(document.querySelectorAll('.tabbox .tabbar'));
|
|
|
+
|
|
|
+ function updateTabBarState(bar) {
|
|
|
+ var maxScroll = Math.max(0, bar.scrollWidth - bar.clientWidth);
|
|
|
+ var scrollable = maxScroll > 1;
|
|
|
+ bar.classList.toggle('is-scrollable', scrollable);
|
|
|
+ bar.classList.toggle('is-at-start', !scrollable || bar.scrollLeft <= 1);
|
|
|
+ bar.classList.toggle('is-at-end', !scrollable || bar.scrollLeft >= maxScroll - 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ function centerActiveTab(bar, button) {
|
|
|
+ if (!bar || !button || bar.scrollWidth <= bar.clientWidth) return;
|
|
|
+ var barRect = bar.getBoundingClientRect();
|
|
|
+ var buttonRect = button.getBoundingClientRect();
|
|
|
+ var target = bar.scrollLeft + buttonRect.left - barRect.left - (barRect.width - buttonRect.width) / 2;
|
|
|
+ target = Math.max(0, Math.min(target, bar.scrollWidth - bar.clientWidth));
|
|
|
+ if (bar.scrollTo) bar.scrollTo({ left: target, behavior: 'smooth' });
|
|
|
+ else bar.scrollLeft = target;
|
|
|
+ }
|
|
|
+
|
|
|
+ tabBars.forEach(function (bar) {
|
|
|
+ updateTabBarState(bar);
|
|
|
+ bar.addEventListener('scroll', function () { updateTabBarState(bar); }, { passive: true });
|
|
|
+ Array.prototype.forEach.call(bar.querySelectorAll('button'), function (button) {
|
|
|
+ button.addEventListener('click', function () {
|
|
|
+ window.requestAnimationFrame(function () {
|
|
|
+ centerActiveTab(bar, button);
|
|
|
+ updateTabBarState(bar);
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ function refreshTabBars() {
|
|
|
+ tabBars.forEach(updateTabBarState);
|
|
|
+ }
|
|
|
+
|
|
|
+ var resizeTimer = 0;
|
|
|
+ window.addEventListener('resize', function () {
|
|
|
+ window.clearTimeout(resizeTimer);
|
|
|
+ resizeTimer = window.setTimeout(refreshTabBars, 120);
|
|
|
+ });
|
|
|
+ window.requestAnimationFrame(refreshTabBars);
|
|
|
+ window.addEventListener('load', refreshTabBars);
|
|
|
+})();
|