图片延迟加载并等比缩放,一个简单的JQuery插件。

本文介绍了一个基于jQuery的图片缩放插件,该插件通过OOP方式编写,可设置图片的高度和宽度,并支持加载中图标显示。文章提供了使用示例和效果演示。

最近在学习JS的OOP所以写了这么个东西

 

使用方法:

$(".viewArea img").zoom({height:74,width:103});

效果演示:

http://www.dadachina.com/test.html

代码:

ContractedBlock.gif ExpandedBlockStart.gif Code
(function($){

    $.fn.zoom 
= function(settings){
                
//一些默认配置;
                settings = $.extend({
                    height:
0,
                    width:
0,
                    loading:
"http://www.dadachina.com/images/lightbox-ico-loading.gif"
                    },settings);
                    
                
var images = this;
                $(images).hide();
                
var loadding = new Image();
                loadding.className
="loadding"
                loadding.src 
= settings.loading;
                $(images).after(loadding);    
                
                
//预加载
                var preLoad = function($this){
                    
var img = new Image();
                    img.src 
= $this.src;
                    
if (img.complete) { 
                        processImg.call($
this);
                        
return;
                    }
                    
//$this.src = loadding.src;//会导致获取错误的尺寸
                    img.onload = function(){
                        
//$this.src = this.src; //会导致获取错误的尺寸
                        
                        processImg.call($
this);
                        img.onload
=function(){};
                    } 
                }
                
                
//计算图片尺寸;
                function processImg(){
                        
//if(settings.height===0||settings.width ===0) return;
                        var m = this.height-settings.height;
                        
var n = this.width - settings.width;
                        
if(m>n)                        
                            
this.height = this.height>settings.height ? settings.height : 

this.height;
                        
else
                            
this.width = this.width >settings.width ? settings.width : 

this.width;

                        $(
this).next(".loadding").remove()
                        $(
this).show();
                }
                
                
return $(images).each(function(){
                    preLoad(
this);
                });                
        }

})(jQuery);

 

效果是这样的:

 

转载于:https://www.cnblogs.com/rentj1/archive/2009/11/08/1598464.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值