// JavaScript Document/* ============================================= */ /* Smart Phone JavaScript Library */ /* last update: 2012/2/3 */ /*============================================= */ var sp_lib = { ver: "1.3.3", /* ------------------------------------------------------------ 端末判定 ------------------------------------------------------------ */ ua: navigator.userAgent, is: { Android: navigator.userAgent.indexOf("Android") != -1, iPhone: navigator.userAgent.indexOf("iPhone") != -1, iPad: navigator.userAgent.indexOf("iPad") != -1, iOS: navigator.userAgent.indexOf("iPhone") != -1 || navigator.userAgent.indexOf("iPad") != -1, noJQAnimation: (function() { var exceptionUA = ["IS11PT", "N-01D"]; for (var i in exceptionUA) { if (navigator.userAgent.indexOf(exceptionUA[i]) != -1) { return true; } } return false; })() }, /* ------------------------------------------------------------ ライブラリー読み込みに実行、初期設定など ------------------------------------------------------------ */ init: new function() { $(function() { if (!location.hash && (sp_lib.is.iOS || sp_lib.is.Android)) { sp_lib.hideURL(); // ステータスバーを隠す } }); }, /* ------------------------------------------------------------ window.onload時に実行 ------------------------------------------------------------ */ onload: function(handler) { $(window).bind("load", handler); }, /* ------------------------------------------------------------ ページ内スクロール ------------------------------------------------------------ */ anchorLinkScroll: function() { $("a[href^='#'], area[href^='#']").attr("onclick", "sp_lib.anchorLinkScrollControl(this); return false;"); }, anchorLinkScrollControl: function(obj) { var pos = $($(obj).attr("href")).position(); if (pos) { $('html,body').animate({ scrollTop: pos.top }, { duration: 200 }); } }, /* ------------------------------------------------------------ 左右のスワイプイベント ------------------------------------------------------------ */ /* サンプル sp_lib.swipe("#id", sw_callback); function sw_callback(event) { eventにはスワイプ方向のleftかrightの文字列が返ってきます。 } */ swipe: function(id, callback, handler) { var sw_scrollStop = 10; // 水平移動でスクロールを止める距離 var sw_distanceX = 30; // スワイプイベントを発生させる距離 var sw_distanceY = 10; var $this = $(id); $this.bind("touchstart", function(event) { var data = event.originalEvent.touches[0]; var start = { x: data.pageX, y: data.pageY } if (handler) { if (handler.start) { handler.start(start); } } var stop; function moveHandler(event) { var data = event.originalEvent.touches[0]; stop = { x: data.pageX, y: data.pageY } if (Math.abs(start.x - stop.x) > sw_scrollStop) { event.preventDefault(); } if (handler) { if (handler.move) { handler.move(stop); } } } $this.bind("touchmove", moveHandler).one("touchend", function() { $this.unbind("touchmove", moveHandler); var swipeDirection; $("#debug5").html(Math.abs(start.x - stop.x)); if (Math.abs(start.x - stop.x) > sw_distanceX) { if (start.x < stop.x) swipeDirection = "swiperight"; else swipeDirection = "swipeleft"; callback(swipeDirection); } }); }); }, /* ------------------------------------------------------------ スワイプスライドショー ------------------------------------------------------------ */ // ※optionの省略されたときのバグを取る swipeSlide: function(selector, option) { /* optionについて loop: ループの有無 true or false auto: 自動スワイプ flick: フリックを有効にするか attach: 指に追従するか duration: スライドの待機時間 ms changeDuration: スライドのディゾルブ時間 ms controller: ネクストバックのコントローラーの有無(true or false) callback: コールバック関数 */ var $this = $(selector); var obj = { count: $(".swipeSlideInner ul li", $this).size(), current: 0, timerID: 0, flagSwipe: false, first: true, // スライドの最初の時はtrue last: false // スライドの最後の時はtrue }; if (option.loop) { var addSlide = "
  • "; addSlide += $(".swipeSlideInner ul li:eq(0)", $this).html(); addSlide += "
  • "; $(".swipeSlideInner ul", $this).append(addSlide); $(".swipeSlideInner", $this).width((obj.count + 1) * 100 + "%"); $(".swipeSlideInner ul li", $this).width(100 / (obj.count + 1) + "%"); } else { $(".swipeSlideInner", $this).width(obj.count * 100 + "%"); $(".swipeSlideInner ul li", $this).width(100 / obj.count + "%"); } this.onload(function() { if (option.auto) launchSwipeAutoSlide(); if (option.flick && !option.attach) { sp_lib.swipe(selector, function(direction) { if (obj.timerID) { clearInterval(obj.timerID); obj.timerID = 0; } swipeSlideCtrl(direction); }); } else if (option.flick && option.attach) { var startLeft, startX, moveX; sp_lib.swipe( selector, function(direction) { if (obj.timerID) { clearInterval(obj.timerID); obj.timerID = 0; } swipeSlideCtrl(direction); }, { start: function(val) { startX = val.x; startLeft = parseInt($(".swipeSlideInner", $this).css("left")); }, move: function(val) { moveX = val.x; $(".swipeSlideInner", $this).css("left", moveX - startX + startLeft); } } ); } for(var i = 0; i < obj.count; i++){ $("#marker").append("
  • "); } $("#marker li").eq(0).addClass("current"); $("#marker li").click(function(){ clearInterval(obj.timerID); obj.timerID = 0; swipeSlideCtrl($(this).index()); return false; }); if (option.controller) { $(".btnPrev", $this).click(function() { clearInterval(obj.timerID); obj.timerID = 0; swipeSlideCtrl("swiperight"); }); $(".btnNext", $this).click(function() { clearInterval(obj.timerID); obj.timerID = 0; swipeSlideCtrl("swipeleft"); }); } }); function launchSwipeAutoSlide() { obj.timerID = setInterval(function() { swipeSlideCtrl("swipeleft"); }, option.duration); } function swipeSlideCtrl(direction) { if (!obj.flagSwipe) { obj.flagSwipe = true; if (direction == "swipeleft") { if (obj.current < obj.count - 1 || option.loop) { obj.current++; obj.x = obj.current * -100 +"%"; } } if (direction == "swiperight") { if (obj.current == 0 && option.loop) { obj.current = obj.count; obj.x = obj.current * -100 +"%"; $(".swipeSlideInner", $this).css("left", obj.x); } if (obj.current > 0) { obj.current--; obj.x = obj.current * -100 +"%"; } } //if文でdirectionが数値のとき(typeof)次の処理 if (typeof direction == "number"){ obj.current = direction; obj.x = obj.current * -100 +"%"; } $(".swipeSlideInner", $this).animate({ left: obj.x }, { duration: option.changeDuration, complete: function() { if (obj.current == obj.count) { obj.current = 0; $(".swipeSlideInner", $this).css("left", 0); } $("#marker li").removeClass("current") $("#marker li").eq(obj.current).addClass("current") obj.first = false; obj.last = false; if (obj.current == 0) { obj.first = true; } if (obj.current == obj.count - 1) { obj.last = true; } obj.flagSwipe = false; if (!obj.timerID && option.auto) launchSwipeAutoSlide(); option.callback(obj); } }); } } } // ライブラリーオブジェクト終了 };