javascript - Javascript 使用 Padding Aware 检测溢出
问题描述
我正在制作一个可以根据屏幕宽度在桌面版和移动版之间转换的导航栏。
@media (min-width: ${minWidth}px)
与用于切换的 Bootstrap 导航栏不同,
我通过比较&使用 JavaScript 进行溢出检测。$0.clientWidth
$0.scrollWidth
问题是,paddingRight
不减去$0.clientWidth
,因此检测不准确。
如 Chrome 的页面检查器所示,我想在没有填充的情况下获得实际的客户端宽度。
如图中绿色矩形所示:
笔记:
- 我不能通过添加
::after
填充右侧来应用 hack,因为容器是网格,从而导致难看的列间隙。 - 我无法将marginRight 应用于菜单,因为边距将填满gridArea。
这是我的导航栏的模型。
请关注 .js 文件:
查看和编辑我的沙箱
解决方案
If you are using list, i mean if your structure is like as
<ul>
<li>
<a href="#">
</a>
</li>
</ul>
Then give the padding
to the anchor tag, and then get the width of the li
tag, your problem will be resolve
推荐阅读
- spring-boot - 使用 Spring Boot 在运行时加载 Yml
- reactjs - Antd 中的 Markdown
- python - 在heroku上初始化Flask-SocketIO python应用
- javascript - 显示来自ajax的数据后阅读更多和更少不起作用
- laravel - 在一条记录中导入多个 id
- node.js - 是否可以在不安装模块的情况下在 NodeJS 中使用 DigestAuthentidfication?
- python - 内存未释放,即使对象引用计数为 0
- scala - DataFrame如何在包含多维数组的列中添加
- javascript - 从元素中获取文本
- python - Spacy:如何在特殊情况标记化规则列表中添加冒号字符