Fixed offset bug in slidebox

Sometimes the slidebox would get out of whack, it was a timing issue
with requestAnimationFrame
This commit is contained in:
Max Lynch
2013-10-16 00:39:38 -05:00
parent 1bfbd701ab
commit acd1b368ba
2 changed files with 64 additions and 54 deletions

59
dist/js/ionic.js vendored
View File

@ -2297,6 +2297,7 @@ window.ionic = {
},
_startDrag: function(e) {
var offsetX, content;
console.log("START");
this._initDrag();
@ -2320,49 +2321,53 @@ window.ionic = {
_handleEndDrag: function(e) {
var _this = this;
console.log("END");
var finalOffsetX, content, ratio, slideWidth, totalWidth, offsetX;
window.requestAnimationFrame(function() {
// We didn't have a drag, so just init and leave
if(!this._currentDrag) {
this._initDrag();
return;
}
// We didn't have a drag, so just init and leave
if(!_this._currentDrag) {
_this._initDrag();
return;
}
// Snap to the correct spot
// Snap to the correct spot
content = this._currentDrag.content;
content = _this._currentDrag.content;
// Enable transition duration
content.classList.add('slide-box-animating');
// Enable transition duration
content.classList.add('slide-box-animating');
offsetX = parseFloat(content.style.webkitTransform.replace('translate3d(', '').split(',')[0]) || 0;
slideWidth = content.offsetWidth;
totalWidth = content.offsetWidth * content.children.length;
offsetX = parseFloat(content.style.webkitTransform.replace('translate3d(', '').split(',')[0]) || 0;
slideWidth = content.offsetWidth;
totalWidth = content.offsetWidth * content.children.length;
// Calculate how far in this slide we've dragged
ratio = (offsetX % slideWidth) / slideWidth;
// Calculate how far in this slide we've dragged
ratio = (offsetX % slideWidth) / slideWidth;
if(ratio >= 0) {
// Anything greater than zero is too far left
finalOffsetX = 0;
} else if(ratio >= -0.5) {
finalOffsetX = Math.max(0, Math.floor(Math.abs(offsetX) / slideWidth) * slideWidth);
} else {
// Sliiide to the right
finalOffsetX = Math.min(totalWidth - slideWidth, Math.ceil(Math.abs(offsetX) / slideWidth) * slideWidth);
}
if(ratio >= 0) {
// Anything greater than zero is too far left
finalOffsetX = 0;
} else if(ratio >= -0.5) {
finalOffsetX = Math.max(0, Math.floor(Math.abs(offsetX) / slideWidth) * slideWidth);
} else {
// Sliiide to the right
finalOffsetX = Math.min(totalWidth - slideWidth, Math.ceil(Math.abs(offsetX) / slideWidth) * slideWidth);
}
// Negative offsetX to slide correctly
content.style.webkitTransform = 'translate3d(' + -finalOffsetX + 'px, 0, 0)';
// Negative offsetX to slide correctly
content.style.webkitTransform = 'translate3d(' + -finalOffsetX + 'px, 0, 0)';
this._initDrag();
_this._initDrag();
});
},
/**
* Process the drag event to move the item to the left or right.
*/
_handleDrag: function(e) {
var _this = this;
console.log("DRAG");
window.requestAnimationFrame(function() {
var content;
@ -2383,7 +2388,7 @@ window.ionic = {
content = _this._currentDrag.content;
// Grab the new X point, capping it at zero
var newX = Math.min(content.offsetWidth, _this._currentDrag.startOffsetX + e.gesture.deltaX);
var newX = Math.min(0, _this._currentDrag.startOffsetX + e.gesture.deltaX);
/*
var rightMostX = -(content.offsetWidth * Math.max(0, content.children.length - 1));