Guest User

Untitled

a guest
Jan 22nd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.42 KB | None | 0 0
  1. (function() {
  2. var swipey = {
  3. slideContainer: null, //<ul> element object that holds the image slides
  4. wrapper: null, //meant for masking/clipping
  5. slides: null, //array of all slides i.e <li> elements
  6. distanceX: 0, //distance moved in X direction i.e left or right
  7. startX: 0, //registers the initial touch co-ordinate
  8. preferredWidth: 0, //dynamic variable to set width
  9. preferredHeight: 0, //dynamic variable to set height
  10. direction: "", //direction of movement
  11. timer: null, //timer that set starts when touch starts
  12. timerCounter: 0, //counter variable for timer
  13. hasSwipeStarted: false, //boolen to chk whether touch has started
  14. maxDistance: 0, //maximum distance in X direction that slide container can move
  15. currentDistance: 0, //current distance moved by slide container through translate
  16.  
  17. //detect touch and then automatically assign events
  18. isTouchSupported: 'ontouchstart' in window.document,
  19.  
  20. initSwipey: function() {
  21. // alert('in initSwipey');
  22. //scroll the window up to hide the address bar of the browser.
  23. window.setTimeout(function() {
  24. window.scrollTo(0, 1);
  25. }, 100);
  26.  
  27. /*mapping touch events to mouse events. Automatic registration of event
  28. based on the device. If touch enabled then touch event is registered.
  29. and if desktop browser then mouse event is registered.*/
  30. swipey.startEvent = swipey.isTouchSupported ? 'touchstart' : 'mousedown',
  31. swipey.moveEvent = swipey.isTouchSupported ? 'touchmove' : 'mousemove',
  32. swipey.endEvent = swipey.isTouchSupported ? 'touchend' : 'mouseup',
  33.  
  34. //get all the instances of the HTML elements
  35. swipey.wrapper = document.getElementById("wrapper");
  36. swipey.slideContainer = document.getElementById("sitePageContainer");
  37. //swipey.slides = slideContainer.getElementsByTagName("li");
  38. swipey.slides = document.getElementsByClassName("sitePage");
  39.  
  40. //for iPhone, the width and height
  41. swipey.preferredWidth = window.innerWidth;
  42. // alert("swipey.preferredWidth: " + swipey.preferredWidth);
  43. swipey.preferredHeight = window.innerHeight; //510 for android
  44. // alert("swipey.preferredHeight: " + swipey.preferredHeight);
  45. //setting the width and height to our wrapper with overflow = hidden
  46. swipey.wrapper.style.width = swipey.preferredWidth + "px";
  47. // alert("swipey.wrapper.style.width: " + swipey.wrapper.style.width);
  48. swipey.wrapper.style.height = swipey.preferredHeight + "px";
  49. // alert("swipey.wrapper.style.height: " + swipey.wrapper.style.height);
  50. //setting the width to our <ul> element which holds all the <li> elements
  51. swipey.slideContainer.style.height = swipey.preferredHeight + "px";
  52. // alert("swipey.slideContainer.style.height: " + swipey.slideContainer.style.height);
  53. swipey.slideContainer.style.width = swipey.slides.length * swipey.preferredWidth + "px";
  54. // alert("swipey.slideContainer.style.width: " + swipey.slideContainer.style.width);
  55. //display the <ul> container now
  56. swipey.slideContainer.style.display = "inline-block";
  57. //setting width and height for <li> elements - the slides
  58. for(var i=0;i<swipey.slides.length;i++)
  59. {
  60. swipey.slides[i].style.width = swipey.preferredWidth + "px";
  61. swipey.slides[i].style.height = swipey.preferredHeight + "px";
  62. }
  63. //calculating the max distance of travel for Slide Container i.e <ul> element
  64. swipey.maxDistance = swipey.slides.length * swipey.preferredHeight;
  65. //initialize and assign the touch events
  66. swipey.initEvents();
  67. },
  68. initEvents: function() {
  69. //registering touch events to the wrapper
  70.  
  71.  
  72.  
  73. //$('#wrapper').bind(swipey.startEvent, swipey.startHandler );
  74. $('#wrapper').bind(swipey.startEvent, function(e){
  75. $('#wrapper').bind(swipey.moveEvent, swipey.moveHandler );
  76. } );
  77.  
  78. $('#wrapper').bind(swipey.endEvent, swipey.endHandler );
  79. //swipey.wrapper.addEventListener(swipey.startEvent, swipey.startHandler, false);
  80. //swipey.wrapper.addEventListener(swipey.moveEvent, swipey.moveHandler, false);
  81. //swipey.wrapper.addEventListener(swipey.endEvent, swipey.endHandler, false);
  82. },
  83. //funciton called when touch start event is fired i.e finger is pressed on the screen
  84. startHandler: function(event) {
  85. //create appropriate event object to read the touch/mouse co-ordinates
  86. var eventObj = swipey.isTouchSupported ? event.touches[0] : event;
  87.  
  88. //stores the starting X co-ordinate when finger touches the device screen
  89. swipey.startX = eventObj.pageX;
  90. //timer is set on
  91. swipey.timer = setInterval(function() {
  92. swipey.timerCounter++;
  93. }, 10);
  94. swipey.hasSwipeStarted = true;
  95. event.preventDefault(); //prevents the window from scrolling.
  96. },
  97. //funciton called when touch move event is fired i.e finger is dragged over the screen
  98. moveHandler: function(event) {
  99. if (swipey.hasSwipeStarted) {
  100. //create appropriate event object to read the touch/mouse co-ordinates
  101. var eventObj = swipey.isTouchSupported ? event.touches[0] : event;
  102.  
  103. swipey.distanceX = eventObj.pageX - swipey.startX;
  104. //move the slide container along with the movement of the finger
  105. swipey.slideContainer.style.webkitTransform = "translate3d(" + (swipey.distanceX + swipey.currentDistance) + "px, 0,0)";
  106. }else{
  107. swipey.startHandler(event);
  108. }
  109. },
  110. //funciton called when touch end event is fired i.e finger is released from screen
  111. endHandler: function(event) {
  112. $('#wrapper').unbind(swipey.moveEvent, swipey.moveHandler );
  113. clearInterval(swipey.timer); //timer is stopped
  114. if(swipey.distanceX == 0) //if the intention is to tap on the image then open a link
  115. {
  116. var link_url = event.target.getAttribute("link"); //read the link from <img /> element
  117. // window.open(link_url,"_blank");
  118. }
  119. else
  120. {
  121. if (swipey.distanceX > 0) {
  122. swipey.direction = "right";
  123. }
  124. if (swipey.distanceX < 0) {
  125. swipey.direction = "left";
  126. }
  127. //the following conditions have been discussed in details
  128. if ((swipey.direction == "right" && swipey.currentDistance == 0) || (swipey.direction == "left" && swipey.currentDistance == -(swipey.maxDistance - swipey.preferredWidth))) {
  129. swipey.comeBack();
  130. }
  131. else if (swipey.timerCounter < 30 && swipey.distanceX > 10) {
  132. swipey.moveRight();
  133. }
  134. else if (swipey.timerCounter < 30 && swipey.distanceX < -10) {
  135. swipey.moveLeft();
  136. }
  137. else if (swipey.distanceX <= -(swipey.preferredWidth / 2)) { //-160
  138. swipey.moveLeft();
  139. }
  140. else if (swipey.distanceX >= (swipey.preferredWidth / 2)) { //160
  141. swipey.moveRight();
  142. }
  143. else {
  144. swipey.comeBack();
  145. }
  146. }
  147. swipey.timerCounter = 0; //reset timerCounter
  148. swipey.hasSwipeStarted = false; //reset the boolean var
  149. swipey.distanceX = 0; //reset the distance moved for next iteration
  150.  
  151. //alert(event.screenX+"::"+event.screenY);
  152. var element = document.elementFromPoint(event.screenX, event.screenY);
  153. var $element = $(element);
  154.  
  155. alert($element.attr('id'));
  156. $("div.fixHeader").hide();
  157. $("#"+$element.attr('id')+" div.fixHeader").show();
  158.  
  159. },
  160. moveLeft: function() {
  161. swipey.currentDistance += -swipey.preferredWidth;
  162. swipey.slideContainer.style.webkitTransitionDuration = 300 + "ms";
  163. //using CSS3 transformations - translate3d function for movement
  164. swipey.slideContainer.style.webkitTransform = "translate3d(" + swipey.currentDistance + "px, 0,0)";
  165. },
  166. moveRight: function() {
  167. swipey.currentDistance += swipey.preferredWidth;
  168. swipey.slideContainer.style.webkitTransitionDuration = 300 + "ms";
  169. swipey.slideContainer.style.webkitTransform = "translate3d(" + swipey.currentDistance + "px, 0,0)";
  170. },
  171. comeBack: function() {
  172. swipey.slideContainer.style.webkitTransitionDuration = 250 + "ms";
  173. swipey.slideContainer.style.webkitTransitionTimingFunction = "ease-out";
  174. swipey.slideContainer.style.webkitTransform = "translate3d(" + swipey.currentDistance + "px, 0,0)";
  175. }
  176. }; //end of swipey object
  177. window.swipeyObj = swipey; //expose to global window object
  178. })();
  179.  
  180. window.onload = function() {
  181. swipeyObj.initSwipey();
  182. } //invoke the init method to get started
  183.  
  184. <div id="wrapper">
  185. <div id="sitePageContainer">
  186. <div class="sitePage" id="partypicturespage1"></div>
  187. <div class="sitePage" id="partypicturespage2"></div>
  188.  
  189. $("div.fixHeader:visible")
  190.  
  191. $("div.fixHeader").attr('data-visible', true);
  192.  
  193. $("div.fixHeader[data-visible=true]");
  194.  
  195. $("div.fixHeader[data-visible=true]").attr('data-visible', false);
  196.  
  197. ceil(scrollContainer.left / divWidth) // if index starts from 1
  198. floor(scrollContainer.left / divWidth) // if index starts from 0
Add Comment
Please, Sign In to add comment