日常记录:区分不同box-sizing下jq与原生js获取宽度和绝对定位left值的差异性

本文探讨了`box-sizing`属性在不同库(JQuery和原生JS)中的行为差异,重点讲解了content-box与border-box模式下宽度计算,以及定位时left值的计算方法。作者强调了在使用时对边距、内边距和边框的注意,并揭示了JQuery中的position()方法与原生获取left值的不同之处。

(默认) box-sizing: content-box;

//Jq
$('#ele').width();      //盒子content宽度
$('#ele').innerWidth();      //盒子content + padding
$('#ele').outerWidth();      //盒子content + padding + border
$('#ele').outerWidth(true);      //盒子content + padding + border + margin
//js
$('#ele')[0].offsetWidth   //盒子content宽度

box-sizing: border-box

$('#ele').width();      //盒子content宽度
$('#ele').innerWidth();      //盒子content + padding
$('#ele').outerWidth();      //盒子content + padding + border
$('#ele').outerWidth(true);      //盒子content + padding + border + margin
//js
$('#ele')[0].offsetWidth   //盒子content + padding + border宽度

对于获取绝对定位left值而言

//jQ
$('#ele').position().left        //margin处开始计算(不受margin影响)
//js
$('#ele')[0].offsetLeft        //border处开始计算(随margin变化而变化)
//假如设置left: 0, margin: 20
//则 jq获取值为 0, js为20 
//赋值时两效果一样,但获取的内容不一样
$('#ele')[0].style.left = '0px'
$('#ele').css('left', '0px')

总体而言,Jq的取值方式没有变化,原生js则需要注意border和padding问题;同理,对应的赋值问题也需要注意;对于position定位,jQ的left值不受margin值影响,及赋值多少,获取就是多少,而原生js获取的值等于赋值时的值加上margin-left.

<div style="max-width: 775px; margin: 0 auto; background-color: #ffffff; overflow: hidden; box-sizing: border-box;"> <div style="background-color: #141414; padding: 80px; padding-bottom: 80px; color: #ffffff; box-sizing: border-box; position: relative;"> <div style="margin-bottom: 48px; box-sizing: border-box;"> <img src="https://test-static.playol.com/static/imgs/header.png" alt="PlayOL Logo" style="width: 100px; height: 100px; display: block; box-sizing: border-box;"> </div> <div style="font-size: 36px; font-weight: 600; margin-bottom: 36px; color: #BFBFBF; box-sizing: border-box; line-height: 1.362; height: 49px;"> ${email}, 您好! </div> <div style="font-size: 36px; font-weight: 600; margin-bottom: 36px; color: #FFFFFF; box-sizing: border-box; line-height: 1.362; height: 98px;"> 欢迎来到PlayOL,<br> 这是您的验证码 </div> <div style="font-size: 36px; font-weight: 600; margin-bottom: 24px; color: #FFFFFF; box-sizing: border-box; line-height: 1.362; height: 49px;"> ${code} </div> <div style="font-size: 14px; font-weight: 400; color: #FFFFFF; text-align: left; line-height: 1.362; box-sizing: border-box; margin-bottom: 80px; height: 19px;"> 验证码有效期为5分钟,请勿将验证码透露给他人。如非本人操作,您可以忽略这封邮件。 </div> <div style="padding-left: 24px; border-left: 4px solid #FF1F1F; box-sizing: border-box; height: 80px; display: flex;flex-direction: column;justify-content: center;"> <div style="font-size: 14px; box-sizing: border-box;">祝您游戏愉快!</div> <div style="font-size: 14px; font-weight: 600; box-sizing: border-box;">PlayOL团队</div> </div> </div> <div style="background-color: #ffffff; padding: 60px; padding-left: 80px; padding-right: 80px; color: #000000; box-sizing: border-box;"> <div style="font-size: 14px; font-weight: 400; margin-bottom: 20px; text-align: left; box-sizing: border-box; line-height: 1.4; color: #000000;"> 这封电子邮件由系统自动生成,请勿回复。 </div> <div style="text-align: left; margin-bottom: 50px; box-sizing: border-box;"> <div style="font-size: 14px; font-weight: 400; margin-bottom: 5px; color: #000000; line-height: 1.4; box-sizing: border-box; font-family: 'PingFang SC', sans-serif;"> 游玩网址</div> <a href="https://playol.com" target="_blank" style="text-decoration: none; box-sizing: border-box; font-size: 18px; font-weight: 400; color: #000000; line-height: 1.4; font-family: 'PingFang SC', sans-serif;">https://playol.com</a> </div> <div style="display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 24px; box-sizing: border-box;"> <div style="flex: 1; box-sizing: border-box;"> <div style="text-align: left; margin-bottom: 24px; box-sizing: border-box;"> <div style="display: inline-flex; align-items: center; gap: 10px; box-sizing: border-box;"> <img src="https://test-static.playol.com/static/imgs/logo.png" alt="PlayOL Logo" style="width: 160px; height: 48px; box-sizing: border-box;"> </div> </div> <div style="text-align: left; font-size: 12px; font-weight: 400; line-height: 1.4; box-sizing: border-box; color: #000000; font-family: 'PingFang SC', sans-serif;"> <div style="margin-bottom: 8px; box-sizing: border-box;"><span style="font-weight: 700;">© PalyOL Corporation. All Rights Reserved.</span></div> <div style="box-sizing: border-box;">保留所有权利。所有商标均为其在x国及其他国家/地区的各自持有者所有。</div> </div> </div> </div> </div> </div>每一行的末尾都加上了反斜杠 \
11-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值