当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 监听窗口大小改变,同时根据窗口大小修改某个元素的大小

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

2018年11月15日  | 移动技术网IT编程  | 我要评论

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

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网