CSSs width属性 和offsetwidth属性深入理解

  本人菜鸟一枚,趁着奥运期间,一边看奥运,一边学习,最近在慕课网学习前端技术,学习过程中有向很多前辈们博客学习,现在就来分享我的学习所得。

慕课网,导航条菜单的制作,使用javascript去制作伸缩菜单(水平方向上),下面是学习过程中的一个小插曲:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>导航菜单</title>
<style type="text/css">
* { margin: 0; padding: 0; font-size: 14px; }
a { color: #333; text-decoration: none }
ul { list-style: none; height: 30px; border-bottom: 5px solid #F60; margin-top: 20px; padding-left: 50px; }
ul li { float: left }
ul li a { display: block; height: 30px; text-align: center; line-height: 30px; width:120px; background: #efefef; margin-left: 1px; }
a.on, a:hover { background: #F60; color: #fff; }
</style>
<script>
window.onload=function(){
    var aA=document.getElementsByTagName('a');
    for(var i=0; i<aA.length; i++){
        aA[i].onmouseover=function(){
            var This=this;
        
            This.time=setInterval(function(){
                    This.style.width=This.offsetWidth+8+"px";
                    if(This.width>=160"px")
                    clearInterval(This.time);
                },30)
        }
        aA[i].onmouseout=function(){
                clearInterval(this.time);
                var This=this;
                this.time=setInterval(function(){
                    This.style.width=This.offsetWidth-8+"px";
                    if(This.offsetWidth<=120){
                        This.style.width='120px';
                        clearInterval(This.time);
                    }
                },30)
        }
    }
}
</script>
</head>
<body>
<ul>
    <li><a class="on" href="#">首  页</a></li>
    <li><a href="#">新闻快讯</a></li>
<li><a href="#">产品展示</a></li>
    <li><a href="#">售后服务</a></li>
    <li><a href="#">联系我们</a></li>
</ul>
</body>
</html>

 

 1 <script>
 2 window.onload=function(){
 3     var aA=document.getElementsByTagName('a');
 4     for(var i=0; i<aA.length; i++){
 5         aA[i].onmouseover=function(){
 6             var This=this;
 7         
 8             This.time=setInterval(function(){
 9                     This.style.width=This.offsetWidth+8+"px";
10                     if(This.width>=160)
11                     clearInterval(This.time);
12                 },30)
13         }
14         aA[i].onmouseout=function(){
15                 clearInterval(this.time);
16                 var This=this;
17                 this.time=setInterval(function(){
18                     This.style.width=This.offsetWidth-8+"px";
19                     if(This.offsetWidth<=120){
20                         This.style.width='120px';
21                         clearInterval(This.time);
22                     }
23                 },30)
24         }
25     }
26 }
27 </script>

注意到第10行中的

f(This.width>=160)这一句代码,这句代码是错误的,其实这样的错误的原因是因为对width offsetwidth属性不能深入理解导致的,width是字符串,offsetwidth是数值,那我就想,我在160后面加上“px”,这样这句话就成立了啊,呵呵,可是这是一个判断语句,字符串不参加“>=”的比较啊,所以我瞬间就懂了。
说到这里,估计我再也不会犯这样的错误了。
出现这样的错误,网页浏览器就直接不执行这一句话.

posted on 2016-08-14 14:29  zhang1231  阅读(1932)  评论(0编辑  收藏  举报

导航