jquery or JS gets to the element wide-high precision to decimal

Source: Internet
Author: User

First we should know that when we use the width () method of jquery to get the width of the element and the value of the style attribute, if the element's property is a floating-point number, it is automatically rounded to an integer.

And if we just want to get the actual numeric value of the decimal attribute, what is the method.

In order to ensure consistency with the width of the original element when using the obtained width, there are two solutions that I work with:

First JS native method Getboundingclientrect ()

$ (obj) [0].getboundingclientrect (). width.tofixed (2); Get width Preserve two decimal places

The width and height compatibility of this method is poor: IE9 supports width/height properties.

Attach the IE6~IE8-compatible width/height to the wording:

var rectwidth = rectobject.right-rectobject.left;    Rectheight = Rectobject.bottom-rectobject.top;

If the first party takes the left and top, it is not a position, it refers to the distance between the top of the element and the top or left of the window.

Compatibility with IE11 document mode test, IE5 above can support. Left and top are two more pixels below IE7 and IE7.

The second JS native final style method, that is, currentstyle and getComputedStyle compatible notation

var oabc = $ ('. Element ') [0];
if (Oabc.currentstyle) {
IE, Opera
alert (oAbc.currentStyle.width);
var elementx = parsefloat ($ ('. Element ') [0].currentstyle.left]. toFixed (2);
var elementy = parsefloat ($ ('. Element ') [0].currentstyle.top]. toFixed (2);

} else {
FF, Chrome, Safari
var elementx = parsefloat (getComputedStyle ($ (') element ') [0],false]. left). toFixed (2);
var elementy = parsefloat (getComputedStyle ($ ('. Element ') [0],false]. Top). toFixed (2);
}

getComputedStyle (Obj,false): In the new version of FF only the first number of parameters, that is, the operation of the object, the second reference to write "false" is also the general wording, the purpose is to be compatible with the old version of the Firefox browser.

It is recommended to use the second, good compatibility, and can be taken at the same time width, height, left, top accurate values.

jquery or JS gets to the element wide-high precision to decimal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.