$(function(){ //colorbox $(".gallery").colorbox({ rel: 'slideshow', slideshow: false, slideshowSpeed: 3000, maxWidth: "85%", maxHeight: "85%", opacity: 0.8 }); //colorboxスワイプ切り替え $('#colorbox').on('touchstart', onTouchStart); //指が触れたか検知 $('#colorbox').on('touchmove', onTouchMove); //指が動いたか検知 $('#colorbox').on('touchend', onTouchEnd); //指が離れたか検知 var direction, position; //スワイプ開始時の横方向の座標を格納 function onTouchStart(event) { position = getPosition(event); direction = ''; //一度リセットする } //スワイプの方向(left/right)を取得 function onTouchMove(event) { if (position - getPosition(event) > 70) { // 70px以上移動しなければスワイプと判断しない direction = 'left'; //左と検知 } else if (position - getPosition(event) < -70){ // 70px以上移動しなければスワイプと判断しない direction = 'right'; //右と検知 } } function onTouchEnd(event) { if (direction == 'right'){ $.colorbox.next(); } else if (direction == 'left'){ $.colorbox.prev(); } } //横方向の座標を取得 function getPosition(event) { return event.originalEvent.touches[0].pageX; } //リサイズ時の処理 $(window).resize(function() { var timer = false; if (timer !== false) { clearTimeout(timer); } timer = setTimeout(function() { $.colorbox.close(); }, 100); }); });