窗口缩放自动显示横向滚动条
window.onresize = function(){ //事件会在窗口或框架被调整大小时发生 if(document.body.offsetWidth<300){ //body宽度 $("body").css("overflow-x","auto"); //设置body横向滚动条 }else{ $("body").css("overflow-x","");//取消滚动条 } }
当body宽度<300px,会出现横向滚动条
window.onresize会多次触发,优化:
var resizeTimer = null; function doResize(){ if(document.body.offsetWidth<300){ $("body").css("overflow-x","auto"); }else{ $("body").css("overflow-x",""); } } window.onresize = function(){ if(resizeTimer) clearTimeout(resizeTimer);//取消延时执行 resizeTimer = setTimeout("doResize()",100);//延时执行 }
setTimeout