20130628-imageAutoSize-imagesLoaded-live

jQuery.fn.imageAutoSize = function(w) {
    $(this).each(function() {
        var image = $(this);
		var ow = image.width();
		var oh = image.height();
        if( ow > w) {
			var h = w/ow*oh;
            image.width(w);
            image.height(h);
		}
    });
}

jQuery.fn.imagesLoaded = function(callback, context) {
    var elems = this.filter('img'),
        len = elems.length,
        blank = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";

    context = context || elems;

    function countdown() {
        if (this.src != blank) {
            if (--len <= 0)
                callback.call(context, this);
        }
    }

    elems.each(function() {
        var src = this.src;
        this.src = blank;
        $(this).one('load error', countdown);
        this.src = src;
    });

    if (!elems.length)
        callback.call(context);

    return this;
};

jQuery(document).ready(function($) {
   $('.box img').imagesLoaded(function() {
		$('#boxes').masonry({
			itemSelector: '.box',
			columnWidth: 210,
			gutterWidth: 40
		});
	});

	$('#boxes .box').live({
		'mouseenter': function() {
			$(this).addClass('box-hover');
		}, 
		'mouseleave': function() {
			$(this).removeClass('box-hover');
		}
	});

	$('.post-content img').imagesLoaded(function() {
		var ow = $(this).width();

		if(ow > 710) {
			$(this).imageAutoSize(960);
			var w = $(this).width();
			$(this).css({'margin-left':'-'+(w-710)+'px'});
		}
	});

	$('.totop').click(function(){
		$('html, body').animate({scrollTop:0}, 'slow');
		return false;
	});
});


view code

原创文章:20130628-imageAutoSize-imagesLoaded-live ,未经许可,禁止转载,©版权所有
原文出处:前端开发博客 (http://caibaojian.com/445.html)