Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Z translate. #104

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions jquery.transit.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
// Allows you to rotate, scale and translate.
registerCssHook('scale');
registerCssHook('translate');
registerCssHook('translate3d');
registerCssHook('rotate');
registerCssHook('rotateX');
registerCssHook('rotateY');
Expand All @@ -215,6 +216,7 @@
registerCssHook('skewY');
registerCssHook('x', true);
registerCssHook('y', true);
registerCssHook('z', true);

// ## Transform class
// This is the main class of a transformation property that powers
Expand Down Expand Up @@ -328,33 +330,43 @@
this.perspective = unit(dist, 'px');
},

// ### x / y
// ### x / y / z
// Translations. Notice how this keeps the other value.
//
// .css({ x: 4 }) //=> "translate(4px, 0)"
// .css({ y: 10 }) //=> "translate(4px, 10px)"
// .css({ x: 4 }) //=> "translate(4px, 0, 0)"
// .css({ y: 10 }) //=> "translate(4px, 10px, 0)"
//
x: function(x) {
this.set('translate', x, null);
this.set('translate', x, null, null);
},

y: function(y) {
this.set('translate', null, y);
this.set('translate', null, y, null);
},

z: function(z) {
this.set('translate3d', null, null, z);
},

// ### translate
// Notice how this keeps the other value.
//
// .css({ translate: '2, 5' }) //=> "translate(2px, 5px)"
// .css({ translate: '2, 5, 3' }) //=> "translate(2px, 5px, 3px)"
//
translate: function(x, y) {
this.set('translate3d', x, y, 0);
},

translate3d: function(x, y, z) {
if (this._translateX === undefined) { this._translateX = 0; }
if (this._translateY === undefined) { this._translateY = 0; }
if (this._translateZ === undefined) { this._translateZ = 0; }

if (x !== null && x !== undefined) { this._translateX = unit(x, 'px'); }
if (y !== null && y !== undefined) { this._translateY = unit(y, 'px'); }
if (z !== null && z !== undefined) { this._translateZ = unit(z, 'px'); }

this.translate = this._translateX + "," + this._translateY;
this.translate3d = this._translateX + "," + this._translateY + "," + this._translateZ;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you change this line to

if (support.transform3d)
    this.translate3d = this._translateX + "," + this._translateY + "," + this._translateZ;
else
    this.translate = this._translateX + "," + this._translateY;

your solution also works with browsers that only support 2D transforms!

}
},

Expand All @@ -367,6 +379,10 @@
return this._translateY || 0;
},

z: function() {
return this._translateZ || 0;
},

scale: function() {
var s = (this.scale || "1,1").split(',');
if (s[0]) { s[0] = parseFloat(s[0]); }
Expand Down Expand Up @@ -416,7 +432,7 @@
if (use3d && (i === 'scale')) {
re.push(i + "3d(" + this[i] + ",1)");
} else if (use3d && (i === 'translate')) {
re.push(i + "3d(" + this[i] + ",0)");
re.push(i + "3d(" + this[i] + ")");
} else {
re.push(i + "(" + this[i] + ")");
}
Expand Down
4 changes: 2 additions & 2 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
addScript("../jquery.transit.js");
})();
</script>

<script src="test.js"></script>
<link href="style.css" rel="stylesheet" />
</head>
Expand All @@ -53,7 +53,7 @@ <h1>jQuery transit tests</h1>
<script>
group('Transformations');

test('Translation', function($box) { $box.transition({ x: 20, y: 20 }); });
test('Translation', function($box) { $box.transition({ x: 20, y: 20, z: 20 }); });
test('Rotate', function($box) { $box.transition({ rotate: 45 }); });
test('Rotate via string', function($box) { $box.transition({ rotate: '45deg' }); });
test('Skew X', function($box) { $box.transition({ skewX: 30 }); });
Expand Down