Skip to content

Conversation

@Jocs
Copy link
Contributor

@Jocs Jocs commented Aug 18, 2017

新增两个功能:

  1. 图片延迟加载,延迟加载的图片会等待其他图片加载完后进行加载。
  2. 图片尺寸适配设备屏幕大小,该配置依赖于淘宝的lib-flexible库,根据不同设备,加载不同尺寸图片。以iphone6 width:750px为基准。

prefix: '',
quality: 100
quality: 100,
adapt: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

默认值我觉得要用 false,这样以前的应该才不受影响。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adapt参数没有默认值,全局不配置的话,代码中就获取不到,也就不会进行适配。上面文档中只是例子,也可以设置成false。遵循的规则就是指令中的配置会shadow全局的配置。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

@@ -0,0 +1,2 @@
document.documentElement.setAttribute('style', 'font-size: 64px')
document.documentElement.setAttribute('data-dpr', '2') No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

换行

viewWidth = +(html.getAttribute('style').match(/(\d+)/) || [])[1]
} catch(e) {
console.error('adapt参数需要配合lib-flexible库使用:https://github.com/amfe/lib-flexible')
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最近项目打算从 flexible + rem 的模式切换到 vw,已经做了埋点,支持度大概能达到 99.89% 以上,迁移成本目测也不算高(改 px2rem 代码),以后可能不会再用 flexible 了...

DPR 可以通过 devicePixelRatio 来获得,viewWidth 应该可以计算得到~

Copy link
Contributor

@YongX YongX Aug 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这么帅, vw 已经 99.89% 了? 😃 🎉

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯嗯。淘宝lib-flexible也已经更新了,以前lib-flexible的宽度就是document.documentElement.getBoundingClientRect获取到的。我们也可以直接获取,而不需要从html元素的style特性获取。这一版需要这样改动吗?

src/utils.js Outdated
console.error('adapt参数需要配合lib-flexible库使用:https://github.com/amfe/lib-flexible')
}

if (hasDPR && !Number.isNaN(viewWidth) && typeof viewWidth === 'number') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Number.isNaN 这里可能要注意一下
这个项目用的是 Buble...不清楚有没有安装对应的 Polyfill

似乎用 Number(viewWidth) >= 0 可以代替...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

@banrikun
Copy link
Contributor

大概理解了一下逻辑:
插入时,在 viewport 中就直接触发 update 并且加入队列
不在的话就等待队列完毕之后再加载,并且清空队列

这个逻辑和传统意义上的『懒加载』还是有些区别的
此外,delay 一定要精准控制,如果真的有一张图比较坑爹,会影响后面所有的 lazy 图片加载...

非常感谢这个 PR!太用心了!

@YongX
Copy link
Contributor

YongX commented Aug 18, 2017

是的,我们刚才也讨论了下,命名其实不对的。不是传统意义的懒加载,传统意义的懒加载等会儿会再搞上来。

@Jocs
Copy link
Contributor Author

Jocs commented Aug 18, 2017

我把指令中的参数名修改了下lazy => defer。避免和其他库的懒加载混淆。

} catch(e) {
const dpr = window.devicePixelRatio
const w = html.offsetWidth
if (w / dpr > 540) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其它方面我这里都没问题了,就是这里的 540 不是很能理解……求教……

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这儿我是参考lib-flexible库,当这个比值大于540时,w就取540 * dpr作为最大值。也就是超过540 * dpr的屏幕,就没必要再做适配了。毕竟我们现阶段还是使用的lib-flexible,就和它保持统一,等以后我们切到新的适配方案后,这儿代码估计也要做出调整,至少在不切前基本运行不到catch block。

@YongX YongX merged commit c3e796e into ElemeFE:master Aug 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants