简体   繁体   中英

jQuery - Automatic change values after resizing the browser window

We have following code (addition, the presence of the plugin DataTables)

$('#myTable').dataTable( {
      "scrollY":  height, <-- auto height here
      "scrollCollapse": true,
} );

I dont' know so well jquery and I have a problem with the automatic changing values.

I have a div element where the height is a percentage (for example: 50%) and and I want to get this height in pixels. Ofcourse the height is changing as many times as the browser window is changed.

Unfortunately, I don't know how to do, Someone here can help? (preferably an example)

Update 1:

I tried so like that

    var ch = $('#dataTableWrapper').height() - 110; // I subtracted the value of the height of my static elements in a div 

    $('#dataTableID').dataTable( {
        "scrollY": ch,
        "scrollCollapse": true,
    } );

    $(window).resize(function(){
        ch = $('#dataTableWrapper').height() - 110; // same as above
        $('.dataTables_scrollBody').css('height', ch);
    });

Apparently it works, but if anyone had a more elegant solution I would ask about throwing code.

You can do like this:

$( window ).resize(function() {
   var scroll = $("#yourDiv").height();
   });

This might be a dumb thing to suggest, but are you just looking for a simple refactoring?

function getTableHeight() {
    return $('#dataTableWrapper').height() - 110;
}

$('#dataTableID').dataTable( {
    "scrollY": getTableHeight(),
    "scrollCollapse": true,
} );

$(window).resize(function(){
    $('.dataTables_scrollBody').css('height', getTableHeight());
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM