萬盛學電腦網

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

jQuery 實作網頁固定工具列 (Toolbar) 完整教學:HTML、CSS、動畫、社群圖示、提示氣泡

功能概覽

  • 固定於視窗底部 (position: fixed)
  • 左側:社群圖示 (RSS, Facebook, Twitter) + 懸停提示氣泡
  • 右側:快速選單 + 隱藏/顯示切換按鈕
  • jQuery 動畫:slideToggle 隱藏/顯示,fadeIn/Out 按鈕切換
  • CSS3 圓角、陰影、漸層背景

HTML 結構

<!-- 顯示按鈕 (工具列隱藏時出現) -->
<div id="toolbarBut" style="display:none;">
    <span class="showBar"><a href="#">顯示工具列</a></span>
</div>

<!-- 主工具列容器 -->
<div id="toolbar">
    <!-- 左側:社群圖示 -->
    <div class="leftSide">
        <ul id="social">
            <li>
                <a class="rss" href="#"></a>
                <div id="tipRss" class="tip">
                    <ul>
                        <li><a href="#">580 位訂閱者</a></li>
                        <li><a href="#"><small>[訂閱]</small></a></li>
                    </ul>
                </div>
            </li>
            <li>
                <a class="facebook" href="#"></a>
                <div id="tipFacebook" class="tip">
                    <ul>
                        <li><a href="#">分享頁面</a></li>
                        <li><a href="#">個人檔案</a></li>
                    </ul>
                </div>
            </li>
            <li>
                <a class="twitter" href="#"></a>
                <div id="tipTwitter" class="tip">
                    <ul>
                        <li><a href="#">轉推</a></li>
                    </ul>
                </div>
            </li>
        </ul>
    </div>

    <!-- 右側:隱藏按鈕 + 快速選單 -->
    <div class="rightSide">
        <span class="downArr"><a href="#" title="隱藏工具列"></a></span>
        <ul class="quickMenu">
            <li><a href="#">設定</a></li>
            <li><a href="#">說明</a></li>
            <li><a href="#">登出</a></li>
        </ul>
    </div>
</div>

CSS 樣式 (精簡核心)

/***** 共用固定定位 *****/
#toolbar, #toolbarBut {
    position: fixed;
    bottom: 0; right: 0; left: 0;
    z-index: 9999;
    height: 36px;
    background: url(images/bcktool.png) repeat-x;
    border-top: 1px solid #eee;
    border-left: 1px solid #eee;
    border-right: 1px solid #eee;
    /* CSS3 圓角 (僅頂部) */
    border-radius: 8px 8px 0 0;
    /* 陰影 */
    box-shadow: 0 -1px 10px #666, inset 0 1px 0 #a4a4a4;
}

/***** 主工具列 *****/
#toolbar {
    width: 85%;
    min-width: 700px;
    margin: 0 auto; /* 水平置中 */
}

/***** 顯示按鈕區 *****/
#toolbarBut {
    width: 60px; height: 15px;
    margin-right: 3%;
    display: none;
}
.showBar a { display:block; padding:5px; font-size:10px; color:#989898; text-align:center; }

/***** 兩側浮動 *****/
.leftSide { float: left; }
.rightSide { float: right; }

/***** 社群圖示 *****/
#social { list-style:none; margin:0; padding:5px 0; }
#social li { float:left; margin:0 5px; position:relative; }
#social a { display:block; width:26px; height:26px; background:url(icons.png) no-repeat; }
#social a.rss { background-position: 0 0; }
#social a.facebook { background-position: -26px 0; }
#social a.twitter { background-position: -52px 0; }

/***** 提示氣泡 *****/
.tip { 
    display:none; position:absolute; bottom:36px; left:50%; margin-left:-60px;
    background:#333; color:#fff; padding:8px 12px; border-radius:4px; 
    white-space:nowrap; font-size:12px; z-index:10000;
}
.tip::after { content:''; position:absolute; top:100%; left:50%; margin-left:-5px; 
    border:5px solid transparent; border-top-color:#333; }
.tip ul { list-style:none; margin:0; padding:0; }
.tip a { color:#fff; text-decoration:none; }
.tip a:hover { text-decoration:underline; }

/***** 隱藏按鈕 (下箭頭) *****/
.downArr { float:right; border-left:1px solid #a4a4a4; }
.downArr a { display:block; width:36px; height:26px; padding:25px 0 0 10px; 
    background:url(images/downarrow.png) no-repeat 5px 7px; }

/***** 快速選單 *****/
.quickMenu { list-style:none; margin:0; padding:5px 10px 0 0; }
.quickMenu li { display:inline-block; margin-left:10px; }
.quickMenu a { color:#333; text-decoration:none; font-size:12px; }
.quickMenu a:hover { color:#0066cc; }

jQuery 互動邏輯

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(function() {
    var $toolbar = $('#toolbar');
    var $toolbarBut = $('#toolbarBut');
    var $tipTriggers = $('#social li'); // 觸發提示的 li

    // --- 隱藏/顯示切換 ---
    $('.downArr a').on('click', function(e) {
        e.preventDefault();
        $toolbar.slideToggle('fast');
        $toolbarBut.fadeIn('slow');
    });

    $('.showBar a').on('click', function(e) {
        e.preventDefault();
        $toolbar.slideToggle('fast');
        $toolbarBut.fadeOut('fast');
    });

    // --- 提示氣泡:滑鼠進入/離開 ---
    $tipTriggers.hover(
        function() { // mouseenter
            $(this).find('.tip').stop(true, true).fadeIn('fast');
        },
        function() { // mouseleave
            $(this).find('.tip').stop(true, true).fadeOut('fast');
        }
    );

    // --- 快速選單點擊 (示範) ---
    $('.quickMenu a').on('click', function(e) {
        e.preventDefault();
        alert('選單項目: ' + $(this).text());
    });
});
</script>

進階強化建議

  1. 響應式斷點:小螢幕時隱藏文字標籤、縮減間距、改為漢堡選單。
  2. 狀態持久化:用 localStorage 記住使用者偏好 (展開/收合),下次載入自動套用。
  3. 無障礙 (a11y):加上 role="toolbar"、aria-label、鍵盤可聚焦、ESC 關閉。
  4. 圖示優化:改用 SVG Sprite 或 Icon Font (FontAwesome),支援高解析度螢幕。
  5. 效能:將提示氣泡內容改為資料屬性 data-tip,滑鼠進入時動態建立,減少初始 DOM。

總結

固定工具列是提升網站互動性的經典元件。本文提供從結構、樣式、動畫到進階優化的完整實作,可直接套用於專案,並建議依專案需求進行模組化封裝 (ES6 Class / Web Component)。

copyright © 萬盛學電腦網 all rights reserved