首页 > 解决方案 > jquery css的负边距

问题描述

当然这个问题很愚蠢,我认为它很愚蠢,但我不知道为什么不在这个用于 css 的 jquery 函数中向我显示负值,我需要发送 -15% 作为 margin-left 和 don '不工作,如果我把这个值放在函数里面,但是通过函数发送不,我不明白这个:

function win(top,width,marginizq)
{
jQuery("#win_background").show(1000);
jQuery("#win_loader").css("margin-top",""+top);
jQuery("#win_loader").css("width",""+width);
jQuery("#win_loader").css("margin-left",""+marginizq);
}



<script>
win("200","30%","-15%");
</script>

一切正常,但如果我将内部函数设置为默认值,则在负值 -15% 时无法正常工作

为什么不这样做?我知道价值必须发送负数或否,对吗?

这是我的问题,谢谢

标签: jquerycss

解决方案


您的脚本似乎工作得很好,尽管margin-top在您应该添加单位定义(等)的部分px%一个小错字。另外,添加css时不必在变量前添加引号。

.css("margin-top",""+top)可以写成.css("margin-top", top)

看到它在这里工作正常(纠正错字):

function win(top,width,marginizq)
{
jQuery("#win_loader").css("margin-top", top);
jQuery("#win_loader").css("width", width);
jQuery("#win_loader").css("margin-left", marginizq);
}

win("200px","30%","-15%");
#win_loader{
  background:red;
  height:200px;
  width:100%;
  position:absolute;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="win_loader">

</div>


推荐阅读