Scroll event for Meteor

后端 未结 5 1698
没有蜡笔的小新
没有蜡笔的小新 2021-02-07 20:29

I couldn\'t find a scroll event for meteor in the meteor docs. How do I go about doing something as someone scrolls the window down in a meteor application?

I\'ve tried

5条回答
  •  情深已故
    2021-02-07 21:21

    In Meteor there is no native template support for scroll events, so you have to do within your Template.name.onRendered callback. However, you will get a memory leak if you don't remove it from Template.name.onDestroyed. This is best accomplished with namespaced events, since something like $(window).off('scroll'); will detach all scroll events from window.

    import { $ } from 'jquery';
    
    Template.myTemplateName.onRendered(function(){
      // You can do this multiple times
      $(window).on('scroll.whateverNamespace', function() { ... });
      $(window).on('scroll.whateverNamespace', function() { ... });
    })
    
    Template.myTemplateName.onDestroyed(function(){
      $(window).off('scroll.whateverNamespace');
    })
    

提交回复
热议问题