码迷,mamicode.com
首页 > 移动开发 > 详细

canvas移动端常用技巧图片loading

时间:2017-04-06 18:33:11      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:标题   var   ack   ctc   code   script   find   min   nload   

核心知识点:drawImage

作用:将图片加载在canvas

html:

<canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>

js:

技术分享
<script type="text/javascript">

var c=document.getElementById("myCanvas");
var cxt=c.getContext("2d");
var img=new Image()
img.src="flower.png"
cxt.drawImage(img,0,0);

</script>
技术分享

移动端图片loading实例

需求:需要给一个列表中的图片添加加载效果

html

技术分享
<section class="productul" id="productul">
     <ul>
         <li>
            <a href="#">
             <div class="triangle-topleft"></div>
             <span class="shuxing" data_url="productinfo.html">专属</span>
             <div class="leftimages fl"><canvas data-src="images/product/product1.png" ></canvas></div>
             <div class="productcontent fr">
                 <p class="ptitle pl10">标题</p>
                  <p class="pdes pl10">简介这里简介这里简介这里简介这里简介这里简介这里简介这里简介介这里简介</p>
                  <p class="pprice pl10">价格:<span class="green">¥5000</span></p>
             </div>
           </a>
         </li>
     </ul>    
</section>
技术分享

重点css

img{width:100px;birder:0;}
canvas{width:100px;min-height:100px;background:#fff url("../images/loading.gif") center center no-repeat; background-size:15px auto; }

js

技术分享
var imglength = $("#productul").find("canvas").length;
            if (imglength > 0) {
                $("#productul").find("canvas").each(function () {
                    var imgSrc = $(this).data("src");
                    var imageObj = new Image();
                    imageObj.canvs = $(this)[0];
                    var cvs = imageObj.canvs.getContext(‘2d‘);
                    if (cvs) {
                        imageObj.onload = function () {
                            imageObj.canvs.width = this.width;
                            imageObj.canvs.height = this.height;
                            cvs.drawImage(this, 0, 0);
                            $(imageObj.canvs).css("background-image", "none");
                        }
                        imageObj.src = imgSrc;
                    }
                })
            }
        }
技术分享

 原文链接:http://www.cnblogs.com/leejersey/p/4714613.html

canvas移动端常用技巧图片loading

标签:标题   var   ack   ctc   code   script   find   min   nload   

原文地址:http://www.cnblogs.com/liudd595/p/6674757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!