【问题标题】:How to add negative padding-top to html element using jQuery [duplicate]如何使用jQuery向html元素添加负填充顶部[重复]
【发布时间】:2020-04-28 17:50:14
【问题描述】:

我想使用 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');

我想知道“负值”的情况下怎么写。

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    【讨论】:

    • 我不知道。非常感谢您的帮助。
    猜你喜欢
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 2014-09-14
    相关资源
    最近更新 更多