From 5e93f6b98d5622cb60abd77f04bb64d8c58a1938 Mon Sep 17 00:00:00 2001 From: Michael Cordingley Date: Mon, 2 Dec 2013 09:31:35 -0500 Subject: [PATCH] Listen to window scroll in a scalable way Directly writing to window.onscroll will break any other code run before this that writes to that location and will be broken by any other code that runs after this that writes to that location. Replacing with addEventListener (shimmed for IE8) so that N listeners can be on window.onscroll --- src/echo.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/echo.js b/src/echo.js index 5cae710..cf1edae 100644 --- a/src/echo.js +++ b/src/echo.js @@ -24,11 +24,26 @@ window.Echo = (function (window, document, undefined) { var init = function () { store = document.querySelectorAll('[data-echo]'); _pollImages(); - window.onscroll = _pollImages; + _addEventListener(window, 'scroll', _pollImages); }; return { init: init }; + + var _addEventListener = function(element, event, callback) { + if (element.addEventListener) { + _addEventListener = function(element, event, callback) { + element.addEventListener(event, callback, false); + }; + } + else { + _addEventListener = function(element, event, callback) { + element.attachEvent('on' + event, callback); + }; + } + + _addEventListener(element, event, callback); + }; })(window, document);