Showing
5 changed files
with
100 additions
and
3 deletions
@@ -54,6 +54,7 @@ | @@ -54,6 +54,7 @@ | ||
54 | "fast-safe-stringify": "^1.2.0", | 54 | "fast-safe-stringify": "^1.2.0", |
55 | "feed": "^1.1.0", | 55 | "feed": "^1.1.0", |
56 | "geetest": "^4.1.2", | 56 | "geetest": "^4.1.2", |
57 | + "jquery-lazyload": "^1.9.7", | ||
57 | "lodash": "^4.17.4", | 58 | "lodash": "^4.17.4", |
58 | "memory-cache": "^0.2.0", | 59 | "memory-cache": "^0.2.0", |
59 | "moment": "^2.18.1", | 60 | "moment": "^2.18.1", |
1 | var $ = require('yoho-jquery'); | 1 | var $ = require('yoho-jquery'); |
2 | -var lazyLoad = require('yoho-jquery-lazyload'); | 2 | +var lazyLoad = require('plugin/lazyload'); |
3 | var Swiper = require('yoho-swiper'); | 3 | var Swiper = require('yoho-swiper'); |
4 | var qs = require('yoho-qs'); | 4 | var qs = require('yoho-qs'); |
5 | var yoho = require('../yoho-app'); | 5 | var yoho = require('../yoho-app'); |
public/js/plugin/lazyload.js
0 → 100644
1 | +require('./webp-support'); | ||
2 | + | ||
3 | +const $ = require('yoho-jquery'); | ||
4 | + | ||
5 | +require('jquery-lazyload'); | ||
6 | + | ||
7 | +/** | ||
8 | + * 图片链接处理 | ||
9 | + */ | ||
10 | +const imgSrcHandle = (imgUrl, params) => { | ||
11 | + if (!imgUrl) { | ||
12 | + return imgUrl; | ||
13 | + } | ||
14 | + | ||
15 | + let splits = imgUrl.split('?'); | ||
16 | + let url = splits[0]; | ||
17 | + let query = splits[1] || ''; | ||
18 | + | ||
19 | + if (query.indexOf('imageView2') === -1) { | ||
20 | + if (window.supportWebp) { | ||
21 | + url += '?imageView2/0/format/webp'; | ||
22 | + } else { | ||
23 | + return url + '?imageslim'; | ||
24 | + } | ||
25 | + | ||
26 | + url += `/q/${params.q}`; | ||
27 | + } | ||
28 | + | ||
29 | + return url; | ||
30 | +}; | ||
31 | + | ||
32 | +/** | ||
33 | + * 图片链接预处理后使用原生 lazyload | ||
34 | + */ | ||
35 | +const rawLazyload = (selector, options) => { | ||
36 | + let $imgs; | ||
37 | + let params = { | ||
38 | + threshold: 600 | ||
39 | + }; | ||
40 | + let imgParams = { | ||
41 | + q: options && options.q || 75 | ||
42 | + }; | ||
43 | + | ||
44 | + if (selector instanceof $) { | ||
45 | + $imgs = selector; | ||
46 | + } else { | ||
47 | + $imgs = $(selector).find('img.lazy'); | ||
48 | + } | ||
49 | + | ||
50 | + $.extend(params, options); | ||
51 | + | ||
52 | + const imgsProcess = () => { | ||
53 | + $imgs.each((index, elem) => { | ||
54 | + $(elem).attr('data-original', imgSrcHandle($(elem).data('original'), imgParams)); | ||
55 | + }); | ||
56 | + $imgs.lazyload(params); | ||
57 | + }; | ||
58 | + | ||
59 | + if (typeof window.supportWebp !== 'undefined') { | ||
60 | + imgsProcess(); | ||
61 | + } else { | ||
62 | + $(document).on('supportWebp', imgsProcess); | ||
63 | + } | ||
64 | +}; | ||
65 | + | ||
66 | +module.exports = rawLazyload; |
public/js/plugin/webp-support.js
0 → 100644
1 | +const $ = require('yoho-jquery'); | ||
2 | + | ||
3 | +/** | ||
4 | + * 检测浏览器是否支持 webp 图片格式 | ||
5 | + */ | ||
6 | +const checkWebpSupport = (feature) => { | ||
7 | + let kTestImages = { | ||
8 | + lossy: 'UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA', | ||
9 | + | ||
10 | + /* eslint-disable */ | ||
11 | + /* lossless: 'UklGRhoAAABXRUJQVlA4TA0AAAAvAAAAEAcQERGIiP4HAA==', | ||
12 | + alpha: 'UklGRkoAAABXRUJQVlA4WAoAAAAQAAAAAAAAAAAAQUxQSAwAAAARBxAR/Q9ERP8DAABWUDggGAAAABQBAJ0BKgEAAQAAAP4AAA3AAP7mtQAAAA==', // eslint-disable-line | ||
13 | + animation: 'UklGRlIAAABXRUJQVlA4WAoAAAASAAAAAAAAAAAAQU5JTQYAAAD/////AABBTk1GJgAAAAAAAAAAAAAAAAAAAGQAAABWUDhMDQAAAC8AAAAQBxAREYiI/gcA' // eslint-disable-line */ | ||
14 | + /* eslint-enable */ | ||
15 | + }; | ||
16 | + let img = new Image(); | ||
17 | + | ||
18 | + img.onload = () => { | ||
19 | + let result = (img.width > 0) && (img.height > 0); | ||
20 | + | ||
21 | + window.supportWebp = result || false; | ||
22 | + $(document).trigger('supportWebp'); | ||
23 | + }; | ||
24 | + img.onerror = () => { | ||
25 | + window.supportWebp = false; | ||
26 | + $(document).trigger('supportWebp'); | ||
27 | + }; | ||
28 | + img.src = 'data:image/webp;base64,' + kTestImages[feature]; | ||
29 | +}; | ||
30 | + | ||
31 | +checkWebpSupport('lossy'); |
@@ -2,14 +2,13 @@ | @@ -2,14 +2,13 @@ | ||
2 | * @Author: Targaryen | 2 | * @Author: Targaryen |
3 | * @Date: 2017-03-23 11:31:51 | 3 | * @Date: 2017-03-23 11:31:51 |
4 | * @Last Modified by: Targaryen | 4 | * @Last Modified by: Targaryen |
5 | - * @Last Modified time: 2017-05-02 11:17:31 | ||
6 | */ | 5 | */ |
7 | 6 | ||
8 | /** ***************** | 7 | /** ***************** |
9 | * 红人店铺首页 | 8 | * 红人店铺首页 |
10 | ********************/ | 9 | ********************/ |
11 | const Swiper2 = require('yoho-swiper2'); | 10 | const Swiper2 = require('yoho-swiper2'); |
12 | -const lazyLoad = require('yoho-jquery-lazyload'); | 11 | +const lazyLoad = require('plugin/lazyload'); |
13 | let tip = require('../../plugin/tip'); | 12 | let tip = require('../../plugin/tip'); |
14 | let $goodsContainer = $('.index-goods-container'); | 13 | let $goodsContainer = $('.index-goods-container'); |
15 | let $indexGoodsContaniner = $('#indexGoodsContainer'); | 14 | let $indexGoodsContaniner = $('#indexGoodsContainer'); |
-
Please register or login to post a comment