jQuery - 带变量的Calc高度

时间:2017-08-04 14:44:46

标签: jquery css3

有没有办法在jQuery中使用calc?我正在尝试类似下面的内容,但它似乎没有效果。有谁知道这样做的正确方法吗?

var h = $('.mydiv').height();

$('.shopping-cart-table-container').css({ height: 'calc(100% - ' h')'});

2 个答案:

答案 0 :(得分:5)

是的,你有一点语法错误。

$('.shopping-cart-table-container').css( { height: 'calc(100% - ' + h + 'px)' } );

您还可以使用反引号使用新的模板字符串格式:

$('.shopping-cart-table-container').css( { height: `calc(100% - ${h}px)` } );

//更新:请记住,jQuerys height函数会返回没有单位的像素,因此您需要在css语句中添加它。

答案 1 :(得分:0)

很棒!!!我一直在寻找有关这些简单解决方案的日子。非常感谢你!下面,我显示我的代码进行了一些更改,但目标相同。


JS

var h = $('.alturainputs').height();

        $('.dynamic-height').DataTable( {
            "scrollY":        'calc(100vh - 350px - ' + h + 'px)',/*card-title-wrap bar-info height*/
            "scrollCollapse": true,
            "paging":         true
        } );

HTML第1部分

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     Assinatura                                                                                                                                                                                                                                                                                   Assinatura

HTML第2部分

                                                                                                                           名字                                     姓                                     位置                                     办公室                                     年龄                                     开始日期                                     薪水                                     扩展                                     电子邮件                                                                                                                                                                 虎                                     尼克松                                     系统架构师                                     爱丁堡                                     61                                     2011/04/25                                     $ 320,800                                     5421                                     t.nixon@datatables.net