你有沒有看着$(window).resize(function(e) { /* do re-sizing work */ })
爲了進一步擴展:
我得先說,沒有太多的理由對你的div寬度爲100%,除非寬度被重置動態別的地方不應該工作,或者如果父級具有設置的寬度。
但如果你必須有它可以做只是一種方式jQuery的例子(有很多),這裏亞去:
// simple way to start jQuery, similar to `window.onload = function(event) {`
$(function() {
// Establish a simple timer var to make sure code doesnt run like a wild hamster on sesame seed oil
var tmrResize;
// jQuery way to call `window.onresize = function(event) {`
$(window).resize(function(e) {
// simply clear timer, no need for if statement, if it's undefined, its cleared
clearTimeout(tmrResize);
// set timer for 1/4 of a second which outa be enough time to know someone is done resizing a window,
// the longer the timer, the more glitchy it will appear in the end
// and fyi, i see alot of people shorten timers by replacing the function call,
// example ( setTimeout(myFunction, 250); )
// i can tell ya from experience, such method is not "cross-browser" friendly
// just simply put your function, if external, inside the function call
tmrResize = setTimeout(function() {
// jQuery standard similar to document.getElementsByTagName("div").style.width = screen.width
$("div").width($(document).width());
}, 250);
});
});
另請參見:
$(window).resize(function(){}); – 2012-03-21 17:01:21