admin管理员组文章数量:1415119
I'm trying to create a listener kind of thing in jQuery. What I want to do is check all the time, if an divs margin-left == 200px and then fire an event. But I have no idea how to do that. Maybe it's better, that the div calls the event function, when it's margin-left == 200px, but I'm not even sure, if that's possible.
Any help will be very appreciated.
I'm trying to create a listener kind of thing in jQuery. What I want to do is check all the time, if an divs margin-left == 200px and then fire an event. But I have no idea how to do that. Maybe it's better, that the div calls the event function, when it's margin-left == 200px, but I'm not even sure, if that's possible.
Any help will be very appreciated.
Share Improve this question asked Oct 24, 2011 at 12:28 Johannes KlaußJohannes Klauß 11.1k18 gold badges71 silver badges127 bronze badges 1- 2 Possible duplicate of stackoverflow./questions/1397251/… – etuardu Commented Oct 24, 2011 at 12:39
2 Answers
Reset to default 4The following function will check every 1 second whether an element with the class elementClass
has a margin-left
set as 200px
. If so, an alert
will be triggered (as an example).
$(document).ready(function(){
setInterval(function(){
if ($(".elementClass").css("marginLeft")=='200px'){
//do something here
alert("margin is 200px");
}
}, 1000);
});
However, this code will then trigger the event every second that the margin-left
is 200px
. The following will only trigger the event the first time the element has been detected with the 200px margin-left:
var eventtrig = 0;
$(document).ready(function(){
setInterval(function(){
if ($(".elementClass").css("marginLeft")=='200px' && eventtrig=0) {
//do something here
alert("margin is 200px");
eventtrig=1;
}
else {
eventtrig=0;
}
}, 1000);
});
You can check "all the time" by doing a setinterval( ) of say 1 second and then, in the handler for the clock event, check the div's left margin and perhaps alert( ) when/if it ever gets to 200.
EDIT: as a point of information: the process of checking every once in a while -- i.e., every second -- is called "polling."
本文标签: javascriptFire event if marginleft200pxStack Overflow
版权声明:本文标题:javascript - Fire event if margin-left == 200px; - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745206566a2647666.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论