There is a task - to make the block invisible when scrolling.

function closeBox() { $('#block').css('display', 'none'); } $('#block').scroll(function(){ close(); }); 

So it looks inefficient. When scrolling the page, the handler is constantly called.

You can somehow dynamically attach scroll(function(){}) to the #block element when displaying ( .css('display', 'block') ) and remove the handler after closing ( .css('display', 'none') )?

    1 answer 1

    Use the one function

    After the first execution, the handler will be removed immediately.

     function closeBox() { $('#block').css('display', 'none'); } $('#block')).one("scroll", function() { close();; });