jQuery CSS分配负值 [英] Jquery CSS assigning negative values

查看:77
本文介绍了jQuery CSS分配负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的jQuery代码:

This is my jQuery code:

<script type="text/javascript" src="js/jquery.js">
</script>
<script type="text/javascript">
 $(document).ready(function() {
     get_font_size();
     set_sidebar();

});
function get_font_size()
{
var b=$(window).width();
var side_size=b*260/1440;
b=b-side_size;
$("#sidebar").css({"width":side_size});
$("#alexa-widget img").css({"width":side_size});
$(".fb-like-box").attr("data-width",side_size); 
}
function set_sidebar()
{
var b=$(window).width();
var font_size=b*65/1440;
var font_size_2=b*133/1440;
var margin_top=b*10/1440;
margin_top*=-1;
margin_top=Math.round(margin_top);
$("#my_tabs li").css({"font-size":font_size});
$("#header").css({"font-size":font_size_2,"margin-top":margin_top});

}
</script>

除margin-top属性外,其他所有东西都可以正常工作.我无法将margin-top分配给负值.它失败了,但是字体大小和其他属性都可以正常工作.为什么jquery css设置会失败并带有负值?在CSS中使用相同的代码是完美的.我需要根据屏幕分辨率更改网页,所以我无法在CSS内完成此操作.需要jQuery修复.我的代码中已经包含视口元标记,所以请不要建议使用视口或媒体查询解决方案.我已经使用alert(margin_top)检查了margin_top变量,并根据需要向我显示了正确的负值,因此仅在css设置部分失败的情况下才能正确进行计算.请帮助我解决此问题.谢谢.

Everything works fine except the margin-top property.I can't assign margin-top to a negative value.It fails but font size and other properties work's fine.Why jquery css settings fails with negative values?If I do the the same inside css it work's perfect.I need this to alter the webpage according to screen resolutions So I can't do that inside CSS.I need jQuery fix for this.I already have viewport meta tag in my code so please don't suggest viewport or media query solutions.I have checked the margin_top variable with alert(margin_top) and it shows me a correct negative value as desired so the calculation takes place correctly only the css setting part fails.Please help me fix this.Thank You.

推荐答案

使用jQuery设置边距顶部时,必须在末尾添加单位(在这种情况下为px)

When setting the margin-top using jQuery, you must add the units (in this case: px) at the end

示例:

>>> $("body").css("margin-top");
"0px"
>>> $("body").css("margin-top", "10");
[body]
>>> $("body").css("margin-top");
"0px"
>>> $("body").css("margin-top", "10px");
[body]
>>> $("body").css("margin-top");
"10px"


编辑(以使您更清楚地了解如何修正代码:


EDIT (to make it more clear on how to fix your code:

在代码中更改如下所示的行:

Change the line in your code that looks like:

$("#header").css({"font-size":font_size_2,"margin-top":margin_top});

$("#header").css({"font-size":font_size_2,"margin-top":margin_top + "px"});

这篇关于jQuery CSS分配负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆