@@ -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 }
0 commit comments