运用jquery实现图片按比例缩放。
<!DOCTYPE html>
<html>
<title>运用jquery实现图片按比例缩放</title>
<head>
<style>
.thumbnail{overflow:hidden;width:400px;height:240px;}
</style>
</head>
<body>
<div id="content">
<div id="thumbnail" class="thumbnail" >
<img id="web" src="https://jiangweishan.com/zb_users/upload/2017/11/20171112231859_71896.jpeg"/>
</div>
</div>
<script src="/demo/js/jq.js"></script>
<script language="javascript">
$(function(){
/* 图片不完全按比例自动缩小 by zwwooooo */
$('#content div.thumbnail img').each(function(){
var x = 400; //填入目标图片宽度
var y = 240; //填入目标图片高度
var w=$(this).width(), h=$(this).height();//获取图片宽度、高度
if (w > x) { //图片宽度大于目标宽度时
var w_original=w, h_original=h;
h = h * (x / w); //根据目标宽度按比例算出高度
w = x; //宽度等于预定宽度
if (h < y) { //如果按比例缩小后的高度小于预定高度时
w = w_original * (y / h_original); //按目标高度重新计算宽度
h = y; //高度等于预定高度
}
}
$(this).attr({width:w,height:h});
});
});
</script>
</body>
</html> 







网友评论文明上网理性发言 已有0人参与
发表评论: