2017-06-29 74 views
1

我收听resize事件,但我需要扩展它,因为我在全局使用它。在调用此事件之前,我需要对传球进行一些额外的检查。我怎样才能做到这一点?如何扩展'resize'事件?

+0

本次活动将调用浏览器的尺寸调整,把你的条件,这里面做或不做的东西 – Huangism

+0

你不“来电”像这样的事件。它会自动发生并由浏览器自己调用。您不需要中断该过程,只需在事件处理程序中添加自己的条件逻辑即可。 –

回答

0

我要开始说我不完全确定你在问什么,但希望这会有所帮助。在JavaScript中,你不会'调用'像调整大小一样的事件。你倾听它,当它发生时,你会做一些事情。

例如,如果(我想你的意思),你想一旦窗口已调整大小和几个条件已经具备做一些事情,尝试这样的事情:

<html> 
<body onresize = 'myFunction(condition1, condition2, condition3)'> 
    <div id = 'content'> 
     <!--your content here--> 
    </div> 
</body> 

,然后你的JavaScript:

function myFunction(condition1, condition2, condition3){ 
    if(condition1 && condition2 && condition3){ 
     //code that will execute once resize event fires 
     //and all three conditions have been met 
     console.log("Resize event detected, all three conditions have been met"); 
     //your code here 
    }; 
} 

这是你在找什么?

您也可以尝试这样的事:

function myFunction(cond1, cond2, cond3(){ 
    if(cond1 && cond2 && cond3){ 
     //code that will execute once resize event fires 
     //and all three conditions have been met 
     console.log("Resize event detected, all three conditions have been met"); 
     //your code here 
    }; 
}  
window.addEventListener("resize", myFunction(cond1, cond2, cond3);