美文网首页
图片延迟加载并自适应裁剪

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

作者: codeice | 来源:发表于2016-06-20 11:55 被阅读58次

HTML

 <!--LazyLoad Image-->
        <div class="photo-wall">
            <div class="img-container">
                ![](https://img.haomeiwen.com/i1288413/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();

相关文章

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

    HTML CSS JS

  • 懒加载和预加载

    1)概念: 懒加载也叫延迟加载:JS图片延迟加载,延迟加载图片或符合某些条件时才加载某些图片。预加载:提前加载图片...

  • JS

    JS 懒加载,预加载 概念:懒加载也叫延迟加载:JS图片延迟加载,延迟加载图片或符合某些条件时才加载某些图片。预加...

  • 图片懒加载

    懒加载与预加载的基本概念。 懒加载也叫延迟加载:JS图片延迟加载 延迟加载图片或符合某些条件时才加载某些图片。 预...

  • js 实现图片懒加载

    一、懒加载 懒加载也叫延迟加载,Js中图片的延迟加载,延迟图片需要符合某些条件时才加载;对于图片过多的页面,为了加...

  • javascript图片懒加载与预加载的分析

    懒加载与预加载的基本概念。 懒加载也叫延迟加载:前一篇文章有介绍:JS图片延迟加载 延迟加载图片或符合某些条件时才...

  • 关于加载js页面

    图片自适应宽度(webView加载的时候) 设置字体加载图片(label或者textView加载的时候)

  • 业务中常用的原生函数

    一、延迟加载图片 图片进入视觉时开始加载 componentDidMount() { this.t...

  • WKWebView常用介绍

    加载Html代码 加载的状态回调 (WKNavigationDelegate) WKWebView图片自适应 超过...

  • 图片延迟加载

    (function () {("img").delayLoading({defaultImg: "images/l...

网友评论

      本文标题:图片延迟加载并自适应裁剪

      本文链接:https://www.haomeiwen.com/subject/quewdttx.html