Created
March 22, 2013 07:27
-
-
Save stephenfrank/5219574 to your computer and use it in GitHub Desktop.
horizontal-step-box
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $(function () { | |
| if ($('#stepBox').length === 0) { | |
| return; | |
| } | |
| var stepBox = $('#stepBox'); | |
| var stepBoxWidth = stepBox.width(); | |
| stepBox.find('.step-item').width(stepBoxWidth); | |
| var totalWidth = (function () { | |
| var totalWidth = 0; | |
| stepBox.find('.step-item').each(function () { | |
| totalWidth += $(this).width(); | |
| }); | |
| return totalWidth; | |
| }()); | |
| $('#stepBoxInner').width(totalWidth); | |
| var resolveHeight = function () { | |
| var activeBox = stepBox.find('.step-item.active'); | |
| stepBox.animate({height: activeBox.height()}); | |
| }; | |
| setInterval(function () { | |
| resolveHeight(); | |
| }, 1000); | |
| var goToItem = function (index) { | |
| var stepItem = stepBox.find('.step-item[data-index="' + index + '"]'); | |
| var leftOffset = stepItem.offset().left; | |
| $('.step-item').removeClass('active'); | |
| stepItem.addClass('active'); | |
| $('#stepBoxInner').animate({marginLeft: -1 * stepBoxWidth * index}); | |
| $('html,body').animate({scrollTop: 0}); | |
| resolveHeight(); | |
| }; | |
| $('body').on('click', '[data-toggle="next-step"], [data-toggle="prev-step"]', function (e) { | |
| e.preventDefault(); | |
| console.log('[data-toggle="next-step"]'); | |
| var increment = $(this).data('toggle') == 'next-step' ? 1 : -1; | |
| var currentStep = stepBox.find('.step-item.active').data('index'); | |
| var nextStep = currentStep + increment; | |
| console.log('goToItem: ' + nextStep); | |
| goToItem(nextStep); | |
| }); | |
| }); |
stephenfrank
commented
Mar 22, 2013
Author
Author
#stepBox {
position: relative;
overflow: hidden;
.clearfix();
.step-item {
float: left;
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment