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

Posted 无敌小畅叙

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了监听窗口大小改变,同时根据窗口大小修改某个元素的大小相关的知识,希望对你有一定的参考价值。

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();
};

 

以上是关于监听窗口大小改变,同时根据窗口大小修改某个元素的大小的主要内容,如果未能解决你的问题,请参考以下文章