我在网站项目上使用 B-Lazy 插件(http://dinbror.dk/blog/blazy/)。我想根据窗口宽度加载不同的图像:
如果窗口宽度 < 420px,插件接受加载一个 img,其 'data-src' 被 'data-src-small' 替换。
因此,我想获取 data-src 图像 url 并在 '.jpg' 之前添加到末尾的 '-228x170' ,如下所示:
<img data-src="img-name.jpg" >
变得
<img data-src-small="img-name-228x170.jpg" >
这是我的代码:`
$('img').addClass('b-lazy');
$("img.b-lazy").each(function() {
$(this).attr("data-src",$(this).attr("src"));
$(this).attr('src','data:trans.gif');
var src = $(this).attr('data-src');
$(this).attr('src', 'src + -228x170.jpg' ); //here my mistake
});
`