admin管理员组文章数量:1307752
I have a syntax issue as I want to do something quite simple. Apply a negative value to a variable using .css
.
Here yo have the code:
var figureImage = $('.js-image-centering');
var figureImageHeight = figureImage.height();
var figureImageWidth = figureImage.width();
var figureImageMarginLeft = (0-(figureImageWidth/2));
var figureImageMarginTop = (0-(figureImageHeight/2));
figureImage.css('margin-left', figureImageMarginLeft);
figureImage.css('margin-top', figureImageMarginTop);
I would like to forget about figureImageMarginLeft
and figureImageMarginTop
. So, its it possible to something like this?
figureImage.css('margin-left', -figureImageMarginLeft);
How do you write it correctly?
I have a syntax issue as I want to do something quite simple. Apply a negative value to a variable using .css
.
Here yo have the code:
var figureImage = $('.js-image-centering');
var figureImageHeight = figureImage.height();
var figureImageWidth = figureImage.width();
var figureImageMarginLeft = (0-(figureImageWidth/2));
var figureImageMarginTop = (0-(figureImageHeight/2));
figureImage.css('margin-left', figureImageMarginLeft);
figureImage.css('margin-top', figureImageMarginTop);
I would like to forget about figureImageMarginLeft
and figureImageMarginTop
. So, its it possible to something like this?
figureImage.css('margin-left', -figureImageMarginLeft);
How do you write it correctly?
Share Improve this question asked Jan 30, 2014 at 10:36 Daniel Ramirez-EscuderoDaniel Ramirez-Escudero 4,04713 gold badges46 silver badges81 bronze badges 6-
1
you dont need to do
(0-(figureImageWidth/2))
, doing-figureImageWidth/2
is fine. – Ashish Kumar Commented Jan 30, 2014 at 10:40 -
If you dont know that
figureImageMarginLeft
is -ve or +ve, then you can do,-Math.abs(figureImageMarginLeft)
to get negative number always. – Ashish Kumar Commented Jan 30, 2014 at 10:43 - @AshishKumar This worked perfectly, but I would like to know for a future how to apply this negative value on the .css field if I need it. – Daniel Ramirez-Escudero Commented Jan 30, 2014 at 10:43
- @AshishKumar Steve Robinson answer was the correct one, but I would like to know what method would be better for speed quality or best practices – Daniel Ramirez-Escudero Commented Jan 30, 2014 at 10:45
-
1
In jQuery you dont need to specify
px
as unit for.css()
or.animate()
or.height()
etc. – Ashish Kumar Commented Jan 30, 2014 at 10:54
4 Answers
Reset to default 6Yes Absolutely. If you do,
var a = 100;
$(".stuff").css("margin-left",-a)
the element would get the rule: margin-left: -100px
What about figureImage.css('margin-left', "-" + figureImageMarginLeft + 'px');
?
You can just do this:
var mL = -30;
$("div").css("marginLeft", mL + "px");
Fiddle
That will do the trick (will make positive values negative and negatives positiv):
figureImage.css('margin-left', -figureImageMarginLeft+"px");
or, if you want it to be always negative:
figureImage.css('margin-left', -Math.abs(figureImageMarginLeft)+"px");
always positiv:
figureImage.css('margin-left', Math.abs(figureImageMarginLeft)+"px");
example: http://jsfiddle/rN3cw/
本文标签: javascriptapply negative value with variable using css with jqueryStack Overflow
版权声明:本文标题:javascript - apply negative value with variable using .css with jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741810721a2398775.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论