图片延迟加载并自适应裁剪

HTML

 <!--LazyLoad Image-->
        <div class="photo-wall">
            <div class="img-container">
                ![](http://upload-images.jianshu.io/upload_images/1288413-1049f2b7def0fb5c.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
            </div>
</div>

CSS

<style>
            .img-container {
                width: 220px;
                height: 220px;
                overflow: hidden;
            }

                .img-container > img {
                    position: relative;
                    display: block;
                    max-width: none;
                }
</style>

JS

//----UI object init
(function (window, $) {
    //check Zepto
    if (!$) {
        throw 'please include zepto first.';
    }
    window.UI = {
        //---无需参数的函数数组
        boot: [],
        $body: $('body'),
        $doc: $(document),
        $win: $(window),
        $content: $('body').children('.app-content').length ? $('body').children('.app-content') : $('body'),
        init: function () {
            this.boot.forEach(function (fn) {
                if ($.isFunction(fn)) {
                    fn();
                }
            });
        }
    };
    //---组件
    window.Component = function (fn) {
        var UI = window.UI;
        if (!UI) return;
        fn(UI, $);
    };

})(window, Zepto);

//----图片自适应容器裁剪
window.Component(function (UI, $) {
    UI.imageAdapt = function ($imgContainer) {
        var $img = $imgContainer.children('img');
        $img.on('load', function () {
            var rate = $img.width() / $img.height();
            var parentWidth = $imgContainer.width();
            var parentHeight = $imgContainer.height();
            var parentRate = parentWidth / parentHeight;
            if ($img.length > 0 && rate > parentRate) {
                //----horizental cut
                $img.height(parentHeight);
                $img.css("right", "50%");
                $img.css("margin-right", "-" + $img.width() / 2 + "px");
            } else {
                //----vertical cut
                $img.width(parentWidth);
                $img.css("top", "50%");
                $img.css("margin-top", "-" + $img.height() / 2 + "px");
            }
        });
    }
    UI.boot.push(function () {
        $('.img-container').each(function () {
            UI.imageAdapt($(this));
        });


        $(window).resize(function () {
            $('.img-container').each(function () {
                UI.imageAdapt($(this));
            });
        });
    });
});

//---图片延迟加载
window.Component(function (UI, $) {
    UI.lazyLoadImage = function ($content) {
        $('.img-container:not(.loaded)').each(function () {
            var $imgContainer = $(this);
            var $img = $imgContainer.children('img');
            if ($img && $imgContainer.offset().top < $content.height() + $content.offset().top) {
                $imgContainer.addClass('loaded');
                var src = $img.attr('data-src');
                $img.attr('src', src);
                //----图片加载的进行自适应裁剪
                $img.on('load', function () {
                    UI.imageAdapt($imgContainer);
                });
            }
        });
    };

    UI.boot.push(function () {
        //---滚动 加载
        UI.$content.scroll(function () {
            UI.lazyLoadImage(UI.$content);
        });

        UI.lazyLoadImage(UI.$content);
    });
});

//---初始化
window.UI.init();

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,692评论 1 92
  • HTML CSS JS
    codeice阅读 6,218评论 0 0
  • 那时的你牵起我的手 阳光正撒在身上 温暖的滋味你我共享 你的嘴角微微上扬 心中止不住的怯怯甜蜜 渐渐的你离开了 离...
    南瞑阅读 1,571评论 0 0
  • 宁静的阳光在午后被放亮,有一颗心在秋风中荡漾,无处安放。那年那月那日那时,都淡忘成夕阳下的昏黄,那人那屋那事,都是...
    磊磊excuseme阅读 1,292评论 0 4
  • 风渐渐地小了,天暗了下来。 陈永健心神不安的在办公室里走来走去。他看了一眼手表,离下班还有一个小时。他恨不得现在能...
    子谦的世界阅读 3,952评论 0 1

友情链接更多精彩内容