admin管理员组

文章数量:1291595

I have problem with changing background-size via jQuery. It works when you give only one parameter, but when I give 2 parameter it doesn't work, I need to send via jQuery 2 variables into background-size for width and height.

here is my js code example.

var w = 100;
var h = 100;

$("div").css("background-size", w+"px" + h + "px");

here is demo

/

it's just a simple example in real version I need to send 2 parameter that calculate ratio.

Thanks

I have problem with changing background-size via jQuery. It works when you give only one parameter, but when I give 2 parameter it doesn't work, I need to send via jQuery 2 variables into background-size for width and height.

here is my js code example.

var w = 100;
var h = 100;

$("div").css("background-size", w+"px" + h + "px");

here is demo

http://jsfiddle/Lv4wt75f/

it's just a simple example in real version I need to send 2 parameter that calculate ratio.

Thanks

Share Improve this question asked Feb 23, 2015 at 6:30 Aram MkrtchyanAram Mkrtchyan 2,7004 gold badges35 silver badges48 bronze badges 1
  • w3schools./cssref/… – Sadikhasan Commented Feb 23, 2015 at 6:38
Add a ment  | 

4 Answers 4

Reset to default 4

Because both those parameters need to have a space in between. You simply need one extra space

$("div").css("background-size", w+"px " + h + "px");
                                     ^

Fiddle

Syntax Reference

You need a space between that px.

$("div").css("background-size", w+"px " + h + "px");
                                     ^ (Space Here)
$("div").css("background-size": w+"px " + h+"px");
                                     ^ (Space)

use :(colon) instead of ,(ma) after background-size

本文标签: javascriptJquery change background sizeStack Overflow