各种height/width总结

本文深入探讨了网页滚动条与CSS盒模型之间的交互,通过JavaScript接口如clientHeight、scrollHeight等获取高度和宽度的细微区别,并提供计算滚动条宽度的方法。适合前端开发者学习。

CSS盒模型是比较复杂的,尤其是当页面中有滚动条时,仅仅通过css来操作高度宽度是不够的,幸运的是Javascript提供了不少这样的接口。Javascript中clientHeight / cliengWidth, scrollHeight /scrollWidth, offsetHeight / offsetWidth, height / width 都可以获取高度和宽度,但是他们有一些细微的差别:

  • offsetHeight / offsetWidth:文档整体区域包含滚动条和border,对于display:block的元素通过width/height + padding + border可以计算出来。
  • clientHeight / cliengWidth:可见区域包含padding,不包含border和滚动条,不能通过CSS样式计算出来,取决于滚动条的大小。
  • scrollHeight / scrollWidth:内容区域的大小,不包含border,包含不在可见区域内的隐藏部分,不能通过CSS样式计算出来。
  • height / width:不包含borderpadding

另外:滚动条的宽度可以通过如下方式计算:

scrollbarWidth = offsetWidth - clientWidth - getComputedStyle().borderLeftWidth - getComputedStyle().borderRightWidth  

 

转载于:https://www.cnblogs.com/shytong/p/5043573.html

size(800, 600); background(200); boolean isFirstVersion = true; // 标记当前显示的版本 void setup() { surface.setTitle("鼠标点击切换表情"); } void draw() { background(200); // 灰色背景 int hairColor = color(200, 150, 80); // 黄棕色 int clothesColor = color(187, 255, 255); // 天蓝色衣服 if (isFirstVersion) { // 第一个版本的代码 fill(hairColor); noStroke(); ellipse(width/2, height/2 - 50, 200, 200); // 圆形头发 rectMode(CENTER); rect(width/2, height/2 + 50, 200, 200); // 长方形头发 // 绘制脸部 fill(255, 230, 210); // 肤色 ellipse(width/2, height/2, 150, 180); // 脸部椭圆 // 眼睛(保持不变) fill(255); // 眼白 ellipse(width/2 - 40, height/2 - 30, 25, 20); // 左眼白 ellipse(width/2 + 40, height/2 - 30, 25, 20); // 右眼白 fill(160, 80, 40); // 棕色虹膜 ellipse(width/2 - 40, height/2 - 30, 12, 10); // 左虹膜 ellipse(width/2 + 40, height/2 - 30, 12, 10); // 右虹膜 fill(0); // 黑色瞳孔 ellipse(width/2 - 40, height/2 - 30, 8, 8); // 左瞳孔 ellipse(width/2 + 40, height/2 - 30, 8, 8); // 右瞳孔 // 眉毛 strokeWeight(2); line(width/2 - 50, height/2 - 50, width/2 - 30, height/2 - 40); // 左眉 line(width/2 + 30, height/2 - 50, width/2 + 50, height/2 - 40); // 右眉 noStroke(); // 鼻子 fill(180, 140, 120); // 浅棕色 triangle( width/2, height/2 - 10, // 鼻尖Y坐标从-20改为-10(下移10像素) width/2 - 10, height/2, width/2 + 10, height/2 ); // 嘴巴(椭圆上半圆) fill(255, 180, 180); // 淡粉色 ellipseMode(CENTER); arc(width/2, height/2 + 20, 60, 30, 0, PI); // 上半圆(0到PI弧度) // 眉毛(黑色椭圆下半圆) fill(0); // 黑色 ellipseMode(CENTER); // 左眉毛:椭圆下半圆 arc(width/2 - 40, height/2 - 45, 30, 15, PI, TWO_PI); // 中心在左眼上方 // 右眉毛:椭圆下半圆 arc(width/2 + 40, height/2 - 45, 30, 15, PI, TWO_PI); // 对称位置 // 绘制衣服 fill(187,255,255); noStroke(); rectMode(CORNER); rect(width/2 - 60, height/2 + 80, 120, 150); // 衣服 } else { // 第二个版本的代码 // 绘制头发 fill(hairColor); noStroke(); ellipse(width/2, height/2 - 50, 200, 200); rectMode(CENTER); rect(width/2, height/2 + 50, 200, 200); // 绘制脸部 fill(255, 230, 210); ellipse(width/2, height/2, 150, 180); // 眼睛(增大黑色瞳孔) fill(255); // 眼白 ellipse(width/2 - 40, height/2 - 30, 30, 25); // 左眼白 ellipse(width/2 + 40, height/2 - 30, 30, 25); // 右眼白 fill(160, 80, 40); // 棕色虹膜 ellipse(width/2 - 40, height/2 - 30, 20, 18); ellipse(width/2 + 40, height/2 - 30, 20, 18); fill(0); // 黑色瞳孔 ellipse(width/2 - 40, height/2 - 30, 15, 15); ellipse(width/2 + 40, height/2 - 30, 15, 15); // 眉毛(保持不变) strokeWeight(2); line(width/2 - 50, height/2 - 50, width/2 - 30, height/2 - 40); line(width/2 + 30, height/2 - 50, width/2 + 50, height/2 - 40); noStroke(); // 鼻子 fill(180, 140, 120); triangle( width/2, height/2 - 10, width/2 - 10, height/2, width/2 + 10, height/2 ); // 嘴巴(椭圆) fill(255, 180, 180); ellipseMode(CENTER); ellipse(width/2, height/2 + 25, 60, 40); // 衣服 fill(clothesColor); rectMode(CORNER); rect(width/2 - 60, height/2 + 80, 120, 150); // 眉毛(黑色椭圆下半圆,微微抬起) fill(0); ellipseMode(CENTER); arc(width/2 - 40, height/2 - 50, 40, 12, PI, TWO_PI); arc(width/2 + 40, height/2 - 50, 40, 12, PI, TWO_PI); } } void mousePressed() { isFirstVersion =!isFirstVersion; } size(800, 600); background(200); // 灰色背景 int hairColor = color(200, 150, 80); // 黄棕色 // 绘制头发 fill(hairColor); noStroke(); ellipse(width/2, height/2 - 50, 200, 200); // 圆形头发 rectMode(CENTER); rect(width/2, height/2 + 50, 200, 200); // 长方形头发 // 绘制脸部 fill(255, 230, 210); // 肤色 ellipse(width/2, height/2, 150, 180); // 脸部椭圆 // ====================== // 眼睛(保持不变) // ====================== fill(255); // 眼白 ellipse(width/2 - 40, height/2 - 30, 25, 20); // 左眼白 ellipse(width/2 + 40, height/2 - 30, 25, 20); // 右眼白 fill(160, 80, 40); // 棕色虹膜 ellipse(width/2 - 40, height/2 - 30, 12, 10); // 左虹膜 ellipse(width/2 + 40, height/2 - 30, 12, 10); // 右虹膜 fill(0); // 黑色瞳孔 ellipse(width/2 - 40, height/2 - 30, 8, 8); // 左瞳孔 ellipse(width/2 + 40, height/2 - 30, 8, 8); // 右瞳孔 // 眉毛 strokeWeight(2); line(width/2 - 50, height/2 - 50, width/2 - 30, height/2 - 40); // 左眉 line(width/2 + 30, height/2 - 50, width/2 + 50, height/2 - 40); // 右眉 noStroke(); // ====================== fill(180, 140, 120); // 浅棕色 triangle( width/2, height/2 - 10, // 鼻尖Y坐标从-20改为-10(下移10像素) width/2 - 10, height/2, width/2 + 10, height/2 ); // ====================== // 嘴巴(椭圆上半圆) // ====================== fill(255, 180, 180); // 淡粉色 ellipseMode(CENTER); arc(width/2, height/2 + 20, 60, 30, 0, PI); // 上半圆(0到PI弧度) // ====================== // 眉毛(黑色椭圆下半圆) // ====================== fill(0); // 黑色 ellipseMode(CENTER); // 左眉毛:椭圆下半圆 arc(width/2 - 40, height/2 - 45, 30, 15, PI, TWO_PI); // 中心在左眼上方,宽30,高15 // 右眉毛:椭圆下半圆 arc(width/2 + 40, height/2 - 45, 30, 15, PI, TWO_PI); // 对称位置 // ====================== // 绘制衣服(覆盖部分脸部) // ====================== fill(187,255,255); noStroke(); rectMode(CORNER); rect( width/2 - 60, // 左边界:不变 height/2 + 80, // 上边界:从+60改为+80,向下移动20像素 120, // 宽度:120像素(比脸部窄) 150 // 高度:150像素 ); 当鼠标点击时,让这两个表情切换 帮我生成一段processing能用的代码
最新发布
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值