jquery实现图片等比例缩放以及max-width在ie中不兼容解决 - Web前端
作者:98wpeu发布时间:2026-08-06分类:网页前端技术浏览:139
导读:上次用原生的JavaScript实现的图片等比例缩放,max-width和max-height在IE6中不兼容问题,今天用JQuery实现了这些问题jQuery部分代码复制代码...
上次用原生的JavaScript实现的图片等比例缩放,max-width和max-height在IE6中不兼容问题,今天用JQuery实现了这些问题
jQuery部分代码
复制代码 代码如下:
<scripttype="text/javascript">
$(document).ready(function(){
var maxWidth=$(".imgBox").width();
$("img").each(function(){
if(!$.support.style&&$.browser.msie&&($.browser.version==6.0)){ //判定浏览器为IE6的时候
var imgwidth=$(this).width();
var imgHeight=$(this).height();
var maxHeight = maxWidth*imgHeight/imgWidth;
if(imgWidth>maxWidth){
$(this).CSS("width",maxWidth).css("height",maxHeight);
}
}
})
})
</script>
css部分代码:
复制代码 代码如下:
<style type="text/css">
body{ margin:0; padding:0;}
.box{ width:700px; margin:0 auto;}
.imgBox{ background:#CCCCCC;*display:table-cell; width:400px; margin:10px auto; *font-size:350px; line-height:400px; text-align:center; vertical-align:middle; padding:20px;}
img{ border:0 none; max-width:400px; overflow:hidden; vertical-align:middle;}
</style>
html代码:
复制代码 代码如下:
<div class="box">
<div class="imgBox">
<img src="images/12.jpg" alt="等比例缩放图片"/>
</div>
</div>
jQuery部分代码
复制代码 代码如下:
<scripttype="text/javascript">
$(document).ready(function(){
var maxWidth=$(".imgBox").width();
$("img").each(function(){
if(!$.support.style&&$.browser.msie&&($.browser.version==6.0)){ //判定浏览器为IE6的时候
var imgwidth=$(this).width();
var imgHeight=$(this).height();
var maxHeight = maxWidth*imgHeight/imgWidth;
if(imgWidth>maxWidth){
$(this).CSS("width",maxWidth).css("height",maxHeight);
}
}
})
})
</script>
css部分代码:
复制代码 代码如下:
<style type="text/css">
body{ margin:0; padding:0;}
.box{ width:700px; margin:0 auto;}
.imgBox{ background:#CCCCCC;*display:table-cell; width:400px; margin:10px auto; *font-size:350px; line-height:400px; text-align:center; vertical-align:middle; padding:20px;}
img{ border:0 none; max-width:400px; overflow:hidden; vertical-align:middle;}
</style>
html代码:
复制代码 代码如下:
<div class="box">
<div class="imgBox">
<img src="images/12.jpg" alt="等比例缩放图片"/>
</div>
</div>
相关推荐
- jquery的index方法实现tab效果 - Web前端
- Jquery选择子控件"大于号"和" "区别介绍及使用示例 - Web前端
- jquery插件开发注意事项小结 - Web前端
- 关于javascript中的 “ && ” 和 “ || ”那些事 - Web前端
- jquery 触发a链接点击事件解决方案 - Web前端
- jQuery学习笔记(1)--用jQuery实现异步通信(用json传值)具体思路 - Web前端
- Struts2的s:radio标签使用及用jquery添加change事件 - Web前端
- 利用webqq协议使用python登录qq发消息源码参考 - Web前端
- JQuery 图片的展开和伸缩实例讲解 - Web前端
- jQuery仿Excel表格编辑功能的实现代码 - Web前端
- 网页前端技术排行
-
- 1[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 2分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 3jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 4【第六章】Foundation之按钮和下拉功能 - Web前端
- 5基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 6在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 7jQuery编写widget的一些技巧分享 - Web前端
- 8分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 9jQuery实现鼠标移到元素上动态提示消息框效果 - Web前端
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


