js图片自适应宽高
2016-04-08 15:56
435 查看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>原创</title>
<script type="text/javascript" src="jquery-2.1.4.min.js"></script>
<script language="javascript">
/*
if(typeof jQuery == 'undefined'){
window.alert("没有jquery");
}else{
window.alert("jquery");
}
*/
</script>
<style type="text/css">
.content{
width: 400px;
height: 400px;
border:1px solid green;
overflow: hidden;
}
</style>
<script type="text/javascript">
jQuery.fn.LoadImage = function(obj) {
var w = $(obj).width();
//alert (w);
var h = $(obj).height();
return this.each(function() {
var img_w = $(this).width();
//alert(img_w);
var img_h = $(this).height();
//alert(img_h);
if ((img_h/img_w)<(h/w)) {
var newW = (h * img_w) / img_h;
var mleft = -(newW-w)/2
$(this).css({
"width": newW,
"height": h,
'margin-left':mleft
}); //设置缩放后的宽度和高度
}else if ((img_h/img_w)>(h/w)) {
var newH = (w * img_h) / img_w;
var mtop = -(newH-h)/2
$(this).css({
"width": w,
"height": newH,
"margin-top":mtop
}); //设置缩放后的宽度和高度
}
});
}
</script>
</head>
<body>
<div class="content"><img src="1.jpg"/></div>
<div class="content"><img src="2.jpg"/></div>
<div class="content"><img src="3.jpg"/></div>
<div class="content"><img src="4.jpg"/></div>
<script type="text/javascript">
$(window).load(function(){
$('.content img').LoadImage('.content');
});
</script>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>原创</title>
<script type="text/javascript" src="jquery-2.1.4.min.js"></script>
<script language="javascript">
/*
if(typeof jQuery == 'undefined'){
window.alert("没有jquery");
}else{
window.alert("jquery");
}
*/
</script>
<style type="text/css">
.content{
width: 400px;
height: 400px;
border:1px solid green;
overflow: hidden;
}
</style>
<script type="text/javascript">
jQuery.fn.LoadImage = function(obj) {
var w = $(obj).width();
//alert (w);
var h = $(obj).height();
return this.each(function() {
var img_w = $(this).width();
//alert(img_w);
var img_h = $(this).height();
//alert(img_h);
if ((img_h/img_w)<(h/w)) {
var newW = (h * img_w) / img_h;
var mleft = -(newW-w)/2
$(this).css({
"width": newW,
"height": h,
'margin-left':mleft
}); //设置缩放后的宽度和高度
}else if ((img_h/img_w)>(h/w)) {
var newH = (w * img_h) / img_w;
var mtop = -(newH-h)/2
$(this).css({
"width": w,
"height": newH,
"margin-top":mtop
}); //设置缩放后的宽度和高度
}
});
}
</script>
</head>
<body>
<div class="content"><img src="1.jpg"/></div>
<div class="content"><img src="2.jpg"/></div>
<div class="content"><img src="3.jpg"/></div>
<div class="content"><img src="4.jpg"/></div>
<script type="text/javascript">
$(window).load(function(){
$('.content img').LoadImage('.content');
});
</script>
</body>
</html>
相关文章推荐
- js日期函数
- jq与js 区别
- js 取多个id相同的值
- 短信验证js 倒计时效果
- JavaScript能干什么?
- 用户点击获取验证码之后我们会发送一条信息到用户手机,然后就会出现一个倒计时按钮,很像支付宝手机付款效果了,下面我给大家分享两个js效果
- MyEclipse 编写 JSP 代码时很卡的解决办法
- jsonUtil 工具类
- fastjson 私有属性的坑
- 从函数调用的角度,探讨JavaScript中this的用法
- JavaScript代码优化
- js的字符串charAt()方法
- 解归档+json解析
- Ext Js【Hello World】 ——4.1 beta 1
- js生成验证码并验证
- 网页信息抓取进阶 支持Js生成数据 Jsoup的不足之处
- javascript 格式化数字
- js实现跑马灯效果
- json常用
- javaScript中的this关键字解析