jQuery学习——尺寸

.height()

获取匹配元素集合中的第一个元素的当前计算高度值 或 设置每一个匹配元素的高度值。

Contents:

  • .height()
    • .height()
  • .height( value )
    • .height( value )
    • .height( function(index, height) )

.css('height') 和 .height()之间的区别是后者返回一个没有单位的数值(例如,400),前者是返回带有完整单位的字符串(例如,400px)。当一个元素的高度需要数学计算的时候推荐使用.height() 方法 。

$(window).height(); // returns height of browser viewport
$(document).height(); // returns height of HTML document

注意.height()总是返回内容宽度,不管CSS box-sizing属性值。

注意:在绝对定位和给定display:block时,虽然stylescript标签用.width() 或 height()也将报告一个值,强烈建议不要在这些标签中调用这些方法。这是一种不好的做法,结果也证明是不可靠的。

.height( value )

当调用.height(value)方法的时候,这个“value”参数可以是一个字符串(数字加单位)或者是一个数字。如果这个“value”参数只提供一个数字,jQuery会自动加上单位px。如果只提供一个字符串,任何有效的CSS尺寸都可以为高度赋值(就像100px, 50%, 或者 auto)。注意在现代浏览器中,CSS高度属性不包含padding, border, 或者 margin。

如果没有给定明确的单位(像'em' 或者 '%'),那么默认情况下"px"会被直接添加上去(也理解为"px"是默认单位)。

注意.height('value')设置的容器宽度是根据CSS box-sizing属性来定的, 将这个属性值改成border-box,将造成这个函数改变这个容器的outerHeight,而不是原来的内容高度。

点击每个div时设置该div高度为30px,并改变颜色。

<!DOCTYPE html>
<html>
<head>
  <style>div { width:50px; height:70px; float:left; margin:5px;
        background:rgb(255,140,0); cursor:pointer; }  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div></div>
  <div></div>
 
  <div></div>
  <div></div>
  <div></div>
<script>$("div").one('click', function () {
      $(this).height(30)
             .css({cursor:"auto", backgroundColor:"green"});
    });</script>
 
</body>
</html>

.innerHeight()

描述: 为匹配的元素集合中获取第一个元素的当前计算高度值,包括padding,但是不包括border。

这个方法返回元素的高度,包括顶部和底部的padding,单位是像素。

这个方法不适用于window and document对象,可以使用.height()代替。

获取段落的innerHeight。

!DOCTYPE html>
<html>
<head>
  <style>p { margin:10px;padding:5px;border:2px solid #666; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>Hello</p><p></p>
<script>var p = $("p:first");
$("p:last").text( "innerHeight:" + p.innerHeight() );</script>
 
</body>
</html>

.innerWidth()

 描述: 为匹配的元素集合中获取第一个元素的当前计算宽度值,包括padding,但是不包括border。

获取段落的innerWidth。

<!DOCTYPE html>
<html>
<head>
  <style>p { margin:10px;padding:5px;border:2px solid #666; } </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>Hello</p><p></p>
<script>var p = $("p:first");
$("p:last").text( "innerWidth:" + p.innerWidth() );</script>
 
</body>
</html>

.outerHeight()

描述: 获取元素集合中第一个元素的当前计算高度值,包括padding,border和选择性的margin。返回一个整数(不包含“px”)表示的值  ,或如果在一个空集合上调用该方法,则会返回 null。

.outerHeight()计算中总是包含padding-top ,padding-bottom 和 border-top,border-bottom ;如果includeMargin参数是true,那么margin (top 和 bottom)也会被包含。

这个方法不适用于window 和 document对象,可以使用.height()代替。

获取一个段落的outerHeight。

<!DOCTYPE html>
<html>
<head>
  <style>p { margin:10px;padding:5px;border:2px solid #666; } </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>Hello</p><p></p>
<script>var p = $("p:first");
$("p:last").text( "outerHeight:" + p.outerHeight() + " , outerHeight(true):" + p.outerHeight(true) );</script>
 
</body>
</html>

.outerWidth()

描述: 获取元素集合中第一个元素的当前计算宽度值,包括padding,border和选择性的margin。(愚人码头注:返回一个整数(不包含“px”)表示的值,或如果在一个空集合上调用该方法,则会返回 null。

返回元素的宽度,一直包括l左右 padding值,border值和可选择性的margin。单位为像素。

如果 includeMargin省略或者false,padding 和 border会被包含在计算中;如果true,margin也会被包含在计算中 。

这个方法不适用于window 和 document对象,可以使用.width()代替。

获取一个段落的outerWidth。

<!DOCTYPE html>
<html>
<head>
  <style>
  p { margin:10px;padding:5px;border:2px solid #666; }
</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p>Hello</p><p></p>
<script>var p = $("p:first");
$("p:last").text( "outerWidth:" + p.outerWidth()+ " , outerWidth(true):" + p.outerWidth(true) );
</script>
 
</body>
</html>

.width()

为匹配的元素集合中获取第一个元素的当前计算宽度值或给每个匹配的元素设置宽度。

Contents:

  • .width()
    • .width()
  • .width( value )
    • .width( value )
    • .width( function(index, width) )

.css(width) 和 .width()之间的区别是后者返回一个没有单位的数值(例如,400),前者是返回带有完整单位的字符串(例如,400px)。当一个元素的宽度需要数学计算的时候推荐使用.width() 方法 。

这个方法同样能计算出window和document的宽度。

 
$(window).width();   // returns width of browser viewport
$(document).width(); // returns width of HTML document

注意.width()总是返回内容宽度,不管CSS box-sizing属性值。截至jQuery 1.8,这可能需要检索的CSS的宽度加加上box-sizing的属性,然后当元素有 box-sizing: border-box时候,减去个元素上任何潜在border和padding值。为了避免这种问题,使用.css( "width" )而非.width()

显示各个宽度。注意这个来自值iframe的值可能小于你的预期。黄色高亮显示iframe body。

 
<!DOCTYPE html>
<html>
<head>
  <style>
  body { background:yellow; }
  button { font-size:12px; margin:2px; }
  p { width:150px; border:1px red solid; }
  div { color:red; font-weight:bold;  }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <button id="getp">Get Paragraph Width</button>
  <button id="getd">Get Document Width</button>
  <button id="getw">Get Window Width</button>
 
  <div>&nbsp;</div>
  <p>
    Sample paragraph to test width
  </p>
<script>
function showWidth(ele, w) {
  $("div").text("The width for the " + ele +
                " is " + w + "px.");
}
$("#getp").click(function () {
  showWidth("paragraph", $("p").width());
});
$("#getd").click(function () {
  showWidth("document", $(document).width());
});
$("#getw").click(function () {
  showWidth("window", $(window).width());
});
 
</script>
 
</body>
</html>

 

posted @ 2013-12-15 20:44  PiLee  阅读(254)  评论(0编辑  收藏  举报