js 小小jquery等比例缩放图片效果
2010-07-23 10:47
351 查看
jQuery.fn.autoZoomLoadImage = function(scaling, width, height, loadPic) {
if (loadPic == null) loadPic = "http://www.cnblogs.com/Images/WhiteLoader.gif";
return this.each(function() {
var t = $(this);
var src = $(this).attr("src");
var img = new Image();
//alert("Loading")
img.src = src;
//自动缩放图片
var autoScaling = function() {
if (scaling) {
if (img.width > 0 && img.height > 0) {
if (img.width / img.height >= width / height) {
if (img.width > width) {
t.width(width);
t.height((img.height * width) / img.width);
}
else {
t.width(img.width);
t.height(img.height);
}
}
else {
if (img.height > height) {
t.height(height);
t.width((img.width * height) / img.height);
}
else {
t.width(img.width);
t.height(img.height);
}
}
}
}
}
//处理ff下会自动读取缓存图片
if (img.complete) {
//alert("getToCache!");
autoScaling();
return;
}
$(this).attr("src", "");
var loading = $("<img alt=\"加载中\" title=\"图片加载中\" src=\"" + loadPic + "\" />");
t.hide();
t.after(loading);
$(img).load(function() {
autoScaling();
loading.remove();
t.attr("src", this.src);
t.show();
//alert("finally!")
});
});
}
if (loadPic == null) loadPic = "http://www.cnblogs.com/Images/WhiteLoader.gif";
return this.each(function() {
var t = $(this);
var src = $(this).attr("src");
var img = new Image();
//alert("Loading")
img.src = src;
//自动缩放图片
var autoScaling = function() {
if (scaling) {
if (img.width > 0 && img.height > 0) {
if (img.width / img.height >= width / height) {
if (img.width > width) {
t.width(width);
t.height((img.height * width) / img.width);
}
else {
t.width(img.width);
t.height(img.height);
}
}
else {
if (img.height > height) {
t.height(height);
t.width((img.width * height) / img.height);
}
else {
t.width(img.width);
t.height(img.height);
}
}
}
}
}
//处理ff下会自动读取缓存图片
if (img.complete) {
//alert("getToCache!");
autoScaling();
return;
}
$(this).attr("src", "");
var loading = $("<img alt=\"加载中\" title=\"图片加载中\" src=\"" + loadPic + "\" />");
t.hide();
t.after(loading);
$(img).load(function() {
autoScaling();
loading.remove();
t.attr("src", this.src);
t.show();
//alert("finally!")
});
});
}
相关文章推荐
- js+jQuery按比例缩放图片(网络图片或本站图片均适用)
- 【js与jquery】js控制图片显示大小(图片等比例缩放)
- 用jquery实现等比例缩放图片效果插件
- 关于图片按比例自适应缩放的js代码
- jQuery实现图片轮播效果代码(基于jquery.pack.js插件)
- 使用jQuery实现指定图片按比例缩放
- JS图片等比例缩放方法完整示例
- 网页中图片轮翻效果(js+jQuery实现)
- 利用jquery-fform.js插件实现图片所见即所得效果
- jQuery-图片预加载+等比例缩放
- Js实现图片缩放上下左右移动效果
- js和jquery 两种写法 鼠标经过图片切换背景效果
- JS控制图片显示的大小(图片等比例缩放)
- js框架jquery瀑布流图片墙效果代码下载,点击相册图片展示大图
- [JS]JS控制图片超过宽度自动缩小图片等比例缩放源代码实例
- jQuery实现等比例缩放大图片
- 按比例缩放图片的两种方法:JS&CSS
- js 模拟QQ聊天窗口图片播放效果(带滚轮缩放)
- javascript 图片等比例缩放效果