wrapper.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * Created by Nature on 16/1/22.
  3. */
  4. var Wrapper = function () {
  5. var wrapper = document.getElementById("wrapper"),
  6. carousel = document.getElementById("carousel"),
  7. imgs = carousel.getElementsByTagName("li");
  8. var ul = document.createElement("ul");
  9. ul.id = "index";
  10. wrapper.appendChild(ul);
  11. for (var i = 0, ln = imgs.length; i < ln; i++) {
  12. var li = document.createElement("li");
  13. ul.appendChild(li);
  14. imgs[i].index = i;
  15. }
  16. var lis = ul.getElementsByTagName("li");
  17. var speed = 5000,
  18. timeId = null,
  19. delay = null;
  20. YEEPAY.addHandler(carousel, "mouseover", stop);
  21. YEEPAY.addHandler(carousel, "mouseout", auto);
  22. for (var i = 0, l = lis.length; i < l; i++) { //控制按钮绑定事件
  23. lis[i].index = i;
  24. YEEPAY.addHandler(lis[i], "mouseover", function (event) {
  25. stop();
  26. var target = YEEPAY.getTarget(event),
  27. _index = target.index;
  28. delay = setTimeout(function () {
  29. if (!hasClass(target, "cur")) {
  30. run(_index);
  31. }
  32. }, 200);
  33. });
  34. YEEPAY.addHandler(lis[i], "mouseout", function () {
  35. auto();
  36. clearTimeout(delay);
  37. });
  38. }
  39. function auto() { //自动轮播
  40. timeId = setTimeout(function () {
  41. run();
  42. timeId = setTimeout(arguments.callee, speed);
  43. }, speed);
  44. }
  45. function stop() { //停止自动轮播
  46. clearTimeout(timeId);
  47. }
  48. function fadeIn(elem) { //渐显
  49. var i = 0;
  50. var timeId = setTimeout(function () {
  51. i += 0.1;
  52. if (i <= 1) {
  53. elem.style.opacity = i;
  54. elem.style.filter = "alpha(opacity=" + i * 100 + ")";
  55. timeId = setTimeout(arguments.callee, 100);
  56. } else {
  57. clearTimeout(timeId);
  58. }
  59. }, 0);
  60. }
  61. function fadeOut(elem) { //渐隐
  62. var i = 1;
  63. var timeId = setTimeout(function () {
  64. i -= 0.1;
  65. if (i >= 0) {
  66. elem.style.opacity = i;
  67. elem.style.filter = "alpha(opacity=" + i * 100 + ")";
  68. timeId = setTimeout(arguments.callee, 100);
  69. } else {
  70. clearTimeout(timeId);
  71. }
  72. }, 0);
  73. }
  74. function hasClass(node, selector) { //是否有指定class
  75. var className = " " + selector + " ";
  76. if (node.nodeType === 1 && (" " + node.className + " ").replace(/[\n\t\r]g/, " ").indexOf(className) > -1) {
  77. return true;
  78. }
  79. return false;
  80. }
  81. function run(index) {
  82. if (typeof index != "undefined") {
  83. for (var j = 0, l = lis.length; j < l; j++) {
  84. YEEPAY.removeClass(lis[j], "cur");
  85. }
  86. YEEPAY.addClass(lis[index], "cur");
  87. for (var i = 0, l = imgs.length; i < l; i++) {
  88. if(hasClass(imgs[i], "front")){
  89. fadeOut(imgs[i]);
  90. imgs[i].style.zIndex = -1;
  91. YEEPAY.removeClass(imgs[i], "front");
  92. }
  93. }
  94. YEEPAY.addClass(imgs[index], "front");
  95. fadeIn(imgs[index]);
  96. imgs[index].style.zIndex = 1;
  97. } else {
  98. for (var i = 0, l = imgs.length; i < l; i++) {
  99. if (hasClass(imgs[i], "front") && imgs[i].index < imgs.length - 1) {
  100. fadeOut(imgs[i]);
  101. imgs[i].style.zIndex = -1;
  102. YEEPAY.removeClass(imgs[i], "front");
  103. YEEPAY.addClass(YEEPAY.nextNode(imgs[i]), "front");
  104. fadeIn(YEEPAY.nextNode(imgs[i]));
  105. YEEPAY.nextNode(imgs[i]).style.zIndex = 1;
  106. for (var j = 0, l = lis.length; j < l; j++) {
  107. YEEPAY.removeClass(lis[j], "cur");
  108. }
  109. YEEPAY.addClass(lis[YEEPAY.nextNode(imgs[i]).index], "cur");
  110. break;
  111. } else if (hasClass(imgs[i], "front") && imgs[i].index === imgs.length - 1) {
  112. fadeOut(imgs[imgs.length - 1]);
  113. imgs[imgs.length - 1].style.zIndex = -1;
  114. YEEPAY.removeClass(imgs[imgs.length - 1], "front");
  115. YEEPAY.addClass(imgs[0], "front");
  116. fadeIn(imgs[0]);
  117. imgs[0].style.zIndex = 1;
  118. for (var j = 0, l = lis.length; j < l; j++) {
  119. YEEPAY.removeClass(lis[j], "cur");
  120. }
  121. YEEPAY.addClass(lis[0], "cur");
  122. break;
  123. }
  124. }
  125. }
  126. }
  127. return {
  128. init: function () { //初始化
  129. for (var i = 0, l = imgs.length; i < l; i++) {
  130. if (hasClass(imgs[i], "front")) {
  131. imgs[i].style.cssText = "opacity:1; z-index:1; filter:alpha(opacity=100);";
  132. } else {
  133. imgs[i].style.cssText = "opacity:0; z-index:0; filter:alpha(opacity=0);";
  134. }
  135. }
  136. YEEPAY.addClass(lis[0], "cur");
  137. auto();
  138. }
  139. }
  140. }();
  141. Wrapper.init();