欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用jquery等比例控制图片宽高的具体实现

程序员文章站 2022-07-23 09:34:52
用jquery等比例控制图片宽高的具体实现代码如下: $(function() { $(".dvcontent img").each(function()...

用jquery等比例控制图片宽高的具体实现代码如下:

$(function() {
$(".dvcontent img").each(function() {
var maxwidth = 520;
if ($(this).width() > maxwidth) {
var oldwidth = $(this).width();
var oldheight = $(this).height();
var newheight = maxwidth/oldwidth*oldheight;
$(this).css({width:maxwidth+"px",height:newheight+"px",cursor:"pointer"});
$(this).attr("title","点击查看原图");
$(this).click(function(){window.open($(this).attr("src"))});
}
});
});