2015-10-25

javascript:パノラマ写真をドラッグでスクロール(Jqueryプラグイン)

サンプル

javascript:パノラマ写真をドラッグでスクロール を Jqueryプラグインに変更。

プラグインよりもjqueryを先に読み込むこと[]。


2~3機能も追加した。

  • スクロールボタンを追加(オプションで非表示も可能)
  • 切り替え画像に対応(オプション)。例えば、説明を追記した画像を切り替え画像に用いる。
  • 360°パノラマ対応(オプション)

◆html

11~13行目:width, height, src で画像を表示するサイズと画像を定義する(必須)
19~25行目:オプションでパラメータを定義する。
  1. <html>
  2. <head>
  3. <title>dragPanorama</title>
  4. <meta http-equiv="imagetoolbar" content="no" />
  5. <script src="jquery.js"></script>
  6. <script src="jQuery.dragPanorama.js"></script>
  7.  
  8. <script>
  9. $(function() {
  10. $("#panorama_wrapper").dragPanorama({
  11. width: 800,
  12. height: 400,
  13. src: "001.jpg"
  14. });
  15. $("#panorama_wrapper2").dragPanorama({
  16. width: 1000,
  17. height: 300,
  18. src: "00.jpg",
  19. round: true,
  20. buttonSize: "20px",
  21. buttonTextLeft: "左",
  22. buttonTextRight: "右",
  23. buttonTextPause: "停止",
  24. switchText: "表示切替",
  25. src2: "00_2.jpg",
  26. });
  27. });
  28. </script>
  29.  
  30. </head>
  31.  
  32. <body>
  33. <div align="center">
  34. <h1>九十九谷</h1>
  35. <div id="panorama_wrapper"></div>
  36. <br/>
  37. <h1>谷川岳</h1>
  38. <div id="panorama_wrapper2"></div>
  39. </div>
  40. </body>
  41. </html>

◆javascript(panorama.js)

10~29行のパラメータをオプションで設定できる。


  1. (function($) { // 無名関数でラップしてプラグインを独立させておく。jQueryキーワードの代わりにドル記号($)を使用する。
  2.  
  3. $.fn.dragPanorama = function(opts) { // プラグインを定義する。
  4. return this.each(function() { // each();で回して全ての要素に対して同様に処理していく
  5. new $.Panorama(this,opts);
  6. });
  7. };
  8.  
  9. var defaults = {
  10. dx: 0, // マウスダウンした場所の画像の座標
  11. xcache: 0, // 画像の左端のスクリーンの左端に対する座標
  12. dragObj: false, // ドラッグの状態を表す。 null→ドラッグしていない状況
  13. width: 320,
  14. height: 240,
  15. round: false, // 360°パノラマのときはtrue
  16. scroll: true, // 自動スクロールをするときは true
  17. speed: 100, // 自動スクロールのスピード
  18. mouseoverVisibility: "visible", // mouseover の時のスクロールボタンの表示/非表示
  19. mouseoutVisibility: "hidden", // mouseout の時のスクロールボタンの表示/非表示
  20. buttonSize: "100px", // スクロールボタンの大きさ
  21. buttonColor: "grey", // スクロールボタンの色
  22. buttonOpacity: "0.5", // スクロールボタンの透明度
  23. buttonTextLeft: "<", // スクロールボタン(左)のテキスト
  24. buttonTextRight: ">", // スクロールボタン(右)のテキスト
  25. buttonTextPause: "=", // スクロールボタン(pause)のテキスト
  26. switchSize: "18px", // 切り替えボタンのサイズ
  27. switchText: "switch", // 切り替えボタンのテキスト
  28. src2: "", // 切り替え用の画像
  29. durationTime: 1000 // 画像切り替え時間
  30. };
  31.  
  32. $.Panorama = function(elements, opts){
  33.  
  34. this.opts = $.extend({}, defaults, opts); // デフォルトオプションと渡されたオプションのマージ
  35.  
  36. this.$id = $(elements);
  37. this.$id.width(this.opts.width).height(this.opts.height);
  38. this.srcCurrent = this.opts.src;
  39. var self = this; // 以下の関数で用いるために要素(this)を変数(self)に退避
  40.  
  41. var baseBlockCss = {
  42. position: "absolute",
  43. cursor: "pointer",
  44. height: "100%",
  45. width: "100%"
  46. };
  47. var baseInlineCss = {
  48. position: "absolute",
  49. cursor: "pointer",
  50. };
  51.  
  52. this.$id.css({
  53. overflow: "hidden",
  54. position: "relative",
  55. textAlign: "left",
  56. }).append(
  57. $('<div></div>').css(baseBlockCss) // 画像用の<div>
  58. ).append(
  59. $('<div></div>').css(baseBlockCss) // 切り替え画像用の<div>
  60. );
  61.  
  62. if ( this.opts.scroll == true || this.opts.src2) {
  63. this.$id.mouseover(function(){
  64. $(this).find('span').css({
  65. visibility: self.opts.mouseoverVisibility
  66. })
  67. }).mouseout(function(){
  68. $(this).find('span').css({
  69. visibility: self.opts.mouseoutVisibility
  70. })
  71. });
  72. }
  73.  
  74. if ( this.opts.scroll == true ) {
  75. var $table = $('<table></table>').css(baseBlockCss).css({
  76. border: "0",
  77. borderSpacing: "0",
  78. tableLayout: "fixed", // <td>を均等に割り付ける(キャメルケースで記載)
  79. fontSize: self.opts.buttonSize,
  80. color: self.opts.buttonColor,
  81. opacity: self.opts.buttonOpacity,
  82. verticalAlign: "middle",
  83. visibility: self.opts.mouseoutVisibility
  84. }).append(
  85. $('<tr></tr>').append(
  86. $('<td></td>').css({
  87. textAlign: "left",
  88. }).append(
  89. $('<span></span>', { text: self.opts.buttonTextLeft}).click(function(){
  90. self.automovePrc(-1);
  91. })
  92. )
  93. ).append(
  94. $('<td></td>').css({
  95. textAlign: "center"
  96. }).append(
  97. $('<span></span>', { text: self.opts.buttonTextPause}).click(function(){
  98. clearInterval(self.timer);
  99. self.timer = "";
  100. })
  101. )
  102. ).append(
  103. $('<td></td>').css({
  104. textAlign: "right"
  105. }).append(
  106. $('<span></span>', { text: self.opts.buttonTextRight}).click(function(){
  107. self.automovePrc(1);
  108. })
  109. )
  110. )
  111. );
  112. this.$id.append($table);
  113. }
  114.  
  115. if ( this.opts.src2 ) { // 画像の切り替え
  116. var $switch = $('<span></span>', { text: self.opts.switchText }).css(baseInlineCss).css({
  117. fontSize: self.opts.switchSize,
  118. color: self.opts.buttonColor,
  119. opacity: self.opts.buttonOpacity,
  120. verticalAlign: "top",
  121. textAlign: "left",
  122. visibility: self.opts.mouseoutVisibility
  123. }).click(function(){
  124. if ( self.$id.find('div:last').css("opacity") == 0 ) {
  125. self.$id.find('div:last').animate({opacity: 1.0},self.opts.durationTime);
  126. } else {
  127. self.$id.find('div:last').animate({opacity: 0.0},self.opts.durationTime);
  128. }
  129. });
  130. this.$id.append($switch);
  131. }
  132.  
  133. this.img = new Image();
  134.  
  135. this.img.onload = function(){ // imageが読み込まれたら、imageの幅を取得して、maxLeftを定義する。
  136.  
  137. var imgRatio = self.$id.height() / self.img.height; // 画像の高さを合わせて表示するための変形比率を定義
  138. self.imgWidth = self.img.width * imgRatio; // 変形画像の幅
  139. self.imgHeight = self.$id.height(); // 変形画像の高さ
  140.  
  141. if ( self.opts.round == false ) {
  142. self.maxLeft = self.$id.width() - self.imgWidth;
  143. // もっとも左に移動したときの画像の左端のスクリーンの左端に対する座標<0
  144. } else { // 360°パノラマの場合
  145. self.maxLeft = self.$id.width() - self.imgWidth * 2;
  146. // もっとも左に移動したときの画像の左端のスクリーンの左端に対する座標<0
  147. }
  148. self.$id.find('div:first').css({ // 画像用の<div>を設定
  149. opacity: 1.0,
  150. backgroundImage: "url(" + self.opts.src + ")", // キャメルケースで記載("background-image"→backgroundImage)
  151. backgroundSize: self.imgWidth + "px," + self.imgHeight + "px", // 変形比率で表示
  152. width: self.imgWidth*2 + "px"
  153. });
  154. self.$id.find('div:last').css({ // 切り替え画像用の<div>を設定
  155. opacity: 0.0,
  156. backgroundImage: "url(" + self.opts.src2 + ")", // キャメルケースで記載("background-image"→backgroundImage)
  157. backgroundSize: self.imgWidth + "px," + self.imgHeight + "px", // 変形比率で表示
  158. width: self.imgWidth*2 + "px"
  159. });
  160. }
  161.  
  162. this.img.src = this.opts.src;
  163. this.maxLeft = self.maxLeft;
  164. this.imgWidth = self.imgWidth;
  165. this.imgHeight = self.Height;
  166. this.$id.mousedown( function(e) {
  167. self.opts.dragObj = true;
  168. var x = e.pageX ; // マウスダウンした場所のスクリーンの座標
  169. self.opts.dx = x - self.opts.xcache; // マウスダウンした場所の画像の座標
  170. return false; // ドラッグで文字が選択されないようにする。
  171. });
  172. this.$id.mouseup( function(e) {
  173. self.opts.dragObj = false;
  174. });
  175. this.$id.mousemove( function(e) {
  176. if(!self.opts.dragObj)return;
  177. var x = e.pageX ;
  178. self.xcache_tmp = x - self.opts.dx; // ドラッグによる移動量
  179. if ( self.opts.round == true ) { self.roundPrc(); }; // 360°パノラマの場合
  180. if(self.maxLeft < self.xcache_tmp && 0 > self.xcache_tmp){ self.movePrc(); }; // 画像が可動の範囲であれば実行
  181. });
  182. };
  183.  
  184. $.extend($.Panorama.prototype,{
  185.  
  186. roundPrc: function() { // 360°パノラマの場合の処理
  187. if ( this.xcache_tmp >= 0 ) {this.xcache_tmp -= this.imgWidth;}
  188. if ( this.xcache_tmp <= this.maxLeft ) {this.xcache_tmp += this.imgWidth;}
  189. },
  190.  
  191. movePrc: function() { // 移動処理
  192. this.$id.find('div').css("left", this.xcache_tmp + "px");
  193. this.opts.xcache = this.xcache_tmp; // 画像の左端のスクリーンの左端に対する座標
  194. },
  195.  
  196. automovePrc: function(moveStep) { // 自動移動
  197. if (this.timer) return;
  198. var self = this;
  199. this.timer = setInterval(function(){
  200. self.xcache_tmp = self.opts.xcache - moveStep;
  201. if ( self.opts.round == true ) { self.roundPrc(); }; // 360°パノラマの場合
  202. if(self.maxLeft < self.xcache_tmp && self.xcache_tmp < 0){ // 画像が可動の範囲であれば実行
  203. self.movePrc();
  204. } else {
  205. clearInterval(self.timer);
  206. self.timer = "";
  207. }
  208. }, 1000/self.opts.speed )
  209. }
  210. });
  211.  
  212. })(jQuery);

◆その他

パノラマ写真を作るにはImage Composite Editorが便利です。

参考にしたページ

Javascriptでパノラマ写真をドラッグ