简体   繁体   English

使用JavaScript变量在JQuery中设置样式

[英]Setting style in JQuery using JavaScript variable

I can't get this following code sample working 我无法让以下代码示例正常工作

var iZoomValue = Math.round(window.innerWidth/12.5);

$('body').css("zoom",iZoomValue);

Any suggestions what am I doing wrong over here ? 有什么建议我在这做错了吗?

More inormation about CSS Zoom 有关CSS Zoom的更多信息

Make sure that your code is being fired when the document is ready: 确保在文档准备就绪时触发了您的代码:

$(document).ready(function(){
 // code here
});

Remember that zoom is a fairly new feature in the CSS3 spec and isn't supported by all browsers at this time. 请记住, zoom是CSS3规范中的一项相当新的功能,目前尚不受所有浏览器的支持。

Here's a working example (works for me in Chrome) 这是一个有效的示例 (适用于Chrome浏览器)

Not sure that doing it in jQuery document.ready will solve it, as the OP has said that it works if he uses a hard coded value: 不确定在jQuery document.ready中执行此操作是否会解决该问题,因为OP曾说如果他使用硬编码值就可以工作:

$('body').css("zoom","82");

My get feel is that either iZoomValue is not being set to what you think it is - perhaps an alert or console.log will help assert this? 我的感觉是没有将iZoomValue设置为您认为的样子-警​​报或console.log可能会帮助断言这一点?

Or that the jQuery css method accepts string parameter values (as in your hard coded example). 或者说jQuery css方法接受字符串参数值(如您的硬编码示例)。 iZoomValue will be a number, so the result will be iZoomValue将是一个数字,因此结果将是

$('body').css("zoom",82);

It might be worth using a hard coded example with an integer as the param to assert this. 可能需要使用带有整数作为参数的硬编码示例来断言这一点。

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM