1

I tried to apply some dynamic scale and translate css properties some thing like this

var scale="scale(2,2)";
var translate="translate(20,20)";
var prop=scale+" "+translate;
$(this).css({
            "transform":prop
             });

But this is not working. I even tried some thing like this

 prop="'"+scale+" "+translate+"'";
$(this).css({"transform":prop});

Even this not working.Please help to resolve this.

1
  • 3
    Give units for the translate transform. It won't work without it. Otherwise, nothing wrong with your code. Commented May 27, 2016 at 13:28

2 Answers 2

2

As mentioned by Harry, you have to have measurement units on your translate property.

See below:

var scale="scale(2,2)";
var translate="translate(20px,20px)";
var prop=scale+" "+translate;
$(this).css({
   "transform":prop
});

A slightly easier alternative is to do a class change instead, and assign your transform styling to the new class.

Like this:

$(this).addClass('transform-me');

And the CSS

.transform-me {
  transform:translate(20px, 20px) scale(2,2);
}

Doing it this way achieves the exact same results, but it does simplify troubleshooting where the error is occurring (whether it is the CSS or the jQuery causing problems).

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

Comments

0

I wanted to add another way using css matrix and manipulating it with jquery. Powerful way to adjust images. In the following example, we setup variables to hold the data and move an image left by 1 pixel.

var prior = parseInt($(this).css('transform').split(',')[4]) || 0;
lastX -= 1;
args = [lastScale, 0, 0, lastScale, lastX, lastY]
$(this).css('transform', 'matrix(' + args.join(",") + ')');

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.