一、canvas图片加载

关于canvas加载,我的方法是,将文章中所有用到图片的地方,都用canvas代替,给canvas一个data-src,里面存放img的路径,通过canvas方法渲染图片。因为图片渲染需要时间,一般会给canvas一个背景,背景可以用gif图片。图片渲染好了之后把背景去掉。

我的canvas代码如下:

canvasload: function () {
//canvas加载图片
var imglength = $("#您的id").find("canvas").length;
if (imglength > ) {
$("#您的id").find("canvas").each(function () {
var imgSrc = $(this).data("src");
var imageObj = new Image();
imageObj.canvs = $(this)[];
var cvs = imageObj.canvs.getContext('2d');
if (cvs) {
imageObj.onload = function () {
imageObj.canvs.width = this.width;
imageObj.canvs.height = this.height;
cvs.drawImage(this, , );
$(imageObj.canvs).css("background-image", "none");
}
imageObj.src = imgSrc;
}
})
}
},

用法很简单,直接在页面中引用这个函数就可以了,注意,所有canvas外层要包裹一个ID

二、图片延迟加载

图片延迟加载方法有很多,下面简单介绍一个,可以引入一个jquery插件。这个插件是一个网友写的,试了一下能用,代码如下:

(function ($) {
$.fn.lazyloading = function (options) {
var defaults = {
preyimg: "/load.gif",
picpath: "data-original",
container: $(window),
loadfirst: false, //进入页面后是否加载当前页面的图片
defaultHeightID: "lazyloadingHeight"//页面上默认高度的input标签id
//imgPaddingID: "lazyloadingPadding"//img的padding值
};
var params = $.extend({}, defaults, options || {});
params.cache = [];
$(this).each(function () {
var node = this.nodeName.toLowerCase(), url = $(this).attr(params["picpath"]), preyimg = params["preyimg"];
var defaultheight = $("#" + params["defaultHeightID"]).val(); //, padding = $("#" + params["imgPaddingID"]).val(); //
//重组
var data = {
obj: $(this),
tag: node,
url: url,
preyimg: preyimg,
defaultheight: defaultheight
};
params.cache.push(data);
}); var init = function () {
$.each(params.cache, function (i, data) {
var thisImg = data.obj, tag = data.tag, url = data.url, preyimg = data.preyimg;
if (typeof (url) != "undefined")// 判断是否需要延迟加载
{
var parent1 = thisImg.parent(); //a
var Inner = null; //
if (parent1.is("a") == true) {//img wrap by a
Inner = parent1;
}
else {
Inner = thisImg;
}
var width = thisImg.attr("width") || thisImg.css("width");
var height = data.defaultheight || thisImg.css("height");
//if (i == 0) alert(data.defaultheight);
var attrheight = thisImg.attr("height");
if (attrheight != null) height = attrheight;
if (width != null && width.indexOf("px") > -) width.replace("px", "");
if (height != null && height.indexOf("px") > -) height.replace("px", "");
var divstr = "<div class='.loading' style='text-align: left;position:relative;float:left;width:" + width + "px;";
var HasHeight = true; //图片是否指定了高度
divstr = divstr + "height:" + height + "px;";
if (attrheight == null || attrheight == "") {
HasHeight = false;
} thisImg.css("position", "relative"); divstr = divstr + "' ></div>"
//修正外层div:text-align的影响
Inner.wrap(divstr);
//修正img外面不是a标签时parent()已经改变的问题
parent1 = thisImg.parent();
if (HasHeight == true) { parent1.attr("lazyloading_hasheight", ""); } //是否指定了高度
else { { parent1.attr("lazyloading_hasheight", ""); } }
parent1.append("<img class='loadhiddenimg' width='0' height='0' style='display:none;' src='' />");
thisImg.attr("src", preyimg);
thisImg.removeAttr("width").removeAttr("height");
thisImg.attr("width1", width).attr("height1", attrheight); ////thisImg.attr("width", "50px").attr("height", "50px"); //loading图大小
//thisImg.css("margin", "0 auto");
thisImg.css("margin", ((height / ) - ) + "px auto auto " + ((width / ) - ) + "px");
$(".lazyloading").css("display", "table"); //.css("position", "relative");
}
});
}
//动态显示数据
var loading1 = function () { };
var loading = function () {
//窗口的高度+看不见的顶部的高度=屏幕低部距离最顶部的高度
var thisButtomTop = parseInt($(window).height()) + parseInt($(window).scrollTop());
var thisTop = parseInt($(window).scrollTop()); //屏幕顶部距离最顶部的高度 $.each(params.cache, function (i, data) {
var thisImg = data.obj, tag = data.tag, url = data.url, post, posb; if (thisImg) {//对象不为空
if (typeof (url) != "undefined") {// 判断是否需要延迟加载
var PictureTop = parseInt(thisImg.offset().top);
//如果处理可见范围内,并且原图地址data-original不等于src,则加载图片
if (PictureTop >= thisTop && PictureTop <= thisButtomTop && thisImg.attr("data-original") != thisImg.attr("src")) {
var hiddenImg = thisImg.siblings("img.loadhiddenimg"); hiddenImg.load(function () { //隐藏图片加载完之后的回调函数
var width = thisImg.attr("width1");
var height = thisImg.attr("height1");
thisImg.attr("width", width).attr("height", height).removeAttr("width1").removeAttr("height1");
thisImg.css("margin", "0 auto");
if (thisImg.parent().attr("lazyloading_hasheight") == "") {//没有指定高度时,加载图片后去掉div高度自适应
if (thisImg.parent().is("a") == true) {
thisImg.parent().parent().css("height", "");
}
else {
thisImg.parent().css("height", "");
}
}
thisImg.load(function () {
if (thisImg.parent().is("a") == true) {
thisImg.parent().parent().css("height", thisImg.height());
}
else {
thisImg.parent().css("height", thisImg.height());
}
});
thisImg.css('opacity', '0.2');
thisImg.attr("src", hiddenImg.attr("src"));
thisImg.animate({ opacity: 1.0 });
if (thisImg.attr("alt") != "") {
thisImg.attr("title", thisImg.attr("alt"));
thisImg.attr("alt", "");
}
}).error(function () {
thisImg.error(function () {
thisImg.css("margin", "0 auto auto 0");
if (thisImg.parent().attr("lazyloading_hasheight") == "") {//没有指定高度时,加载图片后去掉div高度自适应
if (thisImg.parent().is("a") == true) {
thisImg.parent().parent().css("height", "");
}
else {
thisImg.parent().css("height", "");
}
}
});
thisImg.attr("src", hiddenImg.attr("src")); //alert("error");
if (thisImg.attr("alt") != "") {
thisImg.attr("title", thisImg.attr("alt"));
thisImg.attr("alt", "");
}
});
hiddenImg.attr("src", url);
}
}
}
});
};
//初始化
init();
//事件触发
//加载完毕即执行
if (params["loadfirst"] == true) loading();
//滚动执行
params.container.bind("scroll", loading).bind("resize", loading);
};
})(jQuery);

用法很简单:

<a href="#"><img  alt="haorooms博客" src="" data-original="http://www.haorooms.com/uploads/images/table.gif" class="lazyloading" /></a>
<script type="text/javascript">
$(function () {
$("img.lazyloading").lazyloading({ loadfirst: true });
}) </script>

touchweb手机网站图片加载方法(canvas加载和延迟加载)的更多相关文章

  1. js不需要知道图片宽高的懒加载方法(经过实际测试,不加宽高仍然是无法正常加载的,设置height:auto,height:100%,仍然显示高度为0)

    js不需要知道图片宽高的懒加载方法 懒加载是如何实现的? - 简书https://www.jianshu.com/p/e86c61468285找到一个不需要知道图片宽高的懒加载方法了(经过实际测试,不 ...

  2. javascript文件加载模式与加载方法

    加载方式 形象图像化方法,见 http://www.growingwiththeweb.com/2014/02/async-vs-defer-attributes.html 1. script标签, ...

  3. 手机网站keyup解决方案

    模糊搜索keyup无效,解决方案如下 //手机网站解决keyup的方法 $(function () { $('#repairsearch').bind('focus', filter_time); } ...

  4. 带你认识网站图片img懒加载和预加载的区别

    懒加载 什么是懒加载? 懒加载也就是延迟加载.当访问一个页面的时候,先把img元素或是其他元素的背景图片路径替换成一张大小为1*1px图片的路径(这样就只需请求一次,俗称占位图),只有当图片出现在浏览 ...

  5. fackbook的Fresco的多种图片加载方法以及解码过程

    上篇文章中我们提到了图片加载其实是用了三条线程,如果没看过的同学可以先了解下这里. fackbook的Fresco的Image Pipeline以及自身的缓存机制 那么今天我们就来探索一下如何在代码中 ...

  6. iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile?

      Apple官方的文档为生成一个UIImage对象提供了两种方法: 1. imageNamed,其参数为图片的名字: 2. imageWithContentsOfFile,其参数也是图片文件的路径. ...

  7. 10、 iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile

    Apple官方的文档为生成一个UIImage对象提供了两种方法: 1. imageNamed,其参数为图片的名字: 2. imageWithContentsOfFile,其参数是图片文件的路径. 两种 ...

  8. canvas加载图片需要二次刷新的问题

    如题:此问题我也经在百度问问上进行了解答.https://zhidao.baidu.com/question/1048045241465845579.html 好吧,难怪现在百度那么坑人,理论水军专家 ...

  9. UIImage加载图片的方式以及Images.xcassets对于加载方法的影响

    UIImage加载图片的方式以及Images.xcassets对于加载方法的影响 图片缓存 根据是否将创建好的对象缓存入系统内存,有两类创建UIImage对象的方法可选: 缓存:+ imageName ...

随机推荐

  1. 报错:OpenCV Error: Assertion failed (src.size() == dst.size() && src.type() == dst.ty pe()) in unknown function, file ..……

    在用cvDilate函数的时候,老是导致程序中断,报错如下: OpenCV Error: Assertion failed (src.size() == dst.size() && s ...

  2. Linux内核的引导

    1,当系统上电或复位时,CPU会将PC指针赋值为一个特定的地址0xFFFF0并执行该地址处的指令.在PC机中,该地址位于BIOS中,它保存在主板上的ROM或Flash中 2,BIOS运行时按照CMOS ...

  3. 关于异步请求AJAX的具体解释

    1,异步请求的方法步骤: 1,推断当前用户支持的浏览器类型 XMLHttpRequest:推断是否支持非IE浏览器,相应的创建方法:xmlhttp = new XMLHttpRequest(); wi ...

  4. PropertyGrid—隐藏某些Public属性

    1.定义一个继承ControlDesigner 的类 public class MyControlDesigner:System.Windows.Forms.Design.ControlDesigne ...

  5. VC++动态链接库(DLL)编程深入浅出(二)

    好,让我们正式进入动态链接库的世界,先来看看最一般的DLL,即非MFC DLL 上节给大家介绍了静态链接库与库的调试与查看,本节主要介绍非MFC DLL. 4.非MFC DLL 4.1一个简单的DLL ...

  6. Spring boot Security Disable security

    When I use security.basic.enabled=false to disable security on a Spring Boot project that has the fo ...

  7. 怎样mac上安装apk到连接数据线的手机

    高大上的mac俺也用了一段时间了.不知道大家有木有同一个烦恼.曾经在win上的时候仅仅要安装了应用宝之类的手机助手.就能够双击APK,直接安装到连接数据线的手机上,非常方便哈,可是mac上不行.近期找 ...

  8. iOS开发数据持久化技术02——plist介绍

    有疑问的请加qq交流群:390438081 我的QQ:604886384(注明来意) 微信:niuting823 1. 简单介绍:属性列表是一种xml格式的文件.扩展名.plist: 2. 特性:pl ...

  9. Koa2 + Mongoose + Log4js 持久化日志

    代码地址如下:http://www.demodashi.com/demo/12466.html  之前做的项目是采用 Express 框架进行搭建的,其中的日志管理采用了 winston + Post ...

  10. android:scrollbar的一些属性

    1. activity_maim.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android ...