萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

JS 實現仿百度汽車頻道鼠標懸停圖片展示切換效果

效果演示

模擬百度汽車選車頁面:多張不同尺寸車圖絕對定位排布,鼠標懸停顯示紅色邊框陰影與車型名稱標籤,移出恢復,純 CSS+JS 事件切換 className 實現。

核心 HTML 結構

<div class="main">
  <div class="single caraa" onmouseover="this.className='single caraa show'" onmouseout="this.className='single caraa'">
    <a href="#"><span>起亞K5</span></a>
  </div>
  <div class="single carbb" onmouseover="this.className='single carbb show'" onmouseout="this.className='single carbb'">
    <a href="#"><span>英郎</span></a>
  </div>
  <!-- 更多車型... -->
</div>

CSS 關鍵樣式

.main { position: relative; width: 978px; height: 165px; background: url(banner.jpg) center no-repeat; }
.single { position: absolute; }                    /* 絕對定位精確佈局 */
.single a { display: none; position: absolute; left: -3px; width: 100%; height: 100%; } /* 默認隱藏覆蓋層 */
.single a span { position: absolute; bottom: -20px; left: 0; width: 100%; height: 20px; line-height: 20px; background: #AE0D00; color: #fff; text-align: center; border: 3px solid #AE0D00; box-shadow: 3px 2px 3px #A0A0A0; }
.show { border: 3px solid #AE0D00; cursor: pointer; box-shadow: 3px 2px 3px #A0A0A0; } /* 懸停態:紅框+陰影 */
.show a { display: block; z-index: 99; }           /* 顯示覆蓋層與標籤 */

/* 各車型精確座標與尺寸 */
.caraa { left: 5px; top: 4px; width: 203px; height: 151px; }
.carbb { left: 209px; top: 4px; width: 101px; height: 101px; }
.carcc { left: 260px; top: 106px; width: 101px; height: 50px; }
... /* 共 10 個車位 */

交互邏輯解析

  1. 內聯事件綁定:onmouseover/onmouseout 直接在 HTML 中切換 className。
  2. 狀態類切換:single caraa(默認)↔ single caraa show(懸停)。
  3. CSS 級聯控制:.show 添加邊框陰影;.show a 顯示覆蓋層;.show a span 顯示車名標籤。
  4. 疊層管理:z-index: 99 確保覆蓋層在最上層。

現代化重構建議

1. 事件委託 + 數據驅動

const cars = [
  {id: 'car1', name: '起亞K5', left: 5, top: 4, w: 203, h: 151},
  {id: 'car2', name: '英郎', left: 209, top: 4, w: 101, h: 101},
  // ...
];

const main = document.querySelector('.main');
cars.forEach(car => {
  const el = document.createElement('div');
  el.className = 'car-item';
  el.dataset.id = car.id;
  el.style.cssText = `left:${car.left}px;top:${car.top}px;width:${car.w}px;height:${car.h}px;`;
  el.innerHTML = `${car.name}`;
  main.appendChild(el);
});

main.addEventListener('mouseover', e => {
  const item = e.target.closest('.car-item');
  if (item) item.classList.add('show');
});
main.addEventListener('mouseout', e => {
  const item = e.target.closest('.car-item');
  if (item) item.classList.remove('show');
});

2. CSS :hover 純樣式實現(無 JS)

.car-item { position: absolute; transition: box-shadow .2s, border-color .2s; }
.car-item a { display: none; position: absolute; inset: -3px; }
.car-item a span { position: absolute; bottom: -20px; left: 0; right: 0; height: 20px; line-height: 20px; background: #AE0D00; color: #fff; text-align: center; border: 3px solid #AE0D00; box-shadow: 3px 2px 3px #A0A0A0; opacity: 0; transform: translateY(10px); transition: .2s; }
.car-item:hover { border-color: #AE0D00; box-shadow: 3px 2px 3px #A0A0A0; }
.car-item:hover a { display: block; }
.car-item:hover a span { opacity: 1; transform: translateY(0); }

純 CSS 實現懸停動畫,性能更優、無 JS 依賴、支持鍵盤聚焦(:focus-within)。

無障礙增強

  • 鍵盤 Tab 聚焦觸發 :focus-within 顯示標籤。
  • aria-label 標註車型名稱。
  • 確保顏色對比度達 WCAG AA 級。
copyright © 萬盛學電腦網 all rights reserved