如何在这种情况下使用实际图像更改图像大小



脚本代码不运行,我需要图像的顶部和底部的宽度超过1000px的图像和低于1000px的图像,然后取宽度和高度在else if条件

请解决我的脚本问题

var yourImg = document.getElementById('yourImgId');
if(yourImg && yourImg.style > 1000) { /*  image actual size above 1000px then set below height and width */
    yourImg.style.height = '200px';
    yourImg.style.top = '20px';
    yourImg.style.bottom = '20px';
    yourImg.style.width = '200px';
}
else if (yourImg && yourImg.style < 1000) /*  image actual size below  1000px then set below height and width */
{
  yourImg.style.height = '200px';
    yourImg.style.width = '200px';
}
<div style="  border-right:1px solid #fff; "> 
 <a href="#">
 <img  class="img-responsive" alt="product" src="http://olpur.com/admin2/products/15201small.jpg" id="yourImgId" style=" width:100%;border:none;  object-fit: contain; object-position: top 75%;" />
                                            </a>
</div>

代替yourImg.style,我认为您正在尝试使用yourImg.clientWidth获得图像的当前宽度。

var yourImg = document.getElementById('yourImgId');
if (yourImg && yourImg.clientWidth > 1000) {
  yourImg.style.height = '200px';
  yourImg.style.top = '20px';
  yourImg.style.bottom = '20px';
  yourImg.style.width = '200px';
}
else if (yourImg && yourImg.clientWidth < 1000) {
  yourImg.style.height = '200px';
  yourImg.style.width = '200px'; 
}
https://jsfiddle.net/a35u2fm9/

最新更新