Scroll event for Meteor

后端 未结 5 1696
没有蜡笔的小新
没有蜡笔的小新 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

    This is a bit late but I came up with a solution; at least in the context of my current project.

    I'm implementing D3 with Meteor, and I wanted a custom zoom functionality that changes the template's dimensions when the user scrolls.

    Create a reactive variable 'zoom'

    Template.graph.onCreated(function() {
        var self = this;
        self.zoom = new ReactiveVar(0);
        $(window).on('scroll', function(e) {
            // ... event processing stuff; 
            // say it produces value 'zoomAmount' ...
            self.zoom.set(zoomAmount);
        }
    });
    

    Create a helper that returns zoom. Reference it in the template DOM in a hidden element to make it reactive.

    Template.graph.helpers({
        zoom: function() { 
            // This will be called when 'zoom' changes, 
            // so treat this as your events function
            return Template.instance().zoom.get(); 
        }
    });
    

提交回复
热议问题