2013
6
25
<div id="sliderContainer"> <div id="flame"> <ul class="clearfix" id="slider"> <li><img alt="" src="1枚目画像パス" width="300" height="381" /></li> <li><img alt="" src="2枚目画像パス" width="300" height="381" /></li> <li><img alt="" src="3枚目画像パス" width="300" height="381" /></li> </ul> <ul id="slideIcon"> <li id="prev">«</li> <li id="next">»</li> </ul> </div> <div id="btnPagination"> <ul class="panelNavi clearfix"> <li><a href="#"><img alt="" src="1枚目画像パス" width="96" height="122" /></a></li> <li><a href="#"><img alt="" src="2枚目画像パス" width="96" height="122" /></a></li> <li><a href="#"><img alt="" src="3枚目画像パス" width="96" height="122" /></a></li> </ul> </div> </div>
#flame { width:300px; height:381px; position:relative; overflow:hidden; margin: 10px auto; } ul#slider { margin-left: 0; width: 900px; height: 381px; overflow: hidden; position: relative; } ul#slider li { float: left; width: 300px; position: absolute; } ul#slider li img{ width: 100%; } ul#slideIcon{ width:100%; height:60px; position:relative; } ul#slideIcon li{ display:inline-block; width:60px; height:70px; position:absolute; text-indent:-9999px; float:left; } ul#slideIcon li#prev { left: 0; top: -240px; background:url(左アイコン画像パス) no-repeat 20% center; } ul#slideIcon li#next { right: 0; top: -240px; background:url(右アイコン画像パス) no-repeat 80% center; } ul.panelNavi { margin: 0 auto; width: 318px; } .panelNavi li { float: left; padding: 3px; text-align: center; width: 96px; } .panelNavi li a{ display:block; margin:5px; } .clearfix:after { clear: both; content: "."; display: block; height: 0; visibility: hidden; }
$(function () { //①レイアウト系フィールド設定 var flickBox = $('#flame'); var flickList = $('ul#slider'); var flickListImg = flickList.children('li'); var imgListCount = flickListImg.length; var thumbnail = $('ul.panelNavi li'); var prev = $('#prev'); var next = $('#next'); //②パラメーター系フィールド設定 var slideSpeed = 500; var imgListWidth = 300; var imgListHeight = 381; var imgNum = 0; var slideCurrent = 0; var imgSize = flickListImg.width(); //③フリック対象画像設置設定 flickListImg.each(function(){ $(this).css("margin-left", -imgSize); if(imgNum == slideCurrent){ $(this).css("margin-left", "0"); } imgNum++; }); //④次項・前項クリック処理 next.click(function(e){ //e.preventDefault(); slideImg(slideCurrent + 1); }); prev.click(function(e){ //e.preventDefault(); slideImg(slideCurrent - 1); }); //⑤サムネイルクリック処理 thumbnail.click(function(e){ //e.preventDefault(); var thisNum = thumbnail.index(this); if(thisNum != slideCurrent){ slideImg(thisNum); } }); //⑥スライド移動ファンクション function slideImg(next){ var pos; if(slideCurrent < next){ pos = imgSize; }else{ pos = -imgSize; } if(next == imgNum){ next = 0; }else if(next == -1){ next = (imgNum - 1); } //⑦スライドエフェクト設定 flickListImg.eq(next).css("margin-left", pos).animate({marginLeft: "0"}, "fast"); flickListImg.eq(slideCurrent).animate({marginLeft: -pos}, "fast"); slideCurrent = next; } });
var flickFlag = true; $(flickListImg).bind({ 'touchstart': function(e) { this.touchX = event.changedTouches[0].pageX; this.slideX = flickList.position().left; }, 'touchmove': function(e) { this.slideX = this.slideX - (this.touchX - event.changedTouches[0].pageX ); this.touchX = event.changedTouches[0].pageX; switch(true){ case(this.touchX > (this.touchX + this.slideX)): //e.preventDefault(); if(flickFlag == true){ slideImg(slideCurrent + 1); flickFlag = false; } break; case(this.touchX < (this.touchX + this.slideX)): //e.preventDefault(); if(flickFlag == true){ slideImg(slideCurrent - 1); flickFlag = false; } break; } }, 'touchend': function(e) { flickFlag = true; } });上記タッチイベントを実装させたデモがこちら
//⑦スライドエフェクト設定 flickListImg.eq(next).css("margin-left", pos).stop(true, true).animate({marginLeft: "0"}, "fast"); flickListImg.eq(slideCurrent).stop(true, true).animate({marginLeft: -pos}, "fast"); //タッチイベント処理 $(flickListImg).bind({ 'touchstart': function(e) { this.touchX = event.changedTouches[0].pageX; this.slideX = flickList.position().left; this.touchY = event.changedTouches[0].pageY; this.slideY = flickList.position().top; }, 'touchmove': function(e) { this.slideX = this.slideX - (this.touchX - event.changedTouches[0].pageX ); this.touchX = event.changedTouches[0].pageX; this.slideY = this.slideY - (this.touchY - event.changedTouches[0].pageY ); this.touchY = event.changedTouches[0].pageY; slideMathX = Math.abs(this.slideX); slideMathY = Math.abs(this.slideY); if(slideMathX < slideMathY){ flickListImg.addEventListener('touchmove', scrollY); }else{ switch(true){ case(this.touchX > (this.touchX + this.slideX)): e.preventDefault(); if(flickFlag == true){ slideImg(slideCurrent + 1); flickFlag = false; } break; case(this.touchX < (this.touchX + this.slideX)): e.preventDefault(); if(flickFlag == true){ slideImg(slideCurrent - 1); flickFlag = false; } break; } }; }, 'touchend': function(e) { flickFlag = true; flickListImg.removeEventListener('touchmove', scrollY); } });