监听窗口大小改变,同时根据窗口大小修改某个元素的大小

jQuery的方法:

<script>  
    $(window).resize(function(){  
        var width = $(this).width();  
        var height = $(this).height();  
        alert(‘width’+width+’-height’+height);  
    });  
</script>    

 以上的方法,不能写在页面加载完成事件函数$(function(){})内部,而需要写在外面。

页面加载完成事件:

$(function(){});

$(document).ready(function(){});

window.onload = function(){};

我们项目中的代码:

// 监听窗口大小变化
function changeHeight(){
    let h = document.documentElement.clientHeight;
    document.getElementById("searchResult").style.height = h - 9 - 74 + 'px';
    document.getElementById("goodsTableBody").style.height = h - 9 - 74 -160 + 'px';
}
window.onload = function(){
    changeHeight();
};
window.onresize = function(){
    changeHeight();
};

猜你喜欢

转载自www.cnblogs.com/zoeeying/p/9960242.html