Bind the position of an element to another element's position

这一生的挚爱 提交于 2019-12-11 06:46:25

问题


Is there any way in jQuery to make an element stay at the same position as another element?

Say there is a textbox and a <span>. How would one let the <span> always overlap the textbox, i.e. have it bound at the same position? Also, when the textbox is moving for any reason, I'd like to have the <span> go along.

I was thinking of a setInterval which checks a certain number of times a second and move the <span> appropriately, however this is both expensive and a dirty trick in my opinion.

Thanks for any tips.


回答1:


Unless I am misunderstanding your question, you could put the <span> and the <input type="text" /> in the same parent container, such as a <div>, then move the <div> instead of the <input type="text" />.




回答2:


If you control the times when the textbox moves, you can also trigger the movement of the span. If not, then I'm unaware of any events that would work better than your setInterval idea.



来源:https://stackoverflow.com/questions/5437918/bind-the-position-of-an-element-to-another-elements-position

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