Effectively solve the problem of animation bug after adding border in JS

Source: Internet
Author: User

I did a demo. If a div does not add the border attribute, the Offsetwidth property of the object is used to control width, but if the border attribute is added, the problem comes.

In fact, the Offsetwidth property gets the div content width plus the internal and external margins and the width of the border, so in order to better solve this bug, I have a getstyle method to effectively solve the bug. Here's the code:

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>Test</title></Head><styletype= "Text/css">Div{width:200px;Height:200px;background:Red;Border:2px solid Blue;}</style><Scripttype= "Text/javascript">window.onload= function() {        varOdiv=document.getElementsByTagName ("Div")[0];        Startmove (); functionStartmove () {setinterval (function() {oDiv.style.width=parseint (GetStyle (Odiv,"width"))-1+"px"; }, -); }        functionGetStyle (obj,attr) {if(obj.currentstyle) {returnObj.currentstyle[attr]; }Else{                returngetComputedStyle (obj,false) [attr]; }        }}</Script><Body>    <Div></Div></Body></HTML>

Effectively solve the problem of animation bug after adding border in JS

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.