简体   繁体   English

如果margin-left == 200px,则发生火灾事件;

[英]Fire event if margin-left == 200px;

I'm trying to create a listener kind of thing in jQuery. 我正在尝试在jQuery中创建一个监听器类的东西。 What I want to do is check all the time, if an divs margin-left == 200px and then fire an event. 我想要做的是一直检查,如果一个divs margin-left == 200px然后发起一个事件。 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. 也许它更好,div调用事件函数,当它的margin-left == 200px时,但我甚至不确定,如果可能的话。

Any help will be very appreciated. 任何帮助将非常感激。

The following function will check every 1 second whether an element with the class elementClass has a margin-left set as 200px . 以下函数将每1秒检查一个具有类elementClass的元素是否具有margin-left设置为200px If so, an alert will be triggered (as an example). 如果是这样,将触发alert (作为示例)。

$(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 . 但是,此代码将每秒触发事件,即margin-left200px The following will only trigger the event the first time the element has been detected with the 200px margin-left: 以下内容仅在第一次使用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. 您可以通过执行1秒钟的setinterval()来检查“所有时间”,然后在时钟事件的处理程序中检查div的左边距并且可能在/如果它达到200时警告()。

EDIT: as a point of information: the process of checking every once in a while -- ie, every second -- is called "polling." 编辑:作为一个信息点:每隔一段时间检查一次 - 即每秒 - 被称为“轮询”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM