Minus a few pixels from jQuery height()

后端 未结 4 1343
旧时难觅i
旧时难觅i 2021-01-29 15:35

Ok, amateur question here but im really bumping my head.

Using the jQuery below, i would like to add margin-top:-100px; to the height value.

Please help! Thanks

相关标签:
4条回答
  • 2021-01-29 16:13

    You can just add the css.

    $('#home').css('margin-top','-100px');
    
    0 讨论(0)
  • 2021-01-29 16:16

    If I understand correctly this should do it:

    $(function () {
    function HomePageSize() {
        var winHeight = $(window).height(); // store height in variable
        var myMargin = winHeight - 100; // deduct 100 from height
    
        $('#home').css({
            width: $(window).width(),
            height: winHeight,
            marginTop: myMargin // assign calculated value to margin-top
        });
    }
    $(window).resize(function () {
        HomePageSize();
    });
    HomePageSize();
    });
    
    0 讨论(0)
  • 2021-01-29 16:21

    If you already have margin-top of -100px assigned to some element like #home, then You can try this

    $(function () {
    function HomePageSize() {
    $('#home').css({
        width: $(window).width(),
        height:  $(window).height() + parseInt($("#home").css("margin-top"))
    });
    }
    $(window).resize(function () {
     HomePageSize();
    });
     HomePageSize();
    });
    
    0 讨论(0)
  • 2021-01-29 16:24

    Here code:

    $(function () {
    
       function HomePageSize() {
           $('#home').css({
               width: $(window).width(),
               height: $(window).height()
           });
       }
    
       $(window).resize(function () {
           HomePageSize();
       });
    
      addMargin();
    
      function addMargin() {
           $('#home').css({'margin-top':'-100px'});
      }
    
    });
    
    0 讨论(0)
提交回复
热议问题