script.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // 页面加载完成后执行
  2. document.addEventListener('DOMContentLoaded', function() {
  3. // 添加卡片点击效果
  4. addCardClickEffects();
  5. // 添加滚动动画
  6. addScrollAnimations();
  7. // 添加触摸反馈
  8. addTouchFeedback();
  9. });
  10. // 添加卡片点击效果
  11. function addCardClickEffects() {
  12. const cards = document.querySelectorAll('.card');
  13. cards.forEach((card, index) => {
  14. // 鼠标悬停效果
  15. card.addEventListener('mouseenter', function() {
  16. this.style.transform = 'translateY(-4px) scale(1.02)';
  17. });
  18. card.addEventListener('mouseleave', function() {
  19. this.style.transform = 'translateY(0) scale(1)';
  20. });
  21. });
  22. }
  23. // 添加滚动动画
  24. function addScrollAnimations() {
  25. const cards = document.querySelectorAll('.card');
  26. const observer = new IntersectionObserver((entries) => {
  27. entries.forEach((entry, index) => {
  28. if (entry.isIntersecting) {
  29. entry.target.style.opacity = '0';
  30. entry.target.style.transform = 'translateY(30px)';
  31. setTimeout(() => {
  32. entry.target.style.transition = 'all 0.6s ease';
  33. entry.target.style.opacity = '1';
  34. entry.target.style.transform = 'translateY(0)';
  35. }, index * 100);
  36. }
  37. });
  38. }, {
  39. threshold: 0.1,
  40. rootMargin: '0px 0px -50px 0px'
  41. });
  42. cards.forEach(card => {
  43. observer.observe(card);
  44. });
  45. }
  46. // 添加触摸反馈
  47. function addTouchFeedback() {
  48. const cards = document.querySelectorAll('.card');
  49. cards.forEach(card => {
  50. card.addEventListener('touchstart', function() {
  51. this.style.transform = 'scale(0.98)';
  52. });
  53. card.addEventListener('touchend', function() {
  54. this.style.transform = 'scale(1)';
  55. });
  56. });
  57. }
  58. // 添加CSS动画样式
  59. const style = document.createElement('style');
  60. style.textContent = `
  61. @keyframes ripple {
  62. to {
  63. transform: scale(4);
  64. opacity: 0;
  65. }
  66. }
  67. .card {
  68. transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  69. }
  70. .card:active {
  71. transform: scale(0.98) !important;
  72. }
  73. `;
  74. document.head.appendChild(style);
  75. // 添加页面加载动画
  76. window.addEventListener('load', function() {
  77. document.body.style.opacity = '0';
  78. document.body.style.transition = 'opacity 0.5s ease';
  79. setTimeout(() => {
  80. document.body.style.opacity = '1';
  81. }, 100);
  82. });
  83. // 添加键盘导航支持
  84. document.addEventListener('keydown', function(e) {
  85. const cards = document.querySelectorAll('.card');
  86. const currentIndex = Array.from(cards).findIndex(card =>
  87. card === document.activeElement || card.contains(document.activeElement)
  88. );
  89. let nextIndex = currentIndex;
  90. switch(e.key) {
  91. case 'ArrowDown':
  92. e.preventDefault();
  93. nextIndex = Math.min(currentIndex + 1, cards.length - 1);
  94. break;
  95. case 'ArrowUp':
  96. e.preventDefault();
  97. nextIndex = Math.max(currentIndex - 1, 0);
  98. break;
  99. case 'Enter':
  100. case ' ':
  101. e.preventDefault();
  102. if (currentIndex >= 0) {
  103. cards[currentIndex].click();
  104. }
  105. break;
  106. }
  107. if (nextIndex !== currentIndex && nextIndex >= 0) {
  108. cards[nextIndex].focus();
  109. }
  110. });
  111. // 为卡片添加焦点样式
  112. document.querySelectorAll('.card').forEach(card => {
  113. card.setAttribute('tabindex', '0');
  114. });