productDetails.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. $(document).ready(function () {
  2. //触屏事件 || 点击事件
  3. const tap = "ontouchstart" in window ? "touchstart" : "click";
  4. //缩略图长度
  5. let slideCount = 0
  6. const visibleSlides = 4;
  7. let currentSlide = 0;
  8. let isSlidedOut = false;
  9. //其他详情图是否超出4张
  10. const initThumbnail = () => {
  11. setTimeout(() => {
  12. slideCount = $('.prImgList .prImgItem').length;
  13. if (slideCount > visibleSlides) {
  14. $('.prevArrow').show(0)
  15. $('.nextArrow').show(0)
  16. }
  17. }, 0)
  18. }
  19. initThumbnail()
  20. //商品其他详情图滑动
  21. function updateSlider() {
  22. var slideWidth = $('.prImgItem').outerWidth(true);
  23. var slideOffset = currentSlide * slideWidth;
  24. $('.prImgList').css('transform', 'translateX(' + - slideOffset + 'px)');
  25. }
  26. //上一张
  27. $(".prevArrow").on(tap, function () {
  28. if (currentSlide > 0) {
  29. currentSlide--;
  30. updateSlider();
  31. }
  32. });
  33. //下一张
  34. $(".nextArrow").on(tap, function () {
  35. if (currentSlide < slideCount - visibleSlides) {
  36. currentSlide++;
  37. updateSlider();
  38. }
  39. });
  40. //监听页面滚动
  41. $(window).scroll(function () {
  42. //页面滚动高度
  43. let windowTop = $(window).scrollTop();
  44. if (windowTop > 100 && !isSlidedOut) {
  45. $('.fixedAddSection').slideDown();
  46. isSlidedOut = true;
  47. } else if (windowTop <= 100 && isSlidedOut) {
  48. $('.fixedAddSection').slideUp();
  49. isSlidedOut = false;
  50. }
  51. });
  52. //商品其他详情图点击
  53. $(".prImgItem").on(tap, function () {
  54. const src = $(this).find("img").attr('src')
  55. if (src && src != "") {
  56. $('.prViewImg').attr('src', src);
  57. }
  58. });
  59. // 限制商品数量只能输入数字
  60. const nonNumeric = (event) => {
  61. // 获取按下的键的键码
  62. var keyCode = event.which || event.keyCode;
  63. // 允许使用的特殊键的键码(例如:Backspace、Delete、方向键等)
  64. var allowedSpecialKeys = [8, 9, 37, 39, 46];
  65. // 允许使用的数字键的键码(0-9)
  66. var allowedNumericKeys = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57];
  67. // 检查是否按下了允许使用的特殊键
  68. if ($.inArray(keyCode, allowedSpecialKeys) !== -1) {
  69. return;
  70. }
  71. // 检查是否按下了允许使用的数字键
  72. if ($.inArray(keyCode, allowedNumericKeys) === -1) {
  73. event.preventDefault();
  74. }
  75. }
  76. $('#numberInput').keydown(nonNumeric);
  77. $('#fPrNumberInput').keydown(nonNumeric);
  78. });