jQuery 尺寸

通过 jQuery,很容易处理元素和浏览器窗口的尺寸。

jQuery 尺寸 方法

jQuery 提供多个处理尺寸的重要方法:

  • width()
  • height()
  • innerWidth()
  • innerHeight()
  • outerWidth()
  • outerHeight()

jQuery width() 和 height() 方法

width() 方法设置或返回元素的宽度(不包括内边距、边框或外边距)。

height() 方法设置或返回元素的高度(不包括内边距、边框或外边距)。

下面的例子返回指定的

元素的宽度和高度:

مثال

$("button").click(function(){
  var txt="";
  txt+="Width: " + $("#div1").width() + "
"; txt+="Height: " + $("#div1").height(); $("#div1").html(txt); });

جرب بنفسك

jQuery innerWidth() 和 innerHeight() 方法

innerWidth() 方法返回元素的宽度(包括内边距)。

innerHeight() 方法返回元素的高度(包括内边距)。

下面的例子返回指定的

元素的 inner-width/height:

مثال

$("button").click(function(){
  var txt="";
  txt+="Inner width: " + $("#div1").innerWidth() + "
"; txt+="Inner height: " + $("#div1").innerHeight(); $("#div1").html(txt); });

جرب بنفسك

jQuery outerWidth() و outerHeight() methods

يحصل على العرض (بما في ذلك الهوامش والحواف).

يحصل على الارتفاع (بما في ذلك الهوامش والحواف).

في المثال التالي، يتم العودة إلى outer-width/height المحددة للـ <div> العنصر المحدد:

مثال

$("button").click(function(){
  var txt="";
  txt+="Outer width: " + $("#div1").outerWidth() + "</br>";
  txt+="Outer height: " + $("#div1").outerHeight();
  $("#div1").html(txt);
});

جرب بنفسك

يحصل على العرض (بما في ذلك الهوامش، الحواف والجدارات).

يحصل على الارتفاع (بما في ذلك الهوامش، الحواف والجدارات).

مثال

$("button").click(function(){
  var txt="";
  txt+="Outer width (+margin): " + $("#div1").outerWidth(true) + "</br>";
  txt+="Outer height (+margin): " + $("#div1").outerHeight(true);
  $("#div1").html(txt);
});

جرب بنفسك

jQuery - المزيد من width() و height()

في المثال التالي، يتم العودة إلى العرض والارتفاع للوثيقة (وثيقة HTML) والنافذة (شاشة المتصفح):

مثال

$("button").click(function(){
  var txt="";
  txt+="Width/height of document: " + $(document).width();
  txt+="x" + $(document).height() + "\n";
  txt+="Width/height of window: " + $(window).width();
  txt+="x" + $(window).height();
  alert(txt);
});

جرب بنفسك

في المثال التالي، يتم تعيين العرض والارتفاع للـ <div> العنصر المحدد:

مثال

$("button").click(function(){
  $("#div1").width(500).height(500);
});

جرب بنفسك

دليل مراجعة jQuery CSS

لغا عن مراجعة كاملة لـ jQuery Dimensions، زرنا دليل مراجعة jQuery Dimensions.