60

How can you specify cross-browser transform controls using jQuery since each browser seems to use its own methodology?

Here is the code I am using for Firefox but I can't even get it to work. I think it's because there is no way to tell which transformation – scale, rotate, skew – to use.

$(".oSlider-rotate").slider({
     min: 10,
     max: 74,
     step: .01,
     value: 24,
     slide: function(e,ui){
                 $('.user-text').css('transform', ui.value)

            }                
  });
doubleDown
  • 8,048
  • 1
  • 32
  • 48
ndesign11
  • 1,734
  • 6
  • 20
  • 36

5 Answers5

119

If you want to use a specific transform function, then all you need to do is include that function in the value. For example:

$('.user-text').css('transform', 'scale(' + ui.value + ')');

Secondly, browser support is getting better, but you'll probably still need to use vendor prefixes like so:

$('.user-text').css({
  '-webkit-transform' : 'scale(' + ui.value + ')',
  '-moz-transform'    : 'scale(' + ui.value + ')',
  '-ms-transform'     : 'scale(' + ui.value + ')',
  '-o-transform'      : 'scale(' + ui.value + ')',
  'transform'         : 'scale(' + ui.value + ')'
});

jsFiddle with example: http://jsfiddle.net/jehka/230/

Christian
  • 19,605
  • 3
  • 54
  • 70
  • That only affects IE, which we're not targeting anyway :p – Christian May 29 '12 at 23:57
  • Clarification: The OP wrote **cross browser** transform. – arttronics May 30 '12 at 00:00
  • 1
    Removing the comma doesn't make it fully cross browser, neither the `-moz` nor `-webkit` vendor prefixes apply to IE. My code serves as an example of *how* to apply the properties. If you don't like it, feel free to write your own answer :) – Christian May 30 '12 at 00:03
  • This [SO answer](http://stackoverflow.com/a/5139395/1195891) explains that the extra comma is against ES5 spec with regards to cross-browser syntax. Just sayin. :p – arttronics May 30 '12 at 01:10
  • That answer shows that a trailing comma *is* valid in the ES5 spec. – Christian May 30 '12 at 01:36
  • As stated it depends in what manner, but it's your decision nontheless. – arttronics May 30 '12 at 01:47
  • Simply put, the [SO Answer](http://stackoverflow.com/a/5139232/1195891) accepted answer of that question states big trouble for IE browsers. The OP here wanted a cross-browser solution which is why I downvoted you since you wrote that it's going to be an issue only in IE browsers for which you do not want to target. I never intended to write an answer here, only enlighten you of a syntax error. – arttronics May 30 '12 at 02:04
8

Setting a -vendor prefix that isn't supported in older browsers can cause them to throw an exception with .css. Instead detect the supported prefix first:

// Start with a fall back
var newCss = { 'zoom' : ui.value };

// Replace with transform, if supported
if('WebkitTransform' in document.body.style) 
{
    newCss = { '-webkit-transform': 'scale(' + ui.value + ')'};
}
// repeat for supported browsers
else if('transform' in document.body.style) 
{
    newCss = { 'transform': 'scale(' + ui.value + ')'};
}

// Set the CSS
$('.user-text').css(newCss)

That works in old browsers. I've done scale here but you could replace it with whatever other transform you wanted.

Keith
  • 150,284
  • 78
  • 298
  • 434
2

If you're using jquery, jquery.transit is very simple and powerful lib that allows you to make your transformation while handling cross-browser compability for you. It can be as simple as this : $("#element").transition({x:'90px'}).

Take it from this link : http://ricostacruz.com/jquery.transit/

Monero Jeanniton
  • 441
  • 8
  • 20
1

I started using the 'prefix-free' Script available at http://leaverou.github.io/prefixfree so I don't have to take care about the vendor prefixes. It neatly takes care of setting the correct vendor prefix behind the scenes for you. Plus a jQuery Plugin is available as well so one can still use jQuery's .css() method without code changes, so the suggested line in combination with prefix-free would be all you need:

$('.user-text').css('transform', 'scale(' + ui.value + ')');
Philip Jespersen
  • 2,816
  • 2
  • 19
  • 9
1
$(".oSlider-rotate").slider({
     min: 10,
     max: 74,
     step: .01,
     value: 24,
     slide: function(e,ui){
                 $('.user-text').css('transform', 'scale(' + ui.value + ')')

            }                
  });

This will solve the issue

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    Thank you participating at stackoverflow. Please add some explanation to your answer to make it useful for others. – cronoik Oct 09 '19 at 12:54