标签前添加如下代码:
//引入jquery
<script src="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>
//引入lazyload
<script src="https://cdn.bootcss.com/jquery_lazyload/1.9.7/jquery.lazyload.min.js"></script>
//lazyload设置图片显示方式
<script>$(function() {$("img").lazyload({effect: "fadeIn", threshold: 200});});</script>
1.修改文章内图片,打开Var/hyperdown.php第484-496行,将img的src=改成data-original=,改后源码如下:
    $text = preg_replace_callback(
        "/!\[((?:[^\]]|\\\\\]|\\\\\[)*?)\]\[((?:[^\]]|\\\\\]|\\\\\[)+?)\]/",
        function ($matches) use ($self) {
            $escaped = htmlspecialchars($self->escapeBracket($matches[1]));
            $result = isset( $self->_definitions[$matches[2]] ) ?
                "<img src="你的loading图片" data-original=\"{$self->_definitions[$matches[2]]}\" alt=\"{$escaped}\" title=\"{$escaped}\">"
                : $escaped;
            return $self->makeHolder($result);
        },
        $text
    );
我因为用到采取图片作为thumb图,故未设置src,直接空掉了。
2.修改评论头像,打开var/Widget/Abstract/comments.php第395-407行,将src=改成data-original=,然后添加src="你的loading图片",修改后代码如下:
public function gravatar($size = 32, $default = NULL)
{
    if ($this->options->commentsAvatar && 'comment' == $this->type) {
        $rating = $this->options->commentsAvatarRating;
        
        $this->pluginHandle(__CLASS__)->trigger($plugged)->gravatar($size, $rating, $default, $this);
        if (!$plugged) {
            $url = Typecho_Common::gravatarUrl($this->mail, $size, $rating, $default, $this->request->isSecure());
            echo '<img class="avatar" src="你的loading图片" data-original="' . $url . '" alt="' .
            $this->author . '" width="' . $size . '" height="' . $size . '" />';
        }
    }
}
3.修改后台评论管理,进入admin/Manage-comments.php,在最后加入
//引入jquery
<script src="https://cdn.bootcss.com/jquery/2.1.4/jquery.min.js"></script>
//引入lazyload
<script src="https://cdn.bootcss.com/jquery_lazyload/1.9.7/jquery.lazyload.min.js"></script>
//lazyload设置图片显示方式
<script>$(function() {$("img").lazyload({effect: "fadeIn", threshold: 200});});</script>
在主题文件footer.php的</body>标签前添加如下代码:
<script type="text/javascript">
$(document).on('pjax:complete', function() {//pjax回调
    $("img").lazyload({
    effect: "fadeIn",
    threshold: 200
    });
});
</script>
在ajax评论和ajax翻页回调成功后加入
$("img").lazyload({effect: "fadeIn", threshold: 200, failurelimit: 2});
比如:
    <script type="text/javascript">
        $(function() {
            url = $.ajax({
                type: "get",
                url: "img_url.txt",
                async: true,
                success: function() {
                    $("img").lazyload({effect: "fadeIn", threshold: 200, failurelimit: 2});
                }
            });
        });
    </script>
好了,大功告成!这样直接从源头实现图片lazyload,达到优化目的!
 
   
         
回去试一下
1.2的文件在哪改
大佬 ,1.2版能用不?我咋没成……
可以啊,我现在就用着,不过1.2的文件位置有变动
1.2的文件在哪改
具体的有点多,有空细说
换了个轻量的
测试不行,伤脑筋找了好几个办法了,看不懂
好吧,我现在就这么用的
我在研究这个图片懒加载,请问这个懒加载必须需要修改 typecho 程序文件吗?
也可以不修改,利用function函数主题加载时候更换图片url,我因为是自用的,比较喜欢改底层,这样直接输出更高效
嗯嗯嗯嗯,我抽空把懒加载加到主题里面去了,js部分是F12偷师贵站代码!
拿去用,呵呵
当然js也稍作了修改,加了个延时处理,让懒加载能可视化。
对了,我发评论的时候发现了个问题,你的站点不能用emoji表情。
如果评论中有emoji,那么emoji之后的文字将不显示。
我的MySQL的问题,用的utf8,不兼容4字节,所以不支持emoji。