本文分享製作 360 度全景展示的完整思路,並整理實作過程中必須留意的幾個關鍵問題。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="style.css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>360度全景展示</title>
<script>
window.onload=function () {
var oImg=document.getElementById('img1');
var aImg=document.getElementsByTagName('img');
var lastimage=oImg;
var x=0;
var speed=0;
var lastx=0;
var timer=null;
var temp=0;
for(i=0;i<77;i++) {
var oNewImage=document.createElement('img');
oNewImage.src='img/miaov ('+i+').jpg';
oNewImage.style.display='none';
document.body.appendChild(oNewImage);
}
document.onmousedown=function(ev) {
clearInterval(timer);
var oEvent=ev||event;
var disx=oEvent.clientX-x;
document.onmousemove=function(ev) {
var oEvent=ev||event;
x=oEvent.clientX-disx;
setMove();
speed=x-lastx;
lastx=x;
return false;
}
document.onmouseup=function() {
document.onmousemove=null;
document.onmouseup=null;
timer=setInterval(function(){
x+=speed;
setMove();
document.title=speed;
},30);
}
function setMove() {
if(speed>0){speed--;}
else if(speed==0){clearInterval(timer);}
else {speed++;}
temp=-x;
if(temp>0) {
temp=-x%77;
} else {
temp=-x+(-Math.floor(-x/77)*77);
}
lastimage.style.display='none';
aImg[temp].style.display='block';
lastimage=aImg[temp];
document.title=temp;
}
return false;
}
}
</script>
</head>
<body>
<img id="img1" src="img/miaov (0).jpg" />
</body>
</html>
實作上最關鍵的是「一次只顯示一張」:先把 77 張全部隱藏,切換時只把上一張設為 none、下一張設為 block,避免瀏覽器同時繪製大量圖片而卡頓。以上就是本文全部內容,希望對你的學習有所幫助。