BIG陽
2019-04-26 12:15:26
當(dāng)我單擊瀏覽器窗口的“最大化”按鈕時(shí),我編寫的一個(gè)函數(shù)將在調(diào)整窗口大小時(shí)執(zhí)行,但無法正常工作。當(dāng)我使用鼠標(biāo)調(diào)整窗口大小時(shí),它工作。<div class="div1"> <div class="div2"></div></div>.div1 { position: relative; }.div2 { position: absolute; width: 300px; top: 0; transition: all 0.5s ease; }@media only screen and (max-width: 1024px) { .div2 { width: 200px; } }function aFuntion() { let div1 = document.querySelector('.div1'); let div2 = document.querySelector('.div2'); let diff = div1.clientHeight - div2.clientHeight; div1.style.top = -diff + 10 + 'px';};// Call the function.// This worked!aFuntion();// Call the function when users resize the window.// This worked!window.addEventListener('resize', aFuntion);// Users click on the Maximize button of the window.// The function does not work!
3 回答

森欄
TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊
我已經(jīng)在Firefox,Chrome和Internet Explorer中測試了下面的代碼,它正在按預(yù)期工作。我知道這是一種解決方法,因?yàn)槭录菑腷ody標(biāo)簽觸發(fā)的,但也許它會對你有所幫助。
<html>
<head>
<script>
function resizeFunction() {
console.log("working");
}
</script>
</head>
<body onresize="resizeFunction()">
content
</body>
</html>

慕婉清6462132
TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊
好的。我解決了自己的問題。以某種方式過渡:全部0.5秒輕松; 調(diào)整大小不起作用。我刪除了轉(zhuǎn)換:所有0.5秒輕松; 它起作用了。
添加回答
舉報(bào)
0/150
提交
取消