canvas移動端常用技巧圖片loading

來源:互聯網
上載者:User

標籤:標題   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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.