|
16 | 16 | <div class="swiper-slide"> |
17 | 17 | <img src="https://picsum.photos/800/400?random=3" alt="风景图片3"> |
18 | 18 | </div> |
| 19 | + <div class="swiper-slide"> |
| 20 | + <img src="https://picsum.photos/800/400?random=4" alt="风景图片4"> |
| 21 | + </div> |
| 22 | + <div class="swiper-slide"> |
| 23 | + <img src="https://picsum.photos/800/400?random=5" alt="风景图片5"> |
| 24 | + </div> |
19 | 25 | </div> |
20 | 26 | <!-- 分页器 --> |
21 | 27 | <div class="swiper-pagination"></div> |
|
27 | 33 |
|
28 | 34 | <script> |
29 | 35 | var swiper = new Swiper('.swiper-container', { |
| 36 | + // 确保一次只显示一张幻灯片 |
| 37 | + slidesPerView: 1, |
| 38 | + slidesPerGroup: 1, |
| 39 | + spaceBetween: 0, |
| 40 | + |
| 41 | + // 循环播放 |
30 | 42 | loop: true, |
| 43 | + |
| 44 | + // 自动播放设置 |
31 | 45 | autoplay: { |
32 | | - delay: 3000, |
| 46 | + delay: 4000, // 4秒切换 |
33 | 47 | disableOnInteraction: false, |
34 | 48 | }, |
| 49 | + |
| 50 | + // 分页器 |
35 | 51 | pagination: { |
36 | 52 | el: '.swiper-pagination', |
37 | 53 | clickable: true, |
| 54 | + dynamicBullets: true, |
38 | 55 | }, |
| 56 | + |
| 57 | + // 导航按钮 |
39 | 58 | navigation: { |
40 | 59 | nextEl: '.swiper-button-next', |
41 | 60 | prevEl: '.swiper-button-prev', |
42 | 61 | }, |
| 62 | + |
| 63 | + // 切换效果 - 使用淡入淡出而不是滑动 |
| 64 | + effect: 'fade', |
| 65 | + fadeEffect: { |
| 66 | + crossFade: true |
| 67 | + }, |
| 68 | + |
| 69 | + // 速度控制 |
| 70 | + speed: 800, |
43 | 71 | }); |
44 | 72 | </script> |
45 | 73 |
|
46 | 74 | <style> |
47 | 75 | /* 容器样式 - 限制宽度不遮挡导航栏 */ |
48 | 76 | .image-carousel-container { |
49 | | - max-width: 75%; /* 调整为适合的宽度 */ |
| 77 | + max-width: 75%; |
50 | 78 | margin: 20px auto; |
51 | 79 | padding: 0 15px; |
52 | 80 | } |
53 | 81 |
|
54 | 82 | .swiper-container { |
55 | 83 | width: 100%; |
56 | | - height: 350px; /* 稍微降低高度 */ |
| 84 | + height: 400px; |
57 | 85 | border-radius: 8px; |
| 86 | + overflow: hidden; /* 确保内容不会溢出 */ |
58 | 87 | box-shadow: 0 4px 12px rgba(0,0,0,0.1); |
59 | 88 | } |
60 | 89 |
|
| 90 | +/* 确保每个幻灯片占据整个容器 */ |
| 91 | +.swiper-slide { |
| 92 | + width: 100%; |
| 93 | + height: 100%; |
| 94 | + display: flex; |
| 95 | + justify-content: center; |
| 96 | + align-items: center; |
| 97 | +} |
| 98 | +
|
61 | 99 | .swiper-slide img { |
62 | 100 | width: 100%; |
63 | 101 | height: 100%; |
64 | 102 | object-fit: cover; |
65 | 103 | border-radius: 6px; |
66 | 104 | } |
67 | 105 |
|
68 | | -/* 响应式设计 - 在小屏幕上全宽度 */ |
69 | | -@media (max-width: 768px) { |
70 | | - .image-carousel-container { |
71 | | - max-width: 100%; |
72 | | - padding: 0 10px; |
73 | | - } |
74 | | - |
75 | | - .swiper-container { |
76 | | - height: 300px; |
77 | | - } |
| 106 | +/* 淡入淡出效果增强 */ |
| 107 | +.swiper-container-fade .swiper-slide { |
| 108 | + pointer-events: none; |
| 109 | +} |
| 110 | +.swiper-container-fade .swiper-slide-active { |
| 111 | + pointer-events: auto; |
78 | 112 | } |
79 | 113 |
|
80 | | -/* 导航按钮样式调整 */ |
| 114 | +/* 导航按钮样式 */ |
81 | 115 | .swiper-button-next, |
82 | 116 | .swiper-button-prev { |
83 | | - background-color: rgba(255,255,255,0.7); |
84 | | - width: 40px; |
85 | | - height: 40px; |
| 117 | + background-color: rgba(255,255,255,0.8); |
| 118 | + width: 44px; |
| 119 | + height: 44px; |
86 | 120 | border-radius: 50%; |
87 | 121 | color: #333; |
| 122 | + box-shadow: 0 2px 8px rgba(0,0,0,0.15); |
| 123 | + transition: all 0.3s ease; |
88 | 124 | } |
89 | 125 |
|
90 | 126 | .swiper-button-next:after, |
91 | 127 | .swiper-button-prev:after { |
92 | 128 | font-size: 18px; |
| 129 | + font-weight: bold; |
| 130 | +} |
| 131 | +
|
| 132 | +.swiper-button-next:hover, |
| 133 | +.swiper-button-prev:hover { |
| 134 | + background-color: rgba(255,255,255,0.95); |
| 135 | + transform: scale(1.05); |
93 | 136 | } |
94 | 137 |
|
95 | 138 | /* 分页器样式 */ |
| 139 | +.swiper-pagination { |
| 140 | + bottom: 15px !important; |
| 141 | +} |
| 142 | +
|
96 | 143 | .swiper-pagination-bullet { |
| 144 | + width: 10px; |
| 145 | + height: 10px; |
97 | 146 | background: #fff; |
98 | 147 | opacity: 0.7; |
| 148 | + transition: all 0.3s ease; |
99 | 149 | } |
100 | 150 |
|
101 | 151 | .swiper-pagination-bullet-active { |
102 | 152 | background: #007bff; |
103 | 153 | opacity: 1; |
| 154 | + transform: scale(1.2); |
| 155 | +} |
| 156 | +
|
| 157 | +/* 响应式设计 */ |
| 158 | +@media (max-width: 768px) { |
| 159 | + .image-carousel-container { |
| 160 | + max-width: 95%; |
| 161 | + padding: 0 10px; |
| 162 | + } |
| 163 | + |
| 164 | + .swiper-container { |
| 165 | + height: 300px; |
| 166 | + } |
| 167 | + |
| 168 | + .swiper-button-next, |
| 169 | + .swiper-button-prev { |
| 170 | + width: 36px; |
| 171 | + height: 36px; |
| 172 | + } |
| 173 | + |
| 174 | + .swiper-button-next:after, |
| 175 | + .swiper-button-prev:after { |
| 176 | + font-size: 16px; |
| 177 | + } |
104 | 178 | } |
105 | 179 | </style> |
106 | 180 | ++++ |
0 commit comments