我想使用 jQuery 将负数“padding-top”设置为 .container 元素。
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');
我想知道如果出现“负值”该怎么写。
最佳答案
填充不能为负数:Unlike margin properties, values for padding values cannot be negative.
CSS 盒子模型模块级别 3 规范:Negative values for padding properties are invalid .
关于javascript - 如何使用 jQuery 将负数 padding-top 添加到 html 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59692511/