|
本帖最后由 课程格子 于 2019-10-10 11:36 编辑
html代码- <img class="cacheImg_1" :srcpath="image" />
- <div class="cacheImg_2" :srcpath="image"></div>
复制代码
js使用代码
ajax或者其他方式获取到网络图片之后执行此方法
使用$nextTick是为了保证页面已经渲染成功,attr方法才能成功设置属性- vue.$nextTick(function() {
- imgcache(1, $('.cacheImg_1'));
- imgcache(2, $('.cacheImg_2'));
- });
复制代码
js调用方法
参数说明:type为1时代表设置image标签的src属性,type为2时代表设置背景图片
selector代表所需要设置的html元素
- function imgcache(type, selector) {
- selector.each(function(data) {
- ! function(data) {
- var url = selector.eq(data).attr("srcpath");
- api.imageCache({
- url: url,
- policy: "cache_only",
- thumbnail: false
- }, function(ret, err) {
- if (type == 1) {
- selector.eq(data).attr('src', ret.url);
- } else {
- selector.eq(data).css('backgroundImage', 'url("' + ret.url + '")');
- }
- });
- }(data);
- });
- }
复制代码
|
|