javascript - 如何使用 jQuery 向 html 元素添加负填充顶部
问题描述
我想使用 jQuery 为 .container 元素设置负数“padding-top”。
padding-top 的值将是 .header 元素和 .navigation 元素的总和。
我尝试了下面的代码,但没有奏效。
//This don't work
const $headerHeight = $('.header').height();
const $navHeight = $('.nav').height();
$('.container').css('padding-top', -($headerHeight + $navHeight) + 'px');
它在不是负值时起作用。
它将 style="padding-top: xxxpx" 添加到 .container 如下
div class="container" style="padding-top: xxxpx"
//This works
const $headerHeight = $('.header').height();
const $navHeight = $('.nav').height();
$('.container').css('padding-top', ($headerHeight + $navHeight) + 'px');
我想知道在“负值”的情况下如何写。
解决方案
填充不能为负:与边距属性不同,填充值的值不能为负。
CSS Box Model Module Level 3 specs: Negative values for padding properties are invalid。
推荐阅读
- laravel - Laravel:更新作曲家错误并创建项目
- android - 使用 Gluon-mobile ShareService 从 android 上的应用程序打开 PDF 时出现异常
- twig - ServiceNotFoundException:依赖于不存在的服务“twig”
- javascript - 如何将通过 javascript 编码为 base64 字符串的文件转换为 Ruby 文件对象?
- go - 构建Android时在golang中读取Makefile变量
- ansible - Ansible在dict中组合数组
- sql - 在 SQL 中从 JSON 中提取数据
- apache-spark - 使用 Spark 访问 s3a 时出现 403 错误
- regex - 基于多种模式从 Postgres 表中获取字符串
- gitlab - 从另一个管道 gitlab 触发管道