优惠券分享

slideshow.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Created by libo on 2017/11/9.
  3. */
  4. window.onload = function(){
  5. /*
  6. * 1.×Ô¶¯ÂÖ²¥ ¶¨Ê±Æ÷ ÎÞ·ìÏÎ½Ó ¶¯»­½áÊø˲¼ä¶¨Î»
  7. * 2.µãÐèÒªËæ×ÅÂÖ²¥µÄ¹ö¶¯¸Ä±ä¶ÔÓ¦µÄµã ¸Ä±äµ±Ç°Ñùʽ µ±Ç°Í¼Æ¬µÄË÷Òý
  8. * 3.ÊÖÖ¸»¬¶¯µÄʱºòÈÃÂÖ²¥Í¼»¬¶¯ touchʼþ ¼Ç¼×ø±êÖáµÄ¸Ä±ä ¸Ä±äÂÖ²¥Í¼µÄ¶¨Î»£¨Î»ÒÆcss3£©
  9. * 4.µ±»¬¶¯µÄ¾àÀë²»³¬¹ýÒ»¶¨µÄ¾àÀëµÄʱºò ÐèÒªÎü¸½»ØÈ¥ ¹ý¶ÉµÄÐÎʽȥ×ö
  10. * 5.µ±»¬¶¯³¬¹ýÁËÒ»¶¨µÄ¾àÀë ÐèÒª Ìøµ½ ÏÂÒ»ÕÅ»òÕßÉÏÒ»ÕÅ £¨»¬¶¯µÄ·½Ïò£© Ò»¶¨µÄ¾àÀ루ÆÁÄ»µÄÈý·ÖÖ®Ò»£©
  11. * */
  12. var imageCount = 7; //Ò³ÃæÖÐÓÃÀ´ÂÖ²¥µÄͼƬÓÐ5ÕŲ»Í¬µÄ
  13. //ÂÖ²¥Í¼´óºÐ×Ó
  14. var banner = document.querySelector('.banner');
  15. //ͼƬµÄ¿í¶È
  16. var width = banner.offsetWidth;
  17. //ͼƬºÐ×Ó
  18. var imageBox = banner.querySelector('ul:first-child');
  19. //µãºÐ×Ó
  20. var pointBox = banner.querySelector('ul:last-child');
  21. //ËùÓеĵã
  22. var points = pointBox.querySelectorAll('li');
  23. //¹«Ó÷½·¨
  24. //¼Ó¹ý¶É
  25. var addTransition = function(){
  26. imageBox.style.transition = "all 0.3s";
  27. imageBox.style.webkitTransition = "all 0.3s";/*×ö¼æÈÝ*/
  28. };
  29. //Çå³ý¹ý¶É
  30. var removeTransition = function(){
  31. imageBox.style.transition = "none";
  32. imageBox.style.webkitTransition = "none";
  33. }
  34. //¶¨Î»
  35. var setTranslateX = function(translateX){
  36. var translateX = translateX + 375
  37. imageBox.style.transform = "translateX("+translateX+"px)";
  38. imageBox.style.webkitTransform = "translateX("+translateX+"px)";
  39. }
  40. //¹¦ÄÜʵÏÖ
  41. //×Ô¶¯ÂÖ²¥ ¶¨Ê±Æ÷ ÎÞ·ìÏÎ½Ó ¶¯»­½áÊø˲¼ä¶¨Î»
  42. var index = 1;
  43. var timer = setInterval(function(){
  44. index++ ; //×Ô¶¯ÂÖ²¥µ½ÏÂÒ»ÕÅ
  45. addTransition(); //¼Ó¹ý¶É¶¯»­
  46. setTranslateX(-index * width); //¶¨Î»
  47. },3000);
  48. //µÈ¹ý¶É½áÊøÖ®ºóÀ´×öÎÞ·ìÏνÓ
  49. my.transitionEnd(imageBox, function(){
  50. //´¦Àíʼþ½áÊøºóµÄÒµÎñÂß¼­
  51. if(index > imageCount ){
  52. index = 1;
  53. }else if(index <= 0){
  54. index = imageCount;
  55. }
  56. removeTransition(); //Çå³ý¹ý¶É
  57. setTranslateX(-index * width); //¶¨Î»
  58. setPoint(); //ÉèÖõײ¿ÏÔʾµ±Ç°Í¼Æ¬¶ÔÓ¦µÄÔ²½Ç
  59. });
  60. //¸Ä±äµ±Ç°Ñùʽ µ±Ç°Í¼Æ¬µÄË÷Òý
  61. var setPoint = function(){
  62. //Çå³ýÉÏÒ»´ÎµÄnow
  63. for(var i = 0 ; i < points.length ; i++){
  64. points[i].className = " ";
  65. }
  66. //¸øͼƬ¶ÔÓ¦µÄµã¼ÓÉÏÑùʽ
  67. points[index-1].className = "now";
  68. }
  69. /*
  70. ÊÖÖ¸»¬¶¯µÄʱºòÈÃÂÖ²¥Í¼»¬¶¯ touchʼþ ¼Ç¼×ø±êÖáµÄ¸Ä±ä ¸Ä±äÂÖ²¥Í¼µÄ¶¨Î»£¨Î»ÒÆcss3£©
  71. µ±»¬¶¯µÄ¾àÀë²»³¬¹ýÒ»¶¨µÄ¾àÀëµÄʱºò ÐèÒªÎü¸½»ØÈ¥ ¹ý¶ÉµÄÐÎʽȥ×ö
  72. µ±»¬¶¯³¬¹ýÁËÒ»¶¨µÄ¾àÀë ÐèÒª Ìøµ½ ÏÂÒ»ÕÅ»òÕßÉÏÒ»ÕÅ £¨»¬¶¯µÄ·½Ïò£© Ò»¶¨µÄ¾àÀ루ÆÁÄ»µÄÈý·ÖÖ®Ò»£©
  73. */
  74. //touchʼþ
  75. var startX = 0; //¼Ç¼Æðʼ ¸Õ¸Õ´¥ÃþµÄµãµÄλÖà xµÄ×ø±ê
  76. var moveX = 0; //»¬¶¯µÄʱºòxµÄλÖÃ
  77. var distanceX = 0; //»¬¶¯µÄ¾àÀë
  78. var isMove = false; //ÊÇ·ñ»¬¶¯¹ý
  79. imageBox.addEventListener('touchstart', function(e){
  80. clearInterval(timer); //Çå³ý¶¨Ê±Æ÷
  81. startX = e.touches[0].clientX; //¼Ç¼ÆðʼX
  82. });
  83. imageBox.addEventListener('touchmove',function(e){
  84. moveX = e.touches[0].clientX; //»¬¶¯Ê±ºòµÄX
  85. distanceX = moveX - startX; //¼ÆËãÒƶ¯µÄ¾àÀë
  86. //¼ÆË㵱ǰ¶¨Î» -index*width+distanceX
  87. removeTransition(); //Çå³ý¹ý¶É
  88. setTranslateX(-index * width + distanceX); //ʵʱµÄ¶¨Î»
  89. isMove = true; //Ö¤Ã÷»¬¶¯¹ý
  90. });
  91. //ÔÚÄ£ÄâÆ÷ÉÏÄ£ÄâµÄ»¬¶¯»áÓÐÎÊÌâ ¶ªÊ§µÄÇé¿ö ×îºóÔÚÄ£ÄâÆ÷µÄʱºòÓÃwindow
  92. imageBox.addEventListener('touchend', function(e){
  93. // »¬¶¯³¬¹ý 1/3 ¼´Îª»¬¶¯ÓÐЧ£¬·ñÔò¼´ÎªÎÞЧ£¬ÔòÎü¸½»ØÈ¥
  94. if(isMove && Math.abs(distanceX) > width/3){
  95. //5.µ±»¬¶¯³¬¹ýÁËÒ»¶¨µÄ¾àÀë ÐèÒª Ìøµ½ ÏÂÒ»ÕÅ»òÕßÉÏÒ»ÕÅ £¨»¬¶¯µÄ·½Ïò£©*/
  96. if(distanceX > 0){ //ÉÏÒ»ÕÅ
  97. index --;
  98. }
  99. else{ //ÏÂÒ»ÕÅ
  100. index ++;
  101. }
  102. }
  103. addTransition(); //¼Ó¹ý¶É¶¯»­
  104. setTranslateX(-index * width); //¶¨Î»
  105. if(index > imageCount ){
  106. index = 1;
  107. }else if(index <= 0){
  108. index = imageCount;
  109. }
  110. setPoint();
  111. //ÖØÖòÎÊý
  112. startX = 0;
  113. moveX = 0;
  114. distanceX = 0;
  115. isMove = false;
  116. //¼Ó¶¨Ê±Æ÷
  117. clearInterval(timer); //ÑϽ÷ ÔÙÇå³ýÒ»´Î¶¨Ê±Æ÷
  118. timer= setInterval(function(){
  119. index++ ; //×Ô¶¯ÂÖ²¥µ½ÏÂÒ»ÕÅ
  120. addTransition(); //¼Ó¹ý¶É¶¯»­
  121. setTranslateX(-index * width); //¶¨Î»
  122. },3000);
  123. });
  124. };