maxwidth) { var oldwidth = $(this).width(); v ;更新日期:2025/11/19.幽灵资源网,磁力链接,云盘下载,BT种子,CPU天梯,显卡天梯,UU加速器,阅读3.0,英雄联盟,怪物猎人,无损音乐网,无损音乐下载网站,无损音乐免费下载,320Kmp3下载,无损音乐免费下载网站,音画欣赏,无损音乐,抖音神曲,发烧大碟,车载歌曲,试音天碟,WMA,WAV+CUE,WAV整轨,FLAC分轨,DSD黑胶,HI-FI试音,SACD-ISO,4K高清,高清电影下载,Magnet,Torrent,BitTorrent,迅雷快传,SUB,SRT,ASS/SSA,SUP,RARBG,TLF字幕,BluRay,x265,x264,DTS-HD,WEBRip,10BIT,HDR,DDP5.1,WEB-DL,1080p高清电影下载,中国高清网,高清电影,720p,1080p,MKV,AVI,蓝光原盘,3D高清,电影下载">
网络编程 发布日期:2025/11/19 浏览次数:1
核心代码:
$(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"))});
}
});
});
如果上面的代码不能执行,可以使用下面的代码:
$(window).load(function() {
$(".dvcontent img").each(function() {
var maxwidth = 600;
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"))});
}
});
});
通过css还有一种方法兼容IE6能让图片在超过规定的宽度时自动按比例缩小,但该写法不符合W3C标准。代码如下:
.cate img{
max-width: 600px;
height:auto;
width:expression(this.width > 600 "600px" : this.width);
}
所以在做到尽量兼容IE和其他浏览器以及符合W3C的标准下就通过js来控制图片的宽度了,下面使用jquery控制图片显示时的最大宽度,主代码如下:
$(window).load(function() {
$(".cate img").each(function() {
var maxwidth = 600;
if ($(this).width() > maxwidth) {
$(this).width(maxwidth);
}
});
});
代码很简单,就是cate样式下的所以img的最大宽度只能为600px。.each(function(){......}),each在这里是对指定的图片集合对象逐一调用下面的方法。这种jquery方法在IE6及以上浏览器和chrome及Firefox上都能实现控制图片显示时的最大宽度。