真实的图片宽高,需要在图片加载后,获取
直接 调用 setImgW(); 即可
//通过图片宽高,设置css
public setImgW() {
var imgSrc = $(".parkImg").attr("src");
this.getImageWidth(imgSrc, function (w, h) {
console.log({
width: w,
height: h
});
if (w > 900) {
$('.parkImg').css({ 'height':'40%','position':'relative','top':'28%'})
}else
if (w > 899 && w < 798) {
$('.parkImg').css('height','50%')
} else
if (w > 799) {
$('.parkImg').css({ 'height':'90%','position':'relative','top':'5%'})
}
})
}
//获取图片宽高
public getImageWidth(url, callback) {
var img = new Image();
img.src = url;
// 如果图片被缓存,则直接返回缓存数据
if (img.complete) {
callback(img.width, img.height);
} else {
// 完全加载完毕的事件
img.onload = function () {
callback(img.width, img.height);
}
}
}