Skip to content

Commit 983c0fc

Browse files
authored
Merge pull request #60 from mikejacobson/scroll-active-to-center
Scroll active tab to center rather than minimally within view
2 parents 2f1e6f7 + 5bc322d commit 983c0fc

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jquery-bootstrap-scrolling-tabs",
3-
"version": "2.4.0",
3+
"version": "2.5.0",
44
"main": [
55
"./dist/jquery.scrolling-tabs.js",
66
"./dist/jquery.scrolling-tabs.css"

dist/jquery.scrolling-tabs.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,9 @@
934934
activeTabLeftPos,
935935
activeTabRightPos,
936936
rightArrowLeftPos,
937+
activeTabWidth,
938+
leftPosOffset,
939+
offsetToMiddle,
937940
leftScrollArrowWidth,
938941
rightScrollArrowWidth;
939942

@@ -955,13 +958,14 @@
955958
}
956959

957960
rightScrollArrowWidth = stc.$slideRightArrow.outerWidth();
961+
activeTabWidth = $activeTab.outerWidth();
958962

959963
/**
960964
* @author poletaew
961965
* We need relative offset (depends on $fixedContainer), don't absolute
962966
*/
963967
activeTabLeftPos = $activeTab.offset().left - stc.$fixedContainer.offset().left;
964-
activeTabRightPos = activeTabLeftPos + $activeTab.outerWidth();
968+
activeTabRightPos = activeTabLeftPos + activeTabWidth;
965969

966970
rightArrowLeftPos = stc.fixedContainerWidth - rightScrollArrowWidth;
967971

@@ -981,13 +985,17 @@
981985
}
982986
} else {
983987
if (activeTabRightPos > rightArrowLeftPos) { // active tab off right side
984-
stc.movableContainerLeftPos -= (activeTabRightPos - rightArrowLeftPos + rightScrollArrowWidth);
988+
leftPosOffset = activeTabRightPos - rightArrowLeftPos + rightScrollArrowWidth;
989+
offsetToMiddle = stc.fixedContainerWidth / 2;
990+
leftPosOffset += offsetToMiddle - (activeTabWidth / 2);
991+
stc.movableContainerLeftPos -= leftPosOffset;
985992
smv.slideMovableContainerToLeftPos();
986993
return true;
987994
} else {
988995
leftScrollArrowWidth = stc.$slideLeftArrow.outerWidth();
989-
if (activeTabLeftPos < leftScrollArrowWidth) { // active tab off left side
990-
stc.movableContainerLeftPos += leftScrollArrowWidth - activeTabLeftPos;
996+
if (activeTabLeftPos < 0) { // active tab off left side
997+
offsetToMiddle = stc.fixedContainerWidth / 2;
998+
stc.movableContainerLeftPos += (-activeTabLeftPos) + offsetToMiddle - (activeTabWidth / 2);
991999
smv.slideMovableContainerToLeftPos();
9921000
return true;
9931001
}

0 commit comments

Comments
 (0)