Skip to content

Commit e9b1377

Browse files
committed
add mode option
1 parent fa75b0e commit e9b1377

File tree

7 files changed

+87
-102
lines changed

7 files changed

+87
-102
lines changed

demo/src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
<template>
6666
<div>
6767
<div>
68-
<lazy-ref ref="lazyRef" class="lazy-ref" :opts="{classTarget:'parent'}">
68+
<lazy-ref ref="lazyRef" class="lazy-ref" :opts="{classTarget:'parent', retry:3}">
6969
<div>
7070
<img v-lazy="{ref:'lazyRef', src:img}">
7171
</div>

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
"less": "^2.7.2",
7070
"less-loader": "^2.2.3",
7171
"phantomjs-prebuilt": "^2.1.14",
72-
"simulant": "^0.2.2",
7372
"url-loader": "^0.5.7",
7473
"vue-loader": "^10.0.0",
7574
"vue-style-loader": "^1.0.0",

src/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ const LazyRef = {
4848
el,
4949
});
5050
},
51+
destroyed() {
52+
this.$lazy.destroy();
53+
},
5154
methods: {
5255
check() {
5356
this.$lazy.check();

src/lazy.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { $ } from './util';
22

3-
/* global window */
3+
/* global window, Image */
44
const EV_SCROLL = 'scroll',
55

66
CLASS_LOADING = 'lazy-loading',
@@ -10,6 +10,8 @@ const EV_SCROLL = 'scroll',
1010
CLASS_TARGET_SELF = 'self',
1111
CLASS_TARGET_PARENT = 'parent',
1212

13+
MODE_BG = 'bg',
14+
1315
STAT_NOT_LOAD = 0,
1416
STAT_LOADING = 1,
1517
STAT_LOADED = 2,
@@ -24,6 +26,7 @@ const {
2426
addClass,
2527
removeClass,
2628
attr,
29+
css,
2730
removeAttr,
2831
offset,
2932
isArr,
@@ -95,7 +98,10 @@ function loadHandler(lazyLoader) {
9598
classLoading,
9699
classErr,
97100
classLoaded,
98-
} = opts;
101+
} = opts,
102+
loadEl = el,
103+
onLoad,
104+
onErr;
99105

100106
const classes = [classLoading, classErr, classLoaded],
101107
elTarget = classTarget == CLASS_TARGET_PARENT ? el.parentNode || el : el;
@@ -109,22 +115,39 @@ function loadHandler(lazyLoader) {
109115

110116
src = trim(src);
111117

118+
switch (lazyLoader.opts.mode) {
119+
case MODE_BG:
120+
css(el, 'background-image', 'none');
121+
onLoad = () => {
122+
css(el, 'background-image', `url(${src})`);
123+
};
124+
onErr = () => {
125+
css(el, 'background-image', 'none');
126+
};
127+
loadEl = new Image();
128+
break;
129+
default:// empty
130+
}
131+
112132
if (!src) {
113133
lazyLoader.stat = STAT_LOADED;
114134
switchClass(elTarget, classLoaded);
115135
}
116136
else {
117137
lazyLoader.stat = STAT_LOADING;
138+
118139
lazyLoader.req = new Req({
119-
el,
140+
el: loadEl,
120141
src,
121142
retry,
122143
onLoad: () => {
144+
onLoad && onLoad();
123145
lazyLoader.stat = STAT_LOADED;
124146
switchClass(elTarget, classLoaded);
125147
once && lazyLoader.destroy();
126148
},
127149
onErr: () => {
150+
onErr && onErr();
128151
lazyLoader.stat = STAT_LOADED;
129152
switchClass(elTarget, classErr);
130153
once && lazyLoader.destroy();

src/util.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
/* global getComputedStyle window */
22
const win = window,
3+
cssNumber = {
4+
'column-count': 1,
5+
columns: 1,
6+
'font-weight': 1,
7+
'line-height': 1,
8+
opacity: 1,
9+
'z-index': 1,
10+
zoom: 1,
11+
},
312
round = Math.round;
413

514
function type(o) {
@@ -105,6 +114,35 @@ function throttle(fn, threshold = 250, scope) {
105114
};
106115
}
107116

117+
function camelize(str) {
118+
return str.replace(/-+(.)?/g, (match, chr) => (chr ? chr.toUpperCase() : ''));
119+
}
120+
121+
function dasherize(str) {
122+
return str.replace(/::/g, '/')
123+
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1_$2')
124+
.replace(/([a-z\d])([A-Z])/g, '$1_$2')
125+
.replace(/_/g, '-')
126+
.toLowerCase();
127+
}
128+
129+
function maybeAddPx(name, value) {
130+
return (type(value) == 'number' && !cssNumber[dasherize(name)]) ? `${value}px` : value;
131+
}
132+
133+
function css(element, property, value) {
134+
/* eslint consistent-return: 0 */
135+
const elementSytle = element.style;
136+
if (arguments.length < 3) {
137+
return elementSytle[camelize(property)] || getComputedStyle(element, '').getPropertyValue(property);
138+
}
139+
else if (!value && value !== 0) {
140+
elementSytle.removeProperty(dasherize(property));
141+
}
142+
else {
143+
elementSytle[dasherize(property)] = maybeAddPx(property, value);
144+
}
145+
}
108146

109147
export const $ = {
110148
on,
@@ -116,6 +154,7 @@ export const $ = {
116154
addClass,
117155
removeClass,
118156
attr,
157+
css,
119158
removeAttr,
120159
each,
121160
offset,

test/unit/specs/lazyload.spec.js

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,20 @@
11
// TODO not all tested
22

33
import $ from 'jquery';
4-
import {Carousel, CarouselItem} from '../../../src/index';
5-
import {cssTextToObject, createVM, destroyVM, createVirtualPointer} from '../util';
6-
7-
const EV_CHANGED_INDEX = 'changed-index',
8-
EV_RENDER_UPDATED = 'render-updated',
9-
EV_PREV = 'prev',
10-
EV_NEXT = 'next',
11-
EV_TO = 'to',
12-
13-
EV_CLICK = 'click',
14-
EV_TOUCHSTART = 'touchstart',
15-
EV_TOUCHMOVE = 'touchmove',
16-
EV_TOUCHEND = 'touchend',
17-
EV_MOUSE_DOWN = 'mousedown',
18-
EV_MOUSE_MOVE = 'mousemove',
19-
EV_MOUSE_UP = 'mouseup',
20-
21-
TIME_LAG = 100;
4+
import { VueLLazyload } from '../../../src/index';
5+
import {cssTextToObject, createVM, destroyVM, genImgSrc, genImgList} from '../util';
226

237
jasmine.DEFAULT_TIMEOUT_INTERVAL = 20 * 1000;
248

25-
describe('Suite: test Carousel.vue', () => {
26-
const COMMON_SPEED_TIME = 300,
27-
COMMON_LIST = [
28-
{
29-
url: 'url1'
30-
},
31-
{
32-
url: 'url2'
33-
},
34-
{
35-
url: 'url3'
36-
}
37-
],
38-
COMMON_DATA = {
39-
prevHTML: '&lt;',
40-
nextHTML: '&gt;',
41-
speed: COMMON_SPEED_TIME,
42-
list: COMMON_LIST,
43-
loop: false,
44-
rewind: false,
45-
mouseDrag: false,
46-
auto: 0,
47-
dots: true,
48-
dotsStyle: '',
49-
},
50-
$body = $('body');
9+
describe('Suite: test vue-l-lazyload', () => {
10+
const $body = $('body');
5111
let vm;
5212

5313
$body.css({
5414
padding: 0,
5515
margin: 0,
5616
});
5717

58-
//beforeEach(() => {
59-
//});
60-
//
6118
afterEach(() => {
6219
destroyVM(vm);
6320
});
@@ -67,20 +24,9 @@ describe('Suite: test Carousel.vue', () => {
6724
vm = createVM(
6825
`
6926
<div>
70-
<carousel ref="car" :watch-items="list" :prev-html="prevHTML" :next-html="nextHTML" :speed="speed" :loop="loop" :rewind="rewind" :mouseDrag="mouseDrag" :auto="auto" :dots="dots" :dots-style="dotsStyle">
71-
<carousel-item v-for="(item, index) in list">
72-
<p>CarouselItem{{index}}, URL is {{item.url}}</p>
73-
</carousel-item>
74-
<div slot="before">Insert node before</div>
75-
<div slot="after">Insert node after</div>
76-
</carousel>
7727
</div>
7828
`,
7929
{
80-
components: {
81-
'carousel': Carousel,
82-
'carousel-item': CarouselItem
83-
},
8430
...data,
8531
});
8632
}

test/unit/util.js

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import Vue from 'vue';
22
import $ from 'jquery';
3-
import simulant from 'simulant';
43

54
function type(o) {
65
return typeof o;
@@ -49,42 +48,6 @@ function createVM(Compo, propsData = {}, mounted) {
4948
return new VueExtended(nPropsData).$mount(mounted === false ? null : el)
5049
}
5150

52-
function createVirtualPointer() {
53-
return {
54-
x: 0,
55-
y: 0,
56-
trigger(evtName) {
57-
const x = this.x,
58-
y = this.y;
59-
// TODO: I haven't found a good way to simulate touch events
60-
simulant.fire(document.elementFromPoint(x, y), evtName, {
61-
clientX: x,
62-
clientY: y,
63-
pageX: x,
64-
pageY: y,
65-
touches: [
66-
{
67-
pageX: x,
68-
pageY: y,
69-
}
70-
],
71-
targetTouches: [{
72-
pageX: x,
73-
pageY: y,
74-
}],
75-
});
76-
//$(document.elementFromPoint(x, y)).trigger($.Event(evtName, {
77-
// pageX: x,
78-
// pageY: y,
79-
// targetTouches: [{
80-
// pageX: x,
81-
// pageY: y,
82-
// }],
83-
//}));
84-
},
85-
}
86-
}
87-
8851
function cssTextToObject(txt) {
8952
const result = {};
9053
txt.split(';').forEach((term) => {
@@ -98,10 +61,22 @@ function cssTextToObject(txt) {
9861
return result;
9962
}
10063

64+
function genImgSrc() {
65+
return 'img/lenna.png?_t=' + Math.floor(Math.random() * 100000) + '.' + Date.now();
66+
}
67+
68+
function genImgList(len) {
69+
const arr = [];
70+
while (len--) {
71+
arr.push(genImgSrc());
72+
}
73+
return arr;
74+
}
10175
export {
10276
destroyVM,
10377
createAndReplaceElem,
10478
createVM,
105-
createVirtualPointer,
106-
cssTextToObject
79+
cssTextToObject,
80+
genImgSrc,
81+
genImgList,
10782
};

0 commit comments

Comments
 (0)