首页 > 解决方案 > 如何使用 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');

我想知道在“负值”的情况下如何写。

标签: javascriptjquery

解决方案



推荐阅读