index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. window.addEventListener('scroll', handleScroll, true)
  2. function handleScroll() {
  3. if (window.scrollY > 100) {
  4. document.getElementsByClassName('handler-app-android-download').item(0).className = "handler-app-android-download"
  5. document.getElementsByClassName("handler-to-top").item(0).style.display = "block"
  6. } else {
  7. document.getElementsByClassName('handler-app-android-download').item(0).className = "handler-app-android-download handler-item-bottom"
  8. document.getElementsByClassName("handler-to-top").item(0).style.display = "none"
  9. }
  10. }
  11. function ToTop() {
  12. window.scrollTo({
  13. top: 0,
  14. behavior: 'smooth'
  15. })
  16. }
  17. /*//todo 首页轮播图
  18. window.onload = function () {
  19. var items = document.getElementsByClassName("item");
  20. var circles = document.getElementsByClassName("circle");
  21. var leftBtn = document.getElementById("btn-left");
  22. var rightBtn = document.getElementById("btn-right");
  23. var content = document.querySelector('.content');
  24. var index = 0;
  25. var timer = null;
  26. //清除class
  27. var clearclass = function () {
  28. for (let i = 0; i < items.length; i++) {
  29. items[i].className = "item";
  30. circles[i].className = "circle";
  31. circles[i].setAttribute("num", i);
  32. }
  33. }
  34. /!*只显示一个class*!/
  35. function move() {
  36. clearclass();
  37. items[index].className = "item active";
  38. circles[index].className = "circle white";
  39. }
  40. //点击右边按钮切换下一张图片
  41. rightBtn.onclick = function () {
  42. if (index === items.length - 1) {
  43. index = 0;
  44. } else {
  45. index++
  46. }
  47. move();
  48. }
  49. //点击左边按钮切换上一张图片
  50. leftBtn.onclick = function () {
  51. if (index === 0) {
  52. index = items.length - 1;
  53. } else {
  54. index--
  55. }
  56. move();
  57. }
  58. //开始定时器,点击右边按钮,实现轮播
  59. rightBtn.onclick();
  60. timer = setInterval(function () {
  61. rightBtn.onclick();
  62. }, 3000)
  63. //点击圆点时,跳转到对应图片
  64. for (var i = 0; i < circles.length; i++) {
  65. circles[i].addEventListener("click", function () {
  66. var point_index = this.getAttribute("num");
  67. index = point_index;
  68. move();
  69. })
  70. }
  71. //鼠标移入清除定时器,并开启一个三秒的定时器,使慢慢转动
  72. content.onmouseover = function () {
  73. clearInterval(timer);
  74. timer = setInterval(function () {
  75. rightBtn.onclick();
  76. }, 3000)
  77. }
  78. //鼠标移出又开启定时器
  79. content.onmouseleave = function () {
  80. clearInterval(timer);
  81. timer = setInterval(function () {
  82. rightBtn.onclick();
  83. }, 1500)
  84. }
  85. }*/
  86. window.onload = function () {
  87. var items = document.getElementsByClassName("item");
  88. var circles = document.getElementsByClassName("circle");
  89. var leftBtn = document.getElementById("btn-left");
  90. var rightBtn = document.getElementById("btn-right");
  91. var content = document.querySelector('.content');
  92. var index = 0;
  93. var timer = null;
  94. var startX = 0; // 用于记录触摸起始点
  95. var isDragging = false; // 标记是否正在滑动
  96. // 清除class
  97. var clearclass = function () {
  98. for (let i = 0; i < items.length; i++) {
  99. items[i].className = "item";
  100. circles[i].className = "circle";
  101. circles[i].setAttribute("num", i);
  102. }
  103. }
  104. // 显示当前选中的图片和圆点
  105. function move() {
  106. clearclass();
  107. items[index].className = "item active";
  108. circles[index].className = "circle white";
  109. }
  110. // 点击右边按钮切换下一张图片
  111. rightBtn.onclick = function () {
  112. if (index === items.length - 1) {
  113. index = 0;
  114. } else {
  115. index++;
  116. }
  117. move();
  118. }
  119. // 点击左边按钮切换上一张图片
  120. leftBtn.onclick = function () {
  121. if (index === 0) {
  122. index = items.length - 1;
  123. } else {
  124. index--;
  125. }
  126. move();
  127. }
  128. //避免打开页面白屏
  129. rightBtn.click()
  130. leftBtn.click()
  131. // 开始定时器,实现自动轮播
  132. timer = setInterval(function () {
  133. rightBtn.onclick();
  134. }, 3000);
  135. // 点击圆点时,跳转到对应图片
  136. for (var i = 0; i < circles.length; i++) {
  137. circles[i].addEventListener("click", function () {
  138. var point_index = this.getAttribute("num");
  139. index = point_index;
  140. move();
  141. });
  142. }
  143. // 鼠标移入清除定时器
  144. content.onmouseover = function () {
  145. clearInterval(timer);
  146. }
  147. // 鼠标移出重新开始定时器
  148. content.onmouseleave = function () {
  149. clearInterval(timer);
  150. timer = setInterval(function () {
  151. rightBtn.onclick();
  152. }, 3000);
  153. }
  154. // 触摸事件:触摸开始
  155. content.addEventListener('touchstart', function (e) {
  156. startX = e.touches[0].clientX;
  157. isDragging = true;
  158. clearInterval(timer); // 触摸时暂停自动轮播
  159. });
  160. // 触摸事件:触摸移动
  161. content.addEventListener('touchmove', function (e) {
  162. if (!isDragging) return;
  163. var currentX = e.touches[0].clientX;
  164. var diffX = currentX - startX;
  165. if (diffX > 50) { // 向右滑动
  166. leftBtn.onclick(); // 切换到上一张
  167. isDragging = false;
  168. } else if (diffX < -50) { // 向左滑动
  169. rightBtn.onclick(); // 切换到下一张
  170. isDragging = false;
  171. }
  172. });
  173. // 触摸事件:触摸结束
  174. content.addEventListener('touchend', function () {
  175. isDragging = false;
  176. timer = setInterval(function () {
  177. rightBtn.onclick();
  178. }, 3000); // 重新开始自动轮播
  179. });
  180. }
  181. var CardListElements = null
  182. //初始化加载 topic列表
  183. function TopicListShowLoad() {
  184. CardListElements = document.getElementsByClassName("card-list");
  185. for (let i = 0; i < CardListElements.length; i++) {
  186. let goodsInfoEles = CardListElements[i].getElementsByClassName("goods-info")
  187. for (let i = 0; i < goodsInfoEles.length; i++) {
  188. if (goodsInfoEles[i].getAttribute("index") > 12) {
  189. goodsInfoEles[i].style.display = "none";
  190. }
  191. }
  192. let showEle = CardListElements[i].getElementsByClassName("card-show")
  193. if (!showEle || showEle.length === 0) {
  194. continue
  195. }
  196. let linkElement = showEle.item(0).getElementsByTagName("a").item(0);
  197. linkElement.addEventListener("click", (e) => {
  198. if (linkElement.getAttribute("show")==="true") {
  199. linkElement.setAttribute("show", "false");
  200. linkElement.innerHTML = "查看更多";
  201. for (let i = 0; i < goodsInfoEles.length; i++) {
  202. if (goodsInfoEles[i].getAttribute("index") > 12) {
  203. goodsInfoEles[i].style.display = "none";
  204. }
  205. }
  206. } else {
  207. linkElement.setAttribute("show", "true");
  208. linkElement.innerHTML = "收起";
  209. for (let i = 0; i < goodsInfoEles.length; i++) {
  210. if (goodsInfoEles[i].getAttribute("index") > 12) {
  211. goodsInfoEles[i].style.display = "inline-block";
  212. }
  213. }
  214. }
  215. })
  216. }
  217. // let goodsInfoEles = document.getElementsByClassName("goods-info");
  218. // for (let i = 0; i < goodsInfoEles.length; i++) {
  219. // if (goodsInfoEles[i].getAttribute("index") > 12) {
  220. // goodsInfoEles[i].style.display = "none";
  221. // }
  222. // }
  223. }
  224. handleScroll()
  225. TopicListShowLoad()