Using css transform property in jQuery

前端 未结 5 592
广开言路
广开言路 2020-12-24 10:53

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

5条回答
  •  心在旅途
    2020-12-24 11:09

    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.

提交回复
热议问题