简体   繁体   English

如何避免每次运行函数时都设置点击绑定事件

[英]How to avoid setting the on click bind event every time the function is ran

So I made this overlay function, and I've also made it close on click on the overlay its self. 因此,我做了这个叠加功能,并且也点击了叠加本身就使其关闭。 Problem is I bind the click event every time I run the function ( $overlay.click(function() {...} ), and I think this is bad for performance. Any ideas? 问题是我每次运行函数( $overlay.click(function() {...} )时都绑定click事件,我认为这样做对性能不利。

 function fluidOverlayShow(action, currentElement) { var $overlay = $('#fluid-overlay'); if (action == 'open') { $overlay.click(function() { emgFluidOverlayShow('close', currentElement); }); $(currentElement).addClass('fluid-bring-front'); $overlay.addClass('fluid-anim-overlay'); $overlay.data('statuson', true); } else if (action == 'close') { $overlay.removeClass('fluid-anim-overlay'); $overlay.data('statuson', false); $('.fluid-header').find('.fluid-bring-front').removeClass('fluid-bring-front'); } } $('#overlay_test').mouseover(function() { fluidOverlayShow('open', '#overlay_test'); }); $('#overlay_test').mouseout(function() { fluidOverlayShow('close'); }); 
 #fluid-overlay { display: none; opacity: 0.3; position: fixed; left: 0; top: 0; width: 100%; height: 100%; background: #000; z-index: 1000; } #overlay_test { position: relative; } #fluid-overlay.fluid-anim-overlay { display: block; -webkit-animation: fade-in-overlay 0.2s 1; -moz-animation: fade-in-overlay 0.2s 1; animation: fade-in-overlay 0.2s 1; } .fluid-bring-front { z-index: 1100; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <a href="javascript:;" id="overlay_test">Overlay test</a> <div id="fluid-overlay"></div> 

If you absolutely have to keep the click event in the function then use .on() and .off() and then define .off("click") before you define on("click") like this: 如果你绝对要保持click事件中的函数,然后使用.on().off()然后定义.off("click")定义之前on("click")是这样的:

$overlay.off("click").on("click", function() {
  emgFluidOverlayShow('close', currentElement);
});

This will remove the event binding before it adds it. 这将在添加事件绑定之前将其删除。

You can even namespace the click event like this so it only removes that instance of click (in case other events get added elsewhere): 您甚至可以像这样对click事件进行命名空间,以便仅删除该click实例(以防其他事件添加到其他位置):

$overlay.off("click.fluidOverlayClose").on("click.fluidOverlayClose", function() {
  emgFluidOverlayShow('close', currentElement);
});

Or... 要么...

...do as Guruprasad Rao suggests and move it out of the function (which is a much better way of handling it). ...按照Guruprasad Rao的建议进行操作,然后将其移出函数(这是处理它的更好方法)。

An alternative implementation uses jQuery's .on() method combined with event delgation . 另一种实现方式是将jQuery的.on()方法与事件派发结合使用 What his means is you allow the click event to bubble up through the DOM to a parent element that will always be there that will capture and process the event rather than having to rebind it to the dynamic element everytime it's created. 他的意思是,您允许click事件通过DOM冒泡到达父元素,该父元素将始终存在,该父元素将捕获并处理该事件,而不必在每次创建时将其重新绑定到动态元素。

This would look something like 这看起来像

 $("body").on("click","#fluid-overlay",function() {
     //code goes here
    });

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

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