[英]jQuery - Automatic change values after resizing the browser window
我們有以下代碼(此外,插件DataTables的存在)
$('#myTable').dataTable( {
"scrollY": height, <-- auto height here
"scrollCollapse": true,
} );
我不太了解jquery,並且自動更改值有問題。
我有一個div元素,其中的高度是百分比(例如:50%),並且我想以像素為單位獲取此高度。 當然,高度是隨着瀏覽器窗口的改變而改變的。
不幸的是,我不知道該怎么辦,這里有人可以幫忙嗎? (最好是一個例子)
更新1:
我這樣嘗試過
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);
});
顯然,它可以工作,但是如果有人有一個更優雅的解決方案,我會問有關拋出代碼的問題。
您可以這樣:
$( window ).resize(function() {
var scroll = $("#yourDiv").height();
});
這可能是愚蠢的建議,但是您是否只是在尋找簡單的重構?
function getTableHeight() {
return $('#dataTableWrapper').height() - 110;
}
$('#dataTableID').dataTable( {
"scrollY": getTableHeight(),
"scrollCollapse": true,
} );
$(window).resize(function(){
$('.dataTables_scrollBody').css('height', getTableHeight());
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.