0

I need to detect if the CSS transform property has changed using jQuery. In firebug the CSS property i'm looking to detect changes on is rendered like this:

-moz-transform: translate(-0px, 0px)

I need to detect whether this has changed to another pixel property. For example:

-moz-transform: translate(-100px, 0px)

More details:

Ultimately I'm trying to detect between a click and a drag on the following demo.

http://cubiq.org/dropbox/iscroll4/examples/carousel/

I figured that I could query mouseup on the current transform property and if it remained the same that it was a click or if the transform property changed it would be a drag.

3
  • The user would change it by dragging a container with a large overflow. Commented Dec 21, 2011 at 0:20
  • 1
    Sounds like you might have a bigger problem. Unless this is literally all you need to do, it would be to your benefit to explain what you're doing and why you need this - your solution may not be the only one, or might be too much work, or just isn't the right one. EDIT: Can't you just use a callback for after the user drags the container? Commented Dec 21, 2011 at 0:21
  • added more details about the issue. If there is an easier way to accomplish the ultimate goal, please let me know. Commented Dec 21, 2011 at 0:38

1 Answer 1

2

You can use Modernizr to create the vender prefix and then you can use .split() to break apart the output from .css() and get the desired values.

Here is a method of getting the properly prefixed property:

if (Modernizr.csstransforms3d) {
    trans_key = Modernizr.prefixed('transform').replace(/([A-Z])/g, function(str,m1){ return '-' + m1.toLowerCase(); }).replace(/^ms-/,'-ms-');

} else if (Modernizr.csstransforms) {
    trans_key = Modernizr.prefixed('transform').replace(/([A-Z])/g, function(str,m1){ return '-' + m1.toLowerCase(); }).replace(/^ms-/,'-ms-');
}

Source: http://www.modernizr.com/docs/#prefixed

Then you can use .css() to get the -vender-transform property:

var matrix = $('#element-id').css(trans_key);//output = matrix(1, 0, 0, 1, 100px, 0px)

Then finally you can .split() the matrix value into something useable:

var data = matrix.replace(')', '').split(',');//data[4]='100px', data[5]='0px'

Here is a demo: http://jsfiddle.net/7Yz8B/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.