Image_gallery_example.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport"
  6. content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
  7. <meta http-equiv="x-ua-compatible" content="IE=9">
  8. <link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css" rel="stylesheet">
  9. <link href="http://twitter.github.com/bootstrap/assets/js/google-code-prettify/prettify.css" rel="stylesheet"/>
  10. <link href="css/main.css" type="text/css" rel="stylesheet"/>
  11. <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  12. <script type="text/javascript"
  13. src="https://google-code-prettify.googlecode.com/svn/loader/run_prettify.js"></script>
  14. <script type="text/javascript" src="../jquery.touchSwipe.js"></script>
  15. <script type="text/javascript" src="js/main.js"></script>
  16. <title>touchSwipe</title>
  17. <style>
  18. #content {
  19. height: 340px;
  20. width: 500px;
  21. overflow: hidden;
  22. position: relative;
  23. border: 1px solid black;
  24. }
  25. #imgs {
  26. float: left;
  27. display: inline;
  28. padding: 0;
  29. margin: 0;
  30. width: 1510px;
  31. transition-property: transform;
  32. transition-duration: 0.5s;
  33. transition-timing-function: ease-out;
  34. /*apply a transform to kick in the hardware acceleration. Without this, the first
  35. time we add the transform you get odd rendering of the divs (half missing) */
  36. transform: translate(0, 0);
  37. }
  38. #imgs img {
  39. padding: 0;
  40. margin: 0;
  41. width: 500px;
  42. height: 340px;
  43. /*apply a transform to kick in the hardware acceleration. Without this, the first
  44. time we add the transform you get odd rendering of the divs (half missing) */
  45. transform: translate(0, 0);
  46. }
  47. </style>
  48. <script id='code_1'>
  49. var IMG_WIDTH = 500;
  50. var currentImg = 0;
  51. var maxImages = 3;
  52. var speed = 500;
  53. var imgs;
  54. var swipeOptions = {
  55. triggerOnTouchEnd: true,
  56. swipeStatus: swipeStatus,
  57. allowPageScroll: "vertical",
  58. threshold: 75
  59. };
  60. $(function () {
  61. imgs = $("#imgs");
  62. imgs.swipe(swipeOptions);
  63. });
  64. /**
  65. * Catch each phase of the swipe.
  66. * move : we drag the div
  67. * cancel : we animate back to where we were
  68. * end : we animate to the next image
  69. */
  70. function swipeStatus(event, phase, direction, distance) {
  71. //If we are moving before swipe, and we are going L or R in X mode, or U or D in Y mode then drag.
  72. if (phase == "move" && (direction == "left" || direction == "right")) {
  73. var duration = 0;
  74. if (direction == "left") {
  75. scrollImages((IMG_WIDTH * currentImg) + distance, duration);
  76. } else if (direction == "right") {
  77. scrollImages((IMG_WIDTH * currentImg) - distance, duration);
  78. }
  79. } else if (phase == "cancel") {
  80. scrollImages(IMG_WIDTH * currentImg, speed);
  81. } else if (phase == "end") {
  82. if (direction == "right") {
  83. previousImage();
  84. } else if (direction == "left") {
  85. nextImage();
  86. }
  87. }
  88. }
  89. function previousImage() {
  90. currentImg = Math.max(currentImg - 1, 0);
  91. scrollImages(IMG_WIDTH * currentImg, speed);
  92. }
  93. function nextImage() {
  94. currentImg = Math.min(currentImg + 1, maxImages - 1);
  95. scrollImages(IMG_WIDTH * currentImg, speed);
  96. }
  97. /**
  98. * Manually update the position of the imgs on drag
  99. */
  100. function scrollImages(distance, duration) {
  101. imgs.css("transition-duration", (duration / 1000).toFixed(1) + "s");
  102. //inverse the number we set in the css
  103. var value = (distance < 0 ? "" : "-") + Math.abs(distance).toString();
  104. imgs.css("transform", "translate(" + value + "px,0)");
  105. }
  106. </script>
  107. </head>
  108. <body>
  109. <a href="https://github.com/mattbryson"><img style="position: absolute; top: 0; right: 0; border: 0;"
  110. src="https://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png"
  111. alt="Fork me on GitHub"></a>
  112. <div class="container">
  113. <p>Below is a very simple image gallery to demonstrate how to implement touchSwipe.<br/><br/>
  114. Swipe the images below left and right. Swipe up and down will scroll the page. Uses HTML5
  115. CSS to animate.</p>
  116. <br/>
  117. <div id="content">
  118. <div id="imgs">
  119. <img src="https://lh4.googleusercontent.com/_D9-nzLCi9qU/TNQ2hYNqQEI/AAAAAAAADtI/TcqCdc6N26A/s500/twick_pool_stairs~.jpg"/>
  120. <img src="https://lh6.googleusercontent.com/_D9-nzLCi9qU/TNQ2gdP8JYI/AAAAAAAADtI/NU2WBbaXpgU/s500/twick_pool_stairsAndChanginRoom~.jpg"/>
  121. <img src="https://lh4.googleusercontent.com/_D9-nzLCi9qU/TNQ2UWpqLgI/AAAAAAAADtI/-OG4z6RxHwA/s500/twick_pool_hall~.jpg"/>
  122. </div>
  123. </div>
  124. </div>
  125. </body>
  126. </html>