Chrome brings the integer value for $('.abc').css("top");
The following javascript code returns two different values for Chrome and Firefox for the 开发者_如何学Gosame page element.
$('.abc').css("top");
Chrome returns: 114px
while Firefox returns 114.1230pxI've tried this several times on the same page and Chrome consistently chops off the decimal point and the following digits.
Any suggestions on how I can get the full value within Chrome as well?
Firefox is a little special in this case... it supports decimal pixels. This means that if you set a height OR width on f.e. an image, it will resize the picture and this calculation will contain (most likely) decimal pixels. This is the same with divs, it gets calculated and decimals are used in firefox.
As far as I know, firefox is the only major browser that support decimals...
If you need full pixels, try a regex expression or something to remove the decimals if you have a string or floor()
if you have a number.
Try: $('.abc').offset().top
or $('.abc').position().top
.
(Although what 0.123 of a pixel looks like is beyond me).
Best answer might be to simply floor()
the value so you end up with 114px in both cases. (Chrome rounds down the sub-pixel amount)
As long as you keep the width a decimal, Chrome will truncate it. If you make it a percentage, though, (might not be an option for you), Chrome will keep it the way it is.
This might not be what you're looking for, but you can store the pixel value in the jQuery object:
$('#foo').data('actualTop', 114.123);
But why do those decimal places bother you?
精彩评论