
想做一个JS来获取图片的宽度跟高度?
想做一个JS来获取图片的宽度跟高度?然后做一个判断,如果高度大于宽度就将高度设置成100像素。如果宽度大于高度,就将宽度设置成100像素,求牛人解答。...
想做一个JS来获取图片的宽度跟高度?然后做一个判断,如果高度大于宽度就将高度设置成100像素。如果宽度大于高度,就将宽度设置成100像素,求牛人解答。
展开
2个回答
展开全部
<img id="t" src="图片地址" />
<script>
var img = document.getElementById("t")
var temp = new Image();
temp.onload = function(){
var style = img.style,
ratio = Math.min( 1,
Math.max( 0, 100 ) / this.width || 1,
Math.max( 0, 100 ) / this.height || 1
);
//设置预览尺寸
style.width = Math.round( this.width * ratio ) + "px";
style.height = Math.round( this.height * ratio ) + "px";
}
temp.src = img.src;
</script>
<script>
var img = document.getElementById("t")
var temp = new Image();
temp.onload = function(){
var style = img.style,
ratio = Math.min( 1,
Math.max( 0, 100 ) / this.width || 1,
Math.max( 0, 100 ) / this.height || 1
);
//设置预览尺寸
style.width = Math.round( this.width * ratio ) + "px";
style.height = Math.round( this.height * ratio ) + "px";
}
temp.src = img.src;
</script>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询