也许用一个小功能,检查在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事件。 –