Skip to content

Commit

Permalink
Reverse animation will now respect watched elements
Browse files Browse the repository at this point in the history
  • Loading branch information
IanLunn committed Aug 3, 2015
1 parent 220af90 commit fef32cc
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 17 deletions.
28 changes: 24 additions & 4 deletions scripts/sequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,8 @@ function defineSequence(imagesLoaded, Hammer) {
* @param {Boolean} ignorePhaseThreshold - if true, ignore the threshold
* between phases (breaks reversal of animations but may be used when
* skipping navigation etc)
* @returns {Number} maxWatchedTotal - The new total length
* (duration + delay) for watched elements when reversed
*/
reverseProperties: function(phaseProperties, phaseDelay, phaseThresholdTime, ignorePhaseThreshold) {

Expand All @@ -1453,8 +1455,11 @@ function defineSequence(imagesLoaded, Hammer) {
timingFunctionReversed,
duration,
delay,
total,
maxTotal,
totals = [];
maxWatchedTotal,
totals = [],
watchedTotals = [];

for (i = 0; i < noOfPhaseElements; i++) {
el = phaseElements[i];
Expand All @@ -1463,19 +1468,30 @@ function defineSequence(imagesLoaded, Hammer) {
duration = convertTimeToMs(getStyle(el, Modernizr.prefixed("transitionDuration")));
delay = convertTimeToMs(getStyle(el, Modernizr.prefixed("transitionDelay")));

// Save the total
total = duration + delay;

// Delay elements in relation to the length of other elements in the
// phase eg. If one element A transitions for 1s and element B 2s
// element A should be delayed by 1s (element B length - element A length).
delay = stepDurations.maxTotal - (duration + delay);
delay = stepDurations.maxTotal - total;

// Delay this phase's elements so they animate in relation to the
// other phase
if (ignorePhaseThreshold !== true) {
delay += phaseDelay;
}

// Update the total with the new delay
total = duration + delay;

// Save the total of the reversed animation
totals.push(delay + duration);
totals.push(total);

// Save the total of the reversed animation for watched elements only
if (el.getAttribute("data-seq") !== null) {
watchedTotals.push(total);
}

// Get the timing-function and reverse it
timingFunction = getStyle(el, Modernizr.prefixed("transitionTimingFunction"));
Expand All @@ -1488,6 +1504,10 @@ function defineSequence(imagesLoaded, Hammer) {
// Get the longest total and delay of the reversed animations
maxTotal = Math.max.apply(Math, totals);

// Get the longest total and delay of the reversed animations (for
// watched elements only)
maxWatchedTotal = Math.max.apply(Math, watchedTotals);

// Remove the reversed transition properties from each element once it
// has finished animating; allowing for the inherited styles to take
// effect again.
Expand All @@ -1501,7 +1521,7 @@ function defineSequence(imagesLoaded, Hammer) {
});
}, maxTotal + phaseThresholdTime);

return maxTotal;
return maxWatchedTotal;
},

/**
Expand Down
2 changes: 1 addition & 1 deletion scripts/sequence.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion scripts/sequence.min.map

Large diffs are not rendered by default.

28 changes: 24 additions & 4 deletions src/sequence.js
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,8 @@ function defineSequence(imagesLoaded, Hammer) {
* @param {Boolean} ignorePhaseThreshold - if true, ignore the threshold
* between phases (breaks reversal of animations but may be used when
* skipping navigation etc)
* @returns {Number} maxWatchedTotal - The new total length
* (duration + delay) for watched elements when reversed
*/
reverseProperties: function(phaseProperties, phaseDelay, phaseThresholdTime, ignorePhaseThreshold) {

Expand All @@ -1453,8 +1455,11 @@ function defineSequence(imagesLoaded, Hammer) {
timingFunctionReversed,
duration,
delay,
total,
maxTotal,
totals = [];
maxWatchedTotal,
totals = [],
watchedTotals = [];

for (i = 0; i < noOfPhaseElements; i++) {
el = phaseElements[i];
Expand All @@ -1463,19 +1468,30 @@ function defineSequence(imagesLoaded, Hammer) {
duration = convertTimeToMs(getStyle(el, Modernizr.prefixed("transitionDuration")));
delay = convertTimeToMs(getStyle(el, Modernizr.prefixed("transitionDelay")));

// Save the total
total = duration + delay;

// Delay elements in relation to the length of other elements in the
// phase eg. If one element A transitions for 1s and element B 2s
// element A should be delayed by 1s (element B length - element A length).
delay = stepDurations.maxTotal - (duration + delay);
delay = stepDurations.maxTotal - total;

// Delay this phase's elements so they animate in relation to the
// other phase
if (ignorePhaseThreshold !== true) {
delay += phaseDelay;
}

// Update the total with the new delay
total = duration + delay;

// Save the total of the reversed animation
totals.push(delay + duration);
totals.push(total);

// Save the total of the reversed animation for watched elements only
if (el.getAttribute("data-seq") !== null) {
watchedTotals.push(total);
}

// Get the timing-function and reverse it
timingFunction = getStyle(el, Modernizr.prefixed("transitionTimingFunction"));
Expand All @@ -1488,6 +1504,10 @@ function defineSequence(imagesLoaded, Hammer) {
// Get the longest total and delay of the reversed animations
maxTotal = Math.max.apply(Math, totals);

// Get the longest total and delay of the reversed animations (for
// watched elements only)
maxWatchedTotal = Math.max.apply(Math, watchedTotals);

// Remove the reversed transition properties from each element once it
// has finished animating; allowing for the inherited styles to take
// effect again.
Expand All @@ -1501,7 +1521,7 @@ function defineSequence(imagesLoaded, Hammer) {
});
}, maxTotal + phaseThresholdTime);

return maxTotal;
return maxWatchedTotal;
},

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/themes/test-theme/css/sequence-theme.test-theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@
border: red solid 1px;
left: 100%;
transition-duration: .5s;
/*transition-delay: 1s;*/
transition-timing-function: linear;
}

#sequence .boxb {
top: 100px;
}

#sequence .seq-out .boxb {

#sequence .boxb {
/*transition-delay: 0s;*/
}

#sequence .seq-in .box {
Expand All @@ -72,5 +73,4 @@
#sequence .seq-out .box {
left: 0;
margin-left: -100px;
transition-delay: .25s;
}
6 changes: 3 additions & 3 deletions tests/themes/test-theme/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
<ul class="seq-canvas">
<li id="step1">
<div class="box box1" data-seq>Box 1</div>
<!-- <div class="box boxb" data-seq>Box 1b </div> -->
<!-- <div class="box boxb">Box 1b </div> -->
</li>
<li id="step2">
<div class="box box2" data-seq>Box 2</div>
<!-- <div class="box boxb" data-seq>Box 2b</div> -->
<!-- <div class="box boxb">Box 2b</div> -->
</li>
<li id="step3">
<div class="box box3" data-seq>Box 3</div>
<!-- <div class="box boxb" data-seq>Box 3b</div> -->
<!-- <div class="box boxb">Box 3b</div> -->
</li>
</ul>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var options = {
// autoPlayDirection: -1,
// autoPlayInterval: 2000,
// autoPlayDelay: 1000,
phaseThreshold: false,
phaseThreshold: 1000,
ignorePhaseThresholdWhenSkipped: true,
preloader: false,
keyNavigation: true,
Expand Down

0 comments on commit fef32cc

Please sign in to comment.