1.canvas圖片預加載及進度條的實現
/*star *loading模塊 *實現圖片的預加載,并顯示進度條 *參數:圖片數組對象,加載完成的回調函數 */ function loadImages(sources,callback){ var loadedImages = 0; var numImages = 0; ctx.font="14px bold"; ctx.lineWidth=5; var clearWidth=canvas.width; var clearHeight=canvas.height; // get num of sources for (var src in sources) { numImages++; } for (var src in sources) { images[src] = new Image(); //當一張圖片加載完成時執行 images[src].onload = function(){ //重繪一個進度條 ctx.clearRect(0,0,clearWidth,clearHeight); ctx.fillText("Loading:"+loadedImages+"/"+numImages,200,280); ctx.save(); ctx.strokeStyle="#555"; ctx.beginPath(); ctx.moveTo(200,300); ctx.lineTo(600,300); ctx.stroke(); ctx.beginPath(); ctx.restore(); ctx.moveTo(200,300); ctx.lineTo(loadedImages/numImages*400+200,300); ctx.stroke(); //當所有圖片加載完成時,執行回調函數callback if (++loadedImages >= numImages) { callback(); } }; //把sources中的圖片信息導入images數組 images[src].src = sources[src]; } } //定義預加載圖片數組對象,執行loading模塊 window.onload = function(){ var sources = { PaperBoy1: "images/run/PaperBoy1.png", PaperBoy2: "images/run/PaperBoy2.png", PaperBoy3: "images/run/PaperBoy3.png", PaperBoy4: "images/run/PaperBoy4.png", PaperBoy5: "images/run/PaperBoy5.png", PaperBoy6: "images/run/PaperBoy6.png", PaperBoy7: "images/run/PaperBoy7.png", PaperBoy8: "images/run/PaperBoy8.png", PaperBoy9: "images/run/PaperBoy9.png", PaperBoy10: "images/run/PaperBoy10.png", PaperBoy11: "images/run/PaperBoy11.png", PaperBoy12: "images/run/PaperBoy12.png", PaperBoy13: "images/run/PaperBoy13.png", PaperBoy14: "images/run/PaperBoy14.png", PaperBoy15: "images/run/PaperBoy15.png", PaperBoy16: "images/run/PaperBoy16.png", PaperBoy17: "images/run/PaperBoy17.png", PaperBoy18: "images/run/PaperBoy18.png", PaperBoy19: "images/run/PaperBoy19.png", PaperBoy20: "images/run/PaperBoy20.png", PaperBoy21: "images/run/PaperBoy21.png", PaperBoy22: "images/run/PaperBoy22.png", PaperBoy23: "images/run/PaperBoy23.png", PaperBoy24: "images/run/PaperBoy24.png", _Street:"images/_Street.png", AD:"images/AD.png", building:"images/building.png", cloud:"images/cloud.png" }; //執行圖片預加載,加載完成后執行main loadImages(sources,main); }; /*end*/2.JS實現跨瀏覽器添加事件與移除事件怎樣做才最優?
一般的兼容做法,如下:
跨瀏覽器添加事件
//跨瀏覽器添加事件 function addEvent(obj,type,fn){ if(obj.addEventListener){ obj.addEventListener(type,fn,false); }else if(obj.attachEvent){//IE obj.attchEvent("on"+type,fn); } }
跨瀏覽器移除事件
//跨瀏覽器移除事件 function removeEvent(obj,type,fn){ if(obj.removeEventListener){ obj.removeEventListener(type,fn,false); }else if(obj.detachEvent){//兼容IE obj.detachEvent("on"+type,fn); } }
推薦寫法
function addEvent( obj, type, fn ) { if ( obj.attachEvent ) { obj["e"+type+fn] = fn; obj[type+fn] = function(){obj["e"+type+fn]( window.event );} obj.attachEvent( "on"+type, obj[type+fn] ); } else obj.addEventListener( type, fn, false ); } function removeEvent( obj, type, fn ) { if ( obj.detachEvent ) { obj.detachEvent( "on"+type, obj[type+fn] ); obj[type+fn] = null; } else obj.removeEventListener( type, fn, false ); }
參考地址
addEvent() recoding contest entry
addEvent() – Follow Up
眾所周知,Ajax是通過創建XMLHttpRequest對象或ActiveXObject來連接服務器、發送請求以及響應數據,但它卻不能跨域。而在分布式系統中我們又需要跨域發送接受數據,于是jsonp出現了...
它是一種跨域請求方式,主要利用了script標簽里的src屬性,該屬性可以跨域發送請求,然后服務器返回js代碼,網頁端便響應其信息,然后我們可以對其傳過來的js代碼做處理提取其中的信息。
jsonp發送請求只需在src后面添加“?callback=函數名”就可以,例如“http://www.item.com/list?callback=myfunction",則只需在服務端接受參數myfunction并將函數名與想要返回的數據拼接就可以例如在java中響應該請求,可以獲取參數callback的值myfunction,再拼接成myfunction+"("+data+")"格式返回就行,在前端寫同名函數接受data并處理就可以了。但在jquery中對jsonp進行了封裝,返回函數就是success,數據也用success接受。
例如:
前端代碼:
//發送請求 $.ajax({ //url:"http://localhost:8081/rest/itemcat/list?callback=getMessage", url:"http://localhost:8081/rest/itemcat/message", type:"get", cache:false, dataType:"jsonp", jsonp:"callback", //這里定義了callback的參數名稱,以便服務獲取callback的函數名即getMessage jsonpCallback:"getMessage", //這里定義了jsonp的回調函數 success:function(data){ alert("success:"+data); }, error:function(){ alert("發生異常"); } }); function getMessage(jsonp){ alert("message:"+jsonp); }
這樣發出的請求為:http://localhost:8081/rest/itemcat/message?callback=getMessage
jsonp:"callback",
jsonpCallback:"getMessage",
這兩個參數的值會自動拼接在url后面,所以用jquery的$.ajax方法發出的url可以不用在后面添加callback=getMessag,返回函數則變為了success而不是getMessage
創建API地圖帶有點標記
根據瀏覽器定位獲取當前經緯度
高德地圖根據瀏覽器定位獲取當前經緯度API案例地址:http://lbs.amap.com/api/javascript-api/example/location/browser-location
高德開放平臺:http://lbs.amap.com/api
1.HTML5中的Canvas標簽的創建
window.onload = function(){ createCanvas(); } function createCanvas(){ var canvas_width= 200, canvas_height = 200; document.body.innerHTML = ""; }
2.HTML5Canvas標簽繪制圖形
var canvas_width= 500, canvas_height = 500; var mycanvas, context; window.onload = function(){ createCanvas(); drawRect(); } function createCanvas(){ document.body.innerHTML = ""; mycanvas = document.getElementById("mycanvas"); context = mycanvas.getContext("2d"); } function drawRect(){ context.fillStyle ="#FF0000"; //context.rotate(45);//旋轉45度 //context.translate(200,200);//移動 //context.scale(2,0.5);//縮放 context.fillRect(0,0,200,200); }
3.HTML5Canvas標簽繪制圖片
var canvas_width= 500, canvas_height = 500; var mycanvas, context; window.onload = function(){ createCanvas(); drawImage(); } function createCanvas(){ document.body.innerHTML = ""; mycanvas = document.getElementById("mycanvas"); context = mycanvas.getContext("2d"); } function drawImage(){ var img = new Image(); img.onload = function(){ context.drawImage(img,0,0); } img.src = "1.png"; }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/86543.html
1.canvas圖片預加載及進度條的實現 /*star *loading模塊 *實現圖片的預加載,并顯示進度條 *參數:圖片數組對象,加載完成的回調函數 */ function loadImages(sources,callback){ var loadedImages = 0; var numImages = 0; ctx.font=14px ...
1.canvas圖片預加載及進度條的實現 /*star *loading模塊 *實現圖片的預加載,并顯示進度條 *參數:圖片數組對象,加載完成的回調函數 */ function loadImages(sources,callback){ var loadedImages = 0; var numImages = 0; ctx.font=14px ...
閱讀 3499·2023-04-25 15:52
閱讀 581·2021-11-19 09:40
閱讀 2572·2021-09-26 09:47
閱讀 1023·2021-09-22 15:17
閱讀 3548·2021-08-13 13:25
閱讀 2200·2019-08-30 15:56
閱讀 3460·2019-08-30 13:56
閱讀 2095·2019-08-30 11:27