日期:2012-3-26 来源:GBin1.comjavascript
360度的全景图片效果经常能够用到给客户作产品展现,今天这里咱们推荐一个很是不错的来自Robert Pataki的360全景幻灯实现教程,这里教程中将使用javascript来打造一个超酷的全景幻灯实现,相信你们必定会喜欢的!html
在这个教程中没有使用到任何插件,咱们将使用HTML,css和javascript来实现,固然,也使用是jQuery这个框架!java
咱们将使用预先按照360生成的图片进行轮播来实现动画展现效果。包含了180个图片。因此加载时间可能比较长。jquery
咱们将在css代码中添加media queries,来使得这个效果能够同时在ipad和iphone上实现。web
咱们添加js,css,图片目录。css目录中包含了reset.css。js中包含了jQuery。代码文件以下:canvas
建立一个HTML文件index.html。在<head>中咱们设置了移动设备的viewport,使得内容不支持缩放。添加俩个文件数组
reset.css和threesixty.css。包含了自定义的css样式。app
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0" /> <title>360</title> <link rel="stylesheet" href="css/reset.css" media="screen" type="text/css" /> <link rel="stylesheet" href="css/threesixty.css" media="screen" type="text/css" /> </head> <body> </body> </html>
建立一个<div>来容纳幻灯。其中包含一个<ol>,用来包含图片序列<li>,同时也包含了一个<span>来显示进度条。咱们将使用javascript来动态加载图片。框架
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0" /> <title>360</title> <link rel="stylesheet" href="css/reset.css" media="screen" type="text/css" /> <link rel="stylesheet" href="css/threesixty.css" media="screen" type="text/css" /> </head> <body> <div id="threesixty"> <div id="spinner"> <span>0%</span> </div> <ol id="threesixty_images"></ol> </div> </body> </html>
代码最后,咱们添加jQuery用来处理互动,threesixity.js用来处理图片幻灯。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0" /> <title>360</title> <link rel="stylesheet" href="css/reset.css" media="screen" type="text/css" /> <link rel="stylesheet" href="css/threesixty.css" media="screen" type="text/css" /> </head> <body> <div id="threesixty"> <div id="spinner"> <span>0%</span> </div> <ol id="threesixty_images"></ol> </div> <script src="js/heartcode-canvasloader-min.js"></script> <script src="js/jquery-1.7.min.js"></script> <script src="js/threesixty.js"></script> </body> </html>
咱们添加threesixty.css文件。reset.css用来设置缺省的样式。首先定义#threesixty包装。缺省的图片幻灯是960x450。水平垂直居中。
#threesixty { position:absolute; overflow:hidden; top:50%; left:50%; width:960px; height:540px; margin-left:-480px; margin-top:-270px; } #threesixty_images { display: none; }
为了幻灯针对不一样的设备都能有完美显示。咱们这里添加media queries。使用max-device-width和orientataion属性而且联合使用and操做。
@media screen and (max-device-width: 1024px) and (orientation:portrait) { #threesixty { width:720px; height:450px; margin-left:-360px; margin-top:-225px; } } @media screen and (max-device-width: 480px) and (orientation:landscape), screen and (-webkit-min-device-pixel-ratio: 2) and (orientation:landscape) { #threesixty { width:360px; height:225px; margin-left:-180px; margin-top:-113px; } } @media screen and (max-device-width: 480px) and (orientation:portrait), screen and (-webkit-min-device-pixel-ratio: 2) and (orientation:portrait) { #threesixty { width:320px; height:200px; margin-left:-160px; margin-top:-100px; } }
全部的图片都被存放在<ol>中。咱们不但愿都显示,因此定义current-image和previous-image来控制显示。
#threesixty img { position:absolute; top:0; width:100%; height:auto; } .current-image { visibility:visible; width:100%; } .previous-image { visibility:hidden; width:0; }
#spinner { position:absolute; left:50%; top:50%; width:90px; height:90px; margin-left:-45px; margin-top:-50px; display:none; } #spinner span { position:absolute; top:50%; width:100%; color:#333; font:0.8em Arial, Verdana, sans; text-align:center; line-height:0.6em; margin-top:-0.3em; }
建立一个threesixty.js在js文件夹中。添加jQuery document ready方法。
拖放能够告诉咱们用户是否使用pointer。 使用speedMultiplier咱们能够设置图片幻灯速度。同时咱们使用变量来保存pointer位置。timer将会跟踪pointer变化。咱们定义变量来保存而且跟踪变化来计算图片加载。
$(document).ready(function () { var ready = false, dragging = false, pointerStartPosX = 0, pointerEndPosX = 0, pointerDistance = 0, monitorStartTime = 0, monitorInt = 10, ticker = 0, speedMultiplier = 10, spinner, totalFrames = 180, currentFrame = 0, frames = [], endFrame = 0, loadedImages = 0; });
咱们建立addSpinner方法来在#spinner中添加CanvasLoader实例。以下:
function addSpinner () { spinner = new CanvasLoader("spinner"); spinner.setShape("spiral"); spinner.setDiameter(90); spinner.setDensity(90); spinner.setRange(1); spinner.setSpeed(4); spinner.setColor("#333333"); spinner.show(); $("#spinner").fadeIn("slow"); };
加载图片方法建立了包含<img>的<li>。 loadedimages变量生成图片名字,每次图片加载后自动添加,成功后,调用imageLoaded方法。
function loadImage() { var li = document.createElement("li"); var imageName = "img/threesixty_" + (loadedImages + 1) + ".jpg"; var image = $('<img>').attr('src', imageName).addClass("previous-image").appendTo(li); frames.push(image); $("#threesixty_images").append(li); $(image).load(function() { imageLoaded(); }); };
这里加载的图片太多,所以咱们循环调用loadImage。图片加载处理将加载进度写到#spiner <span>。一旦加载完毕,咱们将显示第一个张图片,而且加载进度条消失。
function imageLoaded() { loadedImages++; $("#spinner span").text(Math.floor(loadedImages / totalFrames * 100) + "%"); if (loadedImages == totalFrames) { frames[0].removeClass("previous-image").addClass("current-image"); $("#spinner").fadeOut("slow", function(){ spinner.hide(); showThreesixty(); }); } else { loadImage(); } };
使用showThreesixty方法来顺畅的显示图片幻灯过渡效果。
function imageLoaded() { loadedImages++; $("#spinner span").text(Math.floor(loadedImages / totalFrames * 100) + "%"); if (loadedImages == totalFrames) { frames[0].removeClass("previous-image").addClass("current-image"); $("#spinner").fadeOut("slow", function(){ spinner.hide(); showThreesixty(); }); } else { loadImage(); } }; function showThreesixty () { $("#threesixty_images").fadeIn("slow"); ready = true; }; addSpinner(); loadImage();
via netmagazine.com