前端必知必会- jQuery - 尺寸函数


jQuery - 尺寸

使用 jQuery,可以轻松处理元素和浏览器窗口的尺寸。

jQuery 尺寸方法
jQuery 有几种处理尺寸的重要方法:

  • width()
  • height()
  • innerWidth()
  • innerHeight()
  • outerWidth()
  • outerHeight()
    在这里插入图片描述

jQuery width() 和 height() 方法

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

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

以下示例返回指定 <div> 元素的宽度和高度:

示例

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

jQuery innerWidth() 和 innerHeight() 方法

innerWidth() 方法返回元素的宽度(包括填充)。

innerHeight() 方法返回元素的高度(包括填充)。

以下示例返回指定 <div> 元素的内部宽度/高度:

示例

$("button").click(function(){
var txt = "";
txt += "内部宽度: " + $("#div1").innerWidth() + "</br>";
txt += "内部高度: " + $("#div1").innerHeight();
$("#div1").html(txt);
});

jQuery outerWidth() 和 outerHeight() 方法

outerWidth() 方法返回元素的宽度(包括填充和边框)。

outerHeight() 方法返回元素的高度(包括填充和边框)。

以下示例返回指定 <div> 元素的外部宽度/高度:

示例

$("button").click(function(){
var txt = "";
txt += "外部宽度: " + $("#div1").outerWidth() + "</br>";
txt += "外部高度: " + $("#div1").outerHeight();
$("#div1").html(txt);
});

outerWidth(true) 方法返回元素的宽度(包括填充、边框和边距)。

outerHeight(true) 方法返回元素的高度(包括填充、边框和边距)。

示例

$("button").click(function(){
var txt = "";
txt += "外部宽度 (+margin): " + $("#div1").outerWidth(true) + "</br>";
txt += "外部高度 (+margin): " + $("#div1").outerHeight(true);
$("#div1").html(txt);
});

jQuery 更多 width() 和 height()

以下示例返回文档(HTML 文档)和窗口(浏览器视口)的宽度和高度:

示例

$("button").click(function(){
var txt = "";
txt += "文档宽度/高度: " + $(document).width();
txt += "x" + $(document).height() + "\n";
txt += "窗口宽度/高度: " + $(window).width();
txt += "x" + $(window).height();
alert(txt);
});

以下示例设置指定 <div> 元素的宽度和高度:

示例

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

总结

本文介绍了jQuery - 尺寸函数使用,如有问题欢迎私信和评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程岁月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值