Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll event for Meteor

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 'scroll window' : function(event) { ... } which doesn't work as expected.

like image 420
user1952811 Avatar asked Jun 08 '14 23:06

user1952811


People also ask

What is window Onscroll in JavaScript?

Definition and Usage. The onscroll event occurs when an element's scrollbar is being scrolled. Tip: use the CSS overflow style property to create a scrollbar for an element.


1 Answers

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(); 
    }
});
like image 92
Alexander Eden Avatar answered Sep 19 '22 21:09

Alexander Eden