jQuery event to detect when element position changes

南楼画角 提交于 2019-12-18 03:03:14

问题


I would like to know if there is a jQuery event that I can use to determine when a particular DIV's top property has changed.

For instance, I have invisible content above a DIV. When that content becomes visible, the DIV is shifted down. I would like to capture that event and then use the offset() function to get the X/Y coordinates.


回答1:


The easy answer is that there are no events in the DOM for detecting layout updates.

You have a couple options the way I see it:

  1. Poll, nasty but it may work depending on your update frequency requirements.

  2. Tap into whatever event causes the invisible DIV to change size and do whatever you need to do in that handler


I shall correct myself.

I took a look at the DOM and noticed the DOMAttrModified event and found this JQuery Plug-In that you might be able to leverage to do what you want.

As the article mentions, it works great in IE and Firefox but seems to have problems in WebKit.




回答2:


I thiiink you should be able to do:

$(document).ready( function (){
  $("#mydiv").bind("movestart", function (){ ...remember start position... });
  $("#mydiv").bind("moveend", function (){ ...calculate offsets etc... });
});



回答3:


$("#someId").resize(function () {

// your code

});


来源:https://stackoverflow.com/questions/355015/jquery-event-to-detect-when-element-position-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!