0
function Scale(){
    X = innerWidth / 1024;
    Y = innerHeight / 768;

    Z = document.getElementById("IFM");
    Z.style.transform = "scale(X, Y)";
}

I have a problem with this code. I couldn't use variables in scale!
What can I do with this problem?

David G
  • 94,763
  • 41
  • 167
  • 253
BlackMB
  • 230
  • 6
  • 20

4 Answers4

2

JavaScript has no inline string variables. All you can do is concatenate strings:

Z.style.transform = "scale("+X+", "+Y+")";

The numbers will be implicitly converted to strings.

You might as well use some custom sprintf-like format- or replace-methods, but usually concatenation with + is simpler.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

You either build the string with concats / plus or use String.prototype.replace.

Z.style.transform = "scale(" + X + "," + Y + ")";

or with a helper like

String.prototype.sFormat = function _simpleFormat( map ) {
    var myString    = this.toString(),
        args        = map instanceof Array ? map : Array.prototype.slice.call( arguments );

    while( ~myString.indexOf( '%r' ) ) {
        myString = myString.replace( '%r', args.shift() );
    }

    return myString;
};

.. and then go like

Z.style.transform = "scale(%r, %r)".sFormat(X,Y);
jAndy
  • 231,737
  • 57
  • 305
  • 359
0

Change that line to:

Z.style.transform = "scale(" + X +", "+ Y + ")";
Austin Mullins
  • 7,307
  • 2
  • 33
  • 48
0

try this way "scale(" +X+ "," +Y+ ")";

user2031802
  • 744
  • 8
  • 7