也許用一個小功能,檢查在onResize如果元素的寬度發生了變化。調整瀏覽器大小並查看控制檯日誌結果。
$(window).resize(function(){
var obj = $('#at')[0];
if(detectResize(obj)) console.log('resize detected');
});
function detectResize(getObject){
var getWidth = getObject.getBoundingClientRect().width;
if(getObject.currentWidth===getWidth){
return false;
} else {
getObject.currentWidth = getWidth;
return true;
}
}
#at{
width:100%;
max-width:400px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="at" style="background:red">
This is a div, you can click on me or resize me.
</div>
我知道$(窗口)只使用來調整,但在你的情況下,要素要調整大小事件。我也在尋找解決方案 –
我相信resize事件只能在窗口對象上使用,所以你需要使用'$(window).resize()',但它看起來更像是你希望當用戶改變元素的大小直接? – Jhecht
是的,正是我想要一個元素上的resize事件。 –