Skip to content

Commit 2af133b

Browse files
committed
fix: increase scroll speed
1 parent 1f88fcb commit 2af133b

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

src/extensionsIntegrated/TabBar/overflow.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,10 @@ define(function (require, exports, module) {
241241

242242

243243
/**
244-
* Scrolls the tab bar to the active tab
244+
* Scrolls the tab bar to the active tab.
245+
* When scrolling the tab bar, we calculate the distance we need to scroll and based on that distance,
246+
* we set the duration.
247+
* This ensures that the scrolling speed is consistent no matter the distance or number of tabs present in tab bar.
245248
*
246249
* @param {JQuery} $tabBarElement - The tab bar element,
247250
* this is either $('#phoenix-tab-bar') or $('phoenix-tab-bar-2')
@@ -263,7 +266,8 @@ define(function (require, exports, module) {
263266

264267
if ($activeTab.length) {
265268
// get the tab bar container's dimensions
266-
const tabBarRect = $tabBarElement[0].getBoundingClientRect();
269+
const tabBar = $tabBarElement[0];
270+
const tabBarRect = tabBar.getBoundingClientRect();
267271
const tabBarVisibleWidth = tabBarRect.width;
268272

269273
// get the active tab's dimensions
@@ -273,23 +277,36 @@ define(function (require, exports, module) {
273277
const tabLeftRelative = tabRect.left - tabBarRect.left;
274278
const tabRightRelative = tabRect.right - tabBarRect.left;
275279

276-
// get the current scroll position
277-
const currentScroll = $tabBarElement.scrollLeft();
280+
// get current scroll position
281+
const currentScroll = tabBar.scrollLeft;
282+
let targetScroll = currentScroll;
283+
let scrollDistance = 0;
278284

279-
// animate the scroll change over 5 for a very fast effect
285+
// calculate needed scroll adjustment
280286
if (tabLeftRelative < 0) {
281-
// Tab is too far to the left
282-
$tabBarElement.animate(
283-
{ scrollLeft: currentScroll + tabLeftRelative - 10 },
284-
5
285-
);
287+
targetScroll = currentScroll + tabLeftRelative - 10;
286288
} else if (tabRightRelative > tabBarVisibleWidth) {
287-
// Tab is too far to the right
288289
const scrollAdjustment = tabRightRelative - tabBarVisibleWidth + 10;
289-
$tabBarElement.animate(
290-
{ scrollLeft: currentScroll + scrollAdjustment },
291-
5
290+
targetScroll = currentScroll + scrollAdjustment;
291+
}
292+
293+
// calculate the scroll distance in pixels
294+
scrollDistance = Math.abs(targetScroll - currentScroll);
295+
296+
// calculate duration based on distance (0.15ms per pixel + 100ms base)
297+
// min 100ms, max 400ms
298+
let duration = Math.min(Math.max(scrollDistance * 0.15, 100), 400);
299+
300+
// only animate if we need to move more than 5 pixels
301+
// otherwise, we can just jump
302+
if (scrollDistance > 5) {
303+
$tabBarElement.stop(true).animate(
304+
{ scrollLeft: targetScroll },
305+
duration,
306+
'linear'
292307
);
308+
} else {
309+
tabBar.scrollLeft = targetScroll;
293310
}
294311
}
295312
}

src/styles/Extn-TabBar.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
overflow-y: hidden;
2222
white-space: nowrap;
2323
transition: height 0.3s ease;
24-
scroll-behavior: smooth;
24+
scroll-behavior: auto;
2525
background-color: #f5f5f5;
2626
}
2727

0 commit comments

Comments
 (0)