var captions = new Array(); var pic_dir = ""; var slide = new Array(); var index = 0; // current slide index function loadImage(url) { if (document.images) { rslt = new Image(); rslt.src = url; return rslt; } } if ( document.images ) { loadImage(pic_dir + "forward_s.png"); loadImage(pic_dir + "backward_s.png"); for(var i in pic) { slide.push(loadImage(pic_dir + pic[i])); } } function prevSlide() { if(--index < 0) { index = pic.length-1; } changeSlide(); } function nextSlide() { if( ++index >= pic.length) { index = 0 } changeSlide(); } function changeSlide() { document.getElementById('display').src = slide[index].src; if (captions[pic[index]]== undefined) { document.getElementById('caption').innerHTML = ""; } else { document.getElementById('caption').innerHTML = captions[pic[index]]; } } function xPosition(nd) { var x=0; while ( nd && nd.offsetParent ) { x += nd.offsetLeft; nd = nd.offsetParent; } return x; } function whichSide(e) { var img=document.getElementById('display'); var x=xPosition(img), half=img.clientWidth/2; if ( e.clientX-x > half ) nextSlide() else prevSlide(); } ///// for touch screen ///// function initSwipe() { var image=document.getElementById('display'); image.addEventListener("touchstart", startPosition, false); image.addEventListener("touchmove", moveHandler, false); image.addEventListener("touchend", endHandler, false); } var tx, dir; function startPosition(e) // touchstart position { var touch = e.touches[0]; tx=touch.pageX; } function moveHandler(e) // touchmove { e.preventDefault(); var touch = e.touches[0]; dir=tx-touch.pageX; } function endHandler(e) // touchleave { if ( dir > 35 ) nextSlide(); if ( dir < -35 ) prevSlide(); dir=tx=0; }