Let’s first take a look at how this bug occurs. Copy code The code is as follows: <br> #div1 {<br> width: 200px;<br> height : 200px;<br> background: red;<br> }<br> <br><br> </div> <p></p> <div class="codetitle"><span>Copy code<a style="CURSOR: pointer" data="90578" class="copybut" id="copybut90578" onclick="doCopy('code90578')"><u></u> The code is as follows:</a></span></div> <body><div class="codebody" id="code90578"> <div id="div1"><br> </body><br> <br><br> <br>The following is the Javascript code used for testing, the purpose is to make the div slowly narrow. <br> </div> <p></p> <p>Copy code</p> <div class="codetitle"> <span><a style="CURSOR: pointer" data="68552" class="copybut" id="copybut68552" onclick="doCopy('code68552')"> The code is as follows:<u></u></a> <script type="text/javascript"></span> setInterval(function(){</div> var oDiv=document.getElementById("div1");<div class="codebody" id="code68552"> oDiv.style.width=oDiv.offsetWidth-1 'px';<br> },30);<br> <br><br> <br>The Javascript code is very simple. If you run it, there will be no problem. The desired div will gradually become smaller. <br> <br>Then where does this offset bug come from? <br> <br>Now let’s move the style and something magical will happen. . . </div> We add a style border to div1: 1px solid #CCCCFF;<p> </p> <p></p> <p></p>Copy code<p></p> <p> The code is as follows:</p> <div class="codetitle"> <span><a style="CURSOR: pointer" data="8073" class="copybut" id="copybut8073" onclick="doCopy('code8073')"> <style type="text/css"><u> #div1 {</u> width: 200px;</a> height : 200px;</span> background: red;</div> border: 1px solid #CCCCFF;<div class="codebody" id="code8073"> }<br> <br> <p>When I run the code at this time, I find that the div is slowly increasing to the right. . . image BUG suddenly appeared. . . . It is obviously minus 1. Why does this happen? </p> <p>Let us think about the characteristics of offset: </p> <p>Example: div width: 200px border 1px. Actually the offsetWidth he got is 202px. </p> <p>So, let’s talk about it. When the movement just started, the width of the div was actually 200px, so the offsetWidth was 202</p> <p>At this time oDiv.style.width=oDiv.offsetWidth-1 'px'; this sentence is equal to oDiv.style.width=202-1=201px; and then assign it to width</p> <p>When this sentence is executed again, the width of the div is 201px; in this case, it will increase by 1px each time, but it will gradually become larger. This is the offset bug. </p> <p>How to solve this problem? </p> <p>In fact, you don’t need this offsetWidth. We use width ! ! Write a function to directly obtain the width in the css style </p> <p>Get styles that are not between lines: </p> <p></p> <div class="codetitle"> <span><a style="CURSOR: pointer" data="89510" class="copybut" id="copybut89510" onclick="doCopy('code89510')"><u>Copy code</u></a></span> The code is as follows:</div> <div class="codebody" id="code89510"> <br> function getStyle(obj, name) {<br> If (obj.currentStyle) {<br> return obj.currentStyle[name];<br> } else {<br> return getComputedStyle(obj, null)[name];<br> }<br> }<br> </div> <p>Then let’s modify the original code: </p> <p></p> <div class="codetitle"> <span><a style="CURSOR: pointer" data="71756" class="copybut" id="copybut71756" onclick="doCopy('code71756')"><u>Copy code</u></a></span> The code is as follows:</div> <div class="codebody" id="code71756"> <br> <script type="text/javascript"><br> setInterval(function(){<br> var oDiv=document.getElementById("div1");<br> oDiv.style.width=parseInt(getStyle(oDiv,'width'))-1 'px';<br> },30);<br> function getStyle(obj, name) {<br> If (obj.currentStyle) {<br> return obj.currentStyle[name];<br> } else {<br> return getComputedStyle(obj, null)[name];<br> }<br> }<br> <br> </div> <br> This way the program will run without any problems. <br> </div>