Skip to content

Commit

Permalink
issue#34
Browse files Browse the repository at this point in the history
  • Loading branch information
hilongjw committed Sep 7, 2016
1 parent b6a3c6b commit 511748d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-lazyload",
"version": "0.7.4",
"version": "0.7.5",
"description": "Vue module for lazy-loading images in your vue.js applications.",
"main": "vue-lazyload.es5.js",
"scripts": {
Expand Down
41 changes: 22 additions & 19 deletions vue-lazyload.es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ exports.install = function (Vue, Options) {
};

var _ = {
on: function on(type, func) {
window.addEventListener(type, func);
on: function on(el, type, func) {
el.addEventListener(type, func);
},
off: function off(type, func) {
window.removeEventListener(type, func);
off: function off(el, type, func) {
el.removeEventListener(type, func);
}
};

Expand All @@ -89,22 +89,22 @@ exports.install = function (Vue, Options) {
}
}, 300);

var onListen = function onListen(start) {
var onListen = function onListen(el, start) {
if (start) {
_.on('scroll', lazyLoadHandler);
_.on('wheel', lazyLoadHandler);
_.on('mousewheel', lazyLoadHandler);
_.on('resize', lazyLoadHandler);
_.on('animationend', lazyLoadHandler);
_.on('transitionend', lazyLoadHandler);
_.on(el, 'scroll', lazyLoadHandler);
_.on(el, 'wheel', lazyLoadHandler);
_.on(el, 'mousewheel', lazyLoadHandler);
_.on(el, 'resize', lazyLoadHandler);
_.on(el, 'animationend', lazyLoadHandler);
_.on(el, 'transitionend', lazyLoadHandler);
} else {
Init.hasbind = false;
_.off('scroll', lazyLoadHandler);
_.off('wheel', lazyLoadHandler);
_.off('mousewheel', lazyLoadHandler);
_.off('resize', lazyLoadHandler);
_.off('animationend', lazyLoadHandler);
_.off('transitionend', lazyLoadHandler);
_.off(el, 'scroll', lazyLoadHandler);
_.off(el, 'wheel', lazyLoadHandler);
_.off(el, 'mousewheel', lazyLoadHandler);
_.off(el, 'resize', lazyLoadHandler);
_.off(el, 'animationend', lazyLoadHandler);
_.off(el, 'transitionend', lazyLoadHandler);
}
};

Expand Down Expand Up @@ -169,7 +169,7 @@ exports.install = function (Vue, Options) {
}

if (Init.hasbind && Listeners.length == 0) {
onListen(false);
onListen(window, false);
}
};

Expand Down Expand Up @@ -205,7 +205,10 @@ exports.install = function (Vue, Options) {
lazyLoadHandler();
if (Listeners.length > 0 && !Init.hasbind) {
Init.hasbind = true;
onListen(true);
onListen(window, true);
}
if (parentEl) {
onListen(parentEl, true);
}
});
};
Expand Down
41 changes: 22 additions & 19 deletions vue-lazyload.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ exports.install = function(Vue, Options) {
}

const _ = {
on (type, func) {
window.addEventListener(type, func)
on (el, type, func) {
el.addEventListener(type, func)
},
off (type, func) {
window.removeEventListener(type, func)
off (el, type, func) {
el.removeEventListener(type, func)
}
}

Expand All @@ -87,22 +87,22 @@ exports.install = function(Vue, Options) {
}
}, 300)

const onListen = (start) => {
const onListen = (el, start) => {
if (start) {
_.on('scroll', lazyLoadHandler)
_.on('wheel', lazyLoadHandler)
_.on('mousewheel', lazyLoadHandler)
_.on('resize', lazyLoadHandler)
_.on('animationend', lazyLoadHandler)
_.on('transitionend', lazyLoadHandler)
_.on(el, 'scroll', lazyLoadHandler)
_.on(el, 'wheel', lazyLoadHandler)
_.on(el, 'mousewheel', lazyLoadHandler)
_.on(el, 'resize', lazyLoadHandler)
_.on(el, 'animationend', lazyLoadHandler)
_.on(el, 'transitionend', lazyLoadHandler)
} else {
Init.hasbind = false
_.off('scroll', lazyLoadHandler)
_.off('wheel', lazyLoadHandler)
_.off('mousewheel', lazyLoadHandler)
_.off('resize', lazyLoadHandler)
_.off('animationend', lazyLoadHandler)
_.off('transitionend', lazyLoadHandler)
_.off(el, 'scroll', lazyLoadHandler)
_.off(el, 'wheel', lazyLoadHandler)
_.off(el, 'mousewheel', lazyLoadHandler)
_.off(el, 'resize', lazyLoadHandler)
_.off(el, 'animationend', lazyLoadHandler)
_.off(el, 'transitionend', lazyLoadHandler)
}
}

Expand Down Expand Up @@ -169,7 +169,7 @@ exports.install = function(Vue, Options) {
}

if (Init.hasbind && Listeners.length == 0) {
onListen(false)
onListen(window, false)
}
}

Expand Down Expand Up @@ -205,7 +205,10 @@ exports.install = function(Vue, Options) {
lazyLoadHandler()
if (Listeners.length > 0 && !Init.hasbind) {
Init.hasbind = true
onListen(true)
onListen(window, true)
}
if (parentEl) {
onListen(parentEl, true)
}
})
}
Expand Down

0 comments on commit 511748d

Please sign in to comment.