fullpager.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. (function(factory) {
  2. "use strict";
  3. if(typeof exports === "object" && typeof module === "object")
  4. {
  5. module.exports = factory(PlaneUI);
  6. }
  7. else if(typeof define === "function" && (define.amd || define.cmd))
  8. {
  9. define(["PlaneUI"], factory);
  10. }
  11. else
  12. {
  13. window.PUI = window.PlaneUI = window.$ = factory(PlaneUI);
  14. }
  15. })(function(PlaneUI) {
  16. // Supported IE8+
  17. (function($) {
  18. var $index = 0;
  19. var settings, pageGroup, pageNav, pageNavItems, pages;
  20. var activedClass;
  21. var mousewheelEvent = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel";
  22. function inlinePagesHanler() {
  23. var lineIndex = 0;
  24. var $inlinePages = $(this);
  25. var inlineWrapper = $inlinePages.find(".pui-full-page-inline-list");
  26. var inlineList = inlineWrapper.find(".pui-full-page-inline");
  27. var inlineTotal = inlineList.length;
  28. var inlineNav = $inlinePages.find(".pui-full-page-inline-nav");
  29. var inlineNavItems = inlineNav.find('.pui-full-page-nav-item');
  30. var prevCustomBtn = $inlinePages.find("[pui-full-page-inline-prev]");
  31. var nextCustomBtn = $inlinePages.find("[pui-full-page-inline-next]");
  32. var prevBtn = $inlinePages.find(".pui-full-page-inline-prev-btn");
  33. var nextBtn = $inlinePages.find(".pui-full-page-inline-next-btn");
  34. this.count = function() {
  35. return inlineTotal;
  36. };
  37. var inlineWrapperSlide = function() {
  38. inlineNavItems.eq(lineIndex).addClass(activedClass).siblings().removeClass(activedClass);
  39. inlineWrapper.stop().animate({ marginLeft : -lineIndex * $(window).width() + "px"});
  40. };
  41. var inlinePageResize = function() {
  42. var windowWidth = $(window).width();
  43. var windowHeight = $(window).height();
  44. inlineList.width(windowWidth);
  45. inlineWrapper.width((windowWidth * inlineTotal) + 50);
  46. inlineWrapper.stop().css("margin-left", -lineIndex * $(window).width() + "px");
  47. prevBtn.css("top", (windowHeight - prevBtn.height()) / 2 + "px");
  48. nextBtn.css("top", (windowHeight - nextBtn.height()) / 2 + "px");
  49. };
  50. inlineNavItems.bind($.clickOrTouch(), function() {
  51. lineIndex = $(this).index();
  52. inlineWrapperSlide();
  53. });
  54. var prevInlinePage = function() {
  55. lineIndex = (lineIndex > 0) ? lineIndex - 1 : inlineTotal - 1;
  56. $.proxy(settings.inlinePrev, inlineWrapper)(lineIndex);
  57. inlineWrapperSlide();
  58. };
  59. var nextInlinePage = function() {
  60. lineIndex = (lineIndex < inlineTotal - 1) ? lineIndex + 1 : 0;
  61. $.proxy(settings.inlineNext, inlineWrapper)(lineIndex);
  62. inlineWrapperSlide();
  63. };
  64. prevBtn.bind($.clickOrTouch(), prevInlinePage);
  65. nextBtn.bind($.clickOrTouch(), nextInlinePage);
  66. prevCustomBtn.bind($.clickOrTouch(), prevInlinePage);
  67. nextCustomBtn.bind($.clickOrTouch(), nextInlinePage);
  68. inlinePageResize();
  69. $(window).resize(inlinePageResize);
  70. console.log("in-viewport =>", inlineWrapper.is(":in-viewport"));
  71. $(window).keyup(function(event) {
  72. if (!inlineWrapper.is(":in-viewport") || !settings.keyboard) {
  73. return ;
  74. }
  75. if (lineIndex < inlineTotal - 1 && event.keyCode == 39) { // right
  76. lineIndex += 1;
  77. $.proxy(settings.inlinePrev, inlineWrapper)(lineIndex);
  78. }
  79. if (lineIndex > 0 && event.keyCode == 37) { // left
  80. lineIndex -= 1;
  81. $.proxy(settings.inlineNext, inlineWrapper)(lineIndex);
  82. }
  83. inlineWrapperSlide();
  84. });
  85. if (document.addEventListener && settings.touchable) {
  86. var startX = 0;
  87. var inlineIndexChanged = false;
  88. var $inlineWrapper = inlineWrapper[0];
  89. var inlineWrapperTouchStart = function(event){
  90. var touch = event.touches[0];
  91. var x = Number(touch.pageX);
  92. startX = x;
  93. $inlineWrapper.removeEventListener("touchstart", inlineWrapperTouchStart, false);
  94. $inlineWrapper.addEventListener("touchmove", inlineWrapperTouchMove, false);
  95. $inlineWrapper.addEventListener("touchend", inlineWrapperTouchEnd, false);
  96. };
  97. var inlineWrapperTouchMove = function(event) {
  98. var touch = event.touches[0];
  99. var x = Number(touch.pageX);
  100. var total = inlineList.length;
  101. if (x - startX != 0)
  102. {
  103. if (x - startX < -60)
  104. {
  105. if (lineIndex < total - 1 && !inlineIndexChanged) {
  106. inlineIndexChanged = true;
  107. lineIndex +=1;
  108. $.proxy(settings.inlinePrev, $inlineWrapper)(lineIndex);
  109. }
  110. }
  111. else if (x - startX > 60 && !inlineIndexChanged)
  112. {
  113. if (lineIndex > 0) {
  114. inlineIndexChanged = true;
  115. lineIndex -=1;
  116. $.proxy(settings.inlineNext, $inlineWrapper)(lineIndex);
  117. }
  118. }
  119. inlineWrapperSlide();
  120. }
  121. };
  122. var inlineWrapperTouchEnd = function(event) {
  123. startX = 0;
  124. inlineIndexChanged = false;
  125. $inlineWrapper.removeEventListener("touchmove", inlineWrapperTouchMove, false);
  126. $inlineWrapper.removeEventListener("touchend", inlineWrapperTouchMove, false);
  127. $inlineWrapper.addEventListener("touchstart", inlineWrapperTouchStart, false);
  128. };
  129. $inlineWrapper.addEventListener("touchstart", inlineWrapperTouchStart, false);
  130. }
  131. return this;
  132. }
  133. function keyUpHandler() {
  134. if (!settings.keyboard) {
  135. return ;
  136. }
  137. var pages = $(this);
  138. var keyCode = event.keyCode;
  139. var total = pages.length;
  140. if ($index < total - 1 && keyCode == 40) { // down
  141. $index += 1;
  142. $.proxy(settings.prev, pageGroup)($index);
  143. }
  144. else if ($index > 0 && keyCode == 38) { //up
  145. $index -= 1;
  146. $.proxy(settings.next, pageGroup)($index);
  147. }
  148. pageGroupSlide();
  149. }
  150. function pageGroupSlide() {
  151. pageGroup.stop().animate({
  152. scrollTop : $(window).height() * $index
  153. }, 800);
  154. pageNavActived();
  155. navbarActived();
  156. }
  157. function pageNavActived() {
  158. pageNavItems.eq($index).addClass(activedClass).siblings().removeClass(activedClass);
  159. }
  160. function navbarActived() {
  161. if (settings.navbar || settings.navbar !== "")
  162. {
  163. if (typeof settings.navbar === "object")
  164. {
  165. $(settings.navbar[0]).removeClass(settings.activedClass);
  166. $(settings.navbar[0]).eq($index).addClass(settings.activedClass);
  167. }
  168. }
  169. }
  170. function mouseWheelHandler(event) {
  171. if (!settings.mouseWheel) {
  172. return ;
  173. }
  174. var delta = 0;
  175. var event = window.event || event;
  176. var delta = (event.detail) ? -event.detail / 3 : event.wheelDelta / 120;
  177. if (event.preventDefault) {
  178. event.preventDefault();
  179. } else {
  180. event.returnValue = false;
  181. }
  182. //console.log(delta, (delta > 0) ? "up" : "down");
  183. var total = pages.length;
  184. if ($index < total - 1 && delta < 0) { // down
  185. $index += 1;
  186. $.proxy(settings.prev, pageGroup)($index);
  187. }
  188. if ($index > 0 && delta > 0) { //up
  189. $index -= 1;
  190. $.proxy(settings.next, pageGroup)($index);
  191. }
  192. pageGroupSlide();
  193. return false;
  194. }
  195. function touchSlide() {
  196. var startY = 0, indexChanged = false;
  197. var $group = pageGroup[0];
  198. var touchStartHandler = function(event) {
  199. var touch = event.touches[0];
  200. var y = Number(touch.pageY);
  201. startY = y;
  202. $group.removeEventListener("touchstart", touchStartHandler, false);
  203. $group.addEventListener("touchmove", touchMoveHandler, false);
  204. $group.addEventListener("touchend", touchEndHandler, false);
  205. };
  206. var touchMoveHandler = function(event) {
  207. var touch = event.touches[0];
  208. var y = Number(touch.pageY);
  209. var total = pages.length;
  210. if (y - startY !== 0)
  211. {
  212. if (y - startY < -60)
  213. {
  214. if ($index < total - 1 && !indexChanged) {
  215. indexChanged = true;
  216. $index += 1;
  217. $.proxy(settings.prev, $group)($index);
  218. }
  219. }
  220. else if (y - startY > 60 && !indexChanged)
  221. {
  222. if($index > 0) {
  223. indexChanged = true;
  224. $index -= 1;
  225. $.proxy(settings.next, $group)($index);
  226. }
  227. }
  228. pageGroupSlide();
  229. }
  230. };
  231. var touchEndHandler = function(event) {
  232. startY = 0;
  233. indexChanged = false;
  234. $group.removeEventListener("touchmove", touchMoveHandler, false);
  235. $group.removeEventListener("touchend", touchMoveHandler, false);
  236. $group.addEventListener("touchstart", touchStartHandler, false);
  237. };
  238. //var fullPageGroup = $(".pui-full-page-group")[0];
  239. $group.addEventListener("touchstart", touchStartHandler, false);
  240. }
  241. function prevPage() {
  242. $index = ($index > 0) ? $index - 1 : pages.length - 1;
  243. $.proxy(settings.prev, pageGroup)($index);
  244. pageGroupSlide();
  245. }
  246. function nextPage() {
  247. $index = ($index < pages.length - 1) ? $index + 1 : 0;
  248. $.proxy(settings.next, pageGroup)($index);
  249. pageGroupSlide();
  250. }
  251. function fullPager(options) {
  252. options = options || {};
  253. var defaults = {
  254. nav : true,
  255. inlineNav : true,
  256. touchable : true,
  257. keyboard : true,
  258. mouseWheel : true,
  259. navbar : null,
  260. next : function() {},
  261. prev : function() {},
  262. inlineNext : function() {},
  263. inlinePrev : function() {},
  264. activedClass : "active"
  265. };
  266. settings = $.extend(defaults, options);
  267. var selector = (this.selector) ? this.selector : ".full-page-group";
  268. pageGroup = $(selector).eq(0);
  269. pageNav = pageGroup.find(".pui-full-page-nav");
  270. pages = pageGroup.find(".pui-full-page");
  271. pageNavItems = pageNav.find('.pui-full-page-nav-item');
  272. activedClass = settings.activedClass;
  273. var inlinePages = pageGroup.find(".pui-full-inline-pages");
  274. var prevBtn = pageGroup.find("[pui-full-page-prev]");
  275. var nextBtn = pageGroup.find("[pui-full-page-next]");
  276. var resize = function() {
  277. var windowWidth = $(window).width();
  278. var windowHeight = $(window).height();
  279. pageNav.css("top", (windowHeight - pageNav.height()) / 2 + "px");
  280. };
  281. inlinePages.each(function() {
  282. $.proxy(inlinePagesHanler, this)();
  283. });
  284. resize();
  285. $(window).resize(resize).keyup($.proxy(keyUpHandler, pages));
  286. pageGroup.bind(mousewheelEvent, function() {
  287. mouseWheelHandler();
  288. });
  289. if (settings.navbar || settings.navbar !== "")
  290. {
  291. if (typeof settings.navbar === "object")
  292. {
  293. $(settings.navbar[0]).bind($.clickOrTouch(), function(){
  294. $index = $(this).index();
  295. pageGroupSlide();
  296. $.proxy(settings.navbar[1], this)($index);
  297. });
  298. }
  299. }
  300. if (settings.touchable) {
  301. $("body").bind("touchmove", function(event) {
  302. event.preventDefault();
  303. });
  304. if (document.addEventListener) {
  305. touchSlide();
  306. }
  307. }
  308. prevBtn.bind($.clickOrTouch(), prevPage);
  309. nextBtn.bind($.clickOrTouch(), nextPage);
  310. pageNavItems.bind($.clickOrTouch(), function() {
  311. $index = $(this).index();
  312. pageGroupSlide();
  313. });
  314. this.prev = prevPage;
  315. this.next = nextPage;
  316. this.settings = function() {
  317. return settings;
  318. };
  319. this.index = function() {
  320. return $index;
  321. };
  322. this.pages = function() {
  323. return pages;
  324. };
  325. this.count = function() {
  326. return pages.length;
  327. };
  328. this.inlinePages = function() {
  329. return inlinePages;
  330. };
  331. return this;
  332. };
  333. $.fn.fullPager = fullPager;
  334. })(PlaneUI);
  335. return PlaneUI;
  336. });