DIV CSS 佈局教程網

用 jQuery 實現背景牆聚光燈效果
編輯:JavaScript綜合知識     

「聚光燈」效果的思路很簡單:一堆圖片平鋪排列,鼠標移到某一張上時,那張保持全亮並稍微放大突出,其餘的整體壓暗。視覺上就像一束光打在當前位置。

核心腳本

<script type="text/javascript" charset="utf-8" src="jquery.js"></script>
<script type="text/javascript" charset="utf-8">
$(window).load(function () {
    var spotlight = {
        // 非懸停圖片的透明度,可自行調整
        opacity: 0.2,
        // 讀取圖片尺寸,以便把 li 撐得一樣大
        imgWidth:  $('.spotlightWrapper ul li').find('img').width(),
        imgHeight: $('.spotlightWrapper ul li').find('img').height()
    };

    // 把每個列表項的尺寸設為與圖片一致
    $('.spotlightWrapper ul li').css({
        'width':  spotlight.imgWidth,
        'height': spotlight.imgHeight
    });

    // 鼠標移入列表項
    $('.spotlightWrapper ul li').hover(function () {
        // 給內部圖片加 active 類並恢復不透明
        $(this).find('img').addClass('active').css({ 'opacity': 1 });

        // 把兄弟項的圖片壓暗
        $(this).siblings('li').find('img').css({ 'opacity': spotlight.opacity });
    }, function () {
        // 移出時移除 active 類,透明度由 CSS 接管
        $(this).find('img').removeClass('active');
    });

    // 鼠標離開整個列表時,全部恢復
    $('.spotlightWrapper ul').bind('mouseleave', function () {
        $(this).find('img').css('opacity', 1);
    });
});
</script>

樣式

<style type="text/css" media="screen">
body {
    background: black;
    color: white;
    font-family: 'georgia';
}

.spotlightWrapper ul {
    list-style-type: none;   /* 去掉默認的圓點 */
    margin: 0;
    padding: 0;
}

.spotlightWrapper ul li {
    float: left;
    position: relative;      /* 為了配合 active 的偏移定位 */
}

.spotlightWrapper ul li a img {
    width: 200px;
    position: relative;
    border: none;
    transition: opacity 0.3s;   /* 讓明暗變化平滑過渡 */
}

.spotlightWrapper ul li a img.active {
    border: 4px solid white;
    z-index: 1;        /* 蓋在其他圖片之上 */
    left: -4px;        /* 抵消 border 造成的位移 */
    top:  -4px;
}

.clear { clear: both; }   /* 清除最後一行的浮動 */
</style>

結構

<h3>jQuery 背景牆聚光燈效果</h3>
<p>點擊圖片查看效果</p>

<div class='spotlightWrapper'>
  <ul>
    <li><a href='#'><img src='images/1.jpg' /></a></li>
    <li><a href='#'><img src='images/2.jpg' /></a></li>
    <li><a href='#'><img src='images/3.png' /></a></li>
    <li><a href='#'><img src='images/4.jpg' /></a></li>
    <div class='clear'></div>
  </ul>
</div>

原理拆解與改進建議

為什麼用 $(window).load() 而不是 ready?因為腳本要讀取圖片的 width() 和 height()。在 ready 階段圖片可能還沒下載完,此時尺寸讀出來是 0,列表項會被壓成一條線。等到 load(所有資源就緒)才安全。這是一個很典型的「依賴資源尺寸」場景。

為什麼 active 狀態要用 left/top 負偏移?加了 4px 的白色邊框後,元素實際佔用的空間變大了 8px,會把周圍元素擠開。用 left: -4px; top: -4px 把視覺位置拉回原位,圖片看起來才是「原地放大」而不是「把鄰居推開」。

三處可以做的改進:

其一,load 事件已經被標準廢棄,現代寫法應該在 CSS 裡直接給 img 固定寬高,或者用 aspect-ratio 保持比例,這樣就不必等圖片加載。

其二,opacity 的變化加了 transition 之後效果柔和很多,原代碼沒有這個過渡,明暗切換會顯得生硬。

其三,.clear 那個 div 可以刪掉,改用 flex 布局一行搞定並自動換行:

.spotlightWrapper ul {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
}

最後一個實務提醒:純 hover 效果在觸屏設備上完全不觸發。移動端需要改用點擊切換,或者乾脆去掉壓暗效果——在沒有鼠標的環境裡,「聚光燈」這個隱喻本身就不成立。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved