萬盛學電腦網

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

JavaScript 滑鼠移動顯示圖片工具提示實作範例

簡介

在網頁中,當使用者將滑鼠移至連結或元素上時,自動顯示對應圖片的工具提示,能大幅提升資訊傳遞效率。本文提供一個純 JavaScript 實作範例,無需外部函式庫即可完成。

核心邏輯

程式碼主要分為三大部分:

  1. 預設參數設定:延遲顯示時間(tPopWait)、顯示持續時間(tPopShow)、淡入步進(showPopStep)、不透明度(popOpacity)。
  2. 動態建立樣式與容器:透過 document.write 注入 CSS 樣式與一個絕對定位的 <div id="dypopLayer"> 作為提示框。
  3. 事件處理函式:showPopupText 捕捉 onmouseover,讀取元素的 alt 或 title 屬性作為提示內容;showIt 計算定位並啟動淡入動畫;fadeOut/fadeIn 控制漸變顯示與隱藏。

完整程式碼

// 預設參數
var tPopWait = 50, tPopShow = 5000, showPopStep = 20, popOpacity = 99;
var sPop = null, curShow = null, tFadeOut = null, tFadeIn = null, tFadeWaiting = null;

// 動態寫入樣式與容器
document.write("<style type='text/css' id='defaultPopStyle'>.cPopText { line-break: normal; word-break: break-all; background-color: #FEFCF3; color: #000; border: 1px #E7B68C solid; padding: 2px 4px; font-size: 12px; width: 96px; filter: Alpha(Opacity=0) }</style>");
document.write("<div id='dypopLayer' style='position:absolute; z-index:1000;' class='cPopText'></div>");

function showPopupText() {
  var o = event.srcElement;
  var MouseX = event.x, MouseY = event.y;
  if (o.alt != null && o.alt != "") { o.dypop = o.alt; o.alt = ""; }
  if (o.title != null && o.title != "") { o.dypop = o.title; o.title = ""; }
  if (o.dypop != sPop) {
    sPop = o.dypop;
    clearTimeout(curShow); clearTimeout(tFadeOut); clearTimeout(tFadeIn); clearTimeout(tFadeWaiting);
    if (sPop == null || sPop == "") {
      dypopLayer.innerHTML = ""; dypopLayer.style.filter = "Alpha()"; dypopLayer.filters.Alpha.opacity = 0;
    } else {
      var popStyle = o.dyclass ? o.dyclass : "cPopText";
      curShow = setTimeout("showIt()", tPopWait);
    }
  }
}

function showIt() {
  dypopLayer.className = popStyle;
  dypopLayer.innerHTML = sPop;
  var popWidth = dypopLayer.clientWidth, popHeight = dypopLayer.clientHeight;
  var popLeftAdjust = (MouseX + 12 + popWidth > document.body.clientWidth) ? -popWidth - 24 : 0;
  var popTopAdjust = (MouseY + 12 + popHeight > document.body.clientHeight) ? -popHeight - 24 : 0;
  dypopLayer.style.left = MouseX + 12 + document.body.scrollLeft + popLeftAdjust;
  dypopLayer.style.top = MouseY + 12 + document.body.scrollTop + popTopAdjust;
  dypopLayer.style.filter = "Alpha(Opacity=0)";
  fadeOut();
}

function fadeOut() {
  if (dypopLayer.filters.Alpha.opacity < popOpacity) {
    dypopLayer.filters.Alpha.opacity += showPopStep;
    tFadeOut = setTimeout("fadeOut()", 1);
  } else {
    dypopLayer.filters.Alpha.opacity = popOpacity;
    tFadeWaiting = setTimeout("fadeIn()", tPopShow);
  }
}

function fadeIn() {
  if (dypopLayer.filters.Alpha.opacity > 0) {
    dypopLayer.filters.Alpha.opacity -= 1;
    tFadeIn = setTimeout("fadeIn()", 1);
  }
}

document.onmouseover = showPopupText;

使用範例

<a href="image1.jpg" target="_blank" title="<img src='image1.jpg' />">大圖</a>
<a href="image2.jpg" target="_blank" title="<img src='image2.jpg' width='96' height='70' />">小圖</a>

注意事項

  • 此範例使用 IE 專屬的 event.srcElement、filter: Alpha 等屬性,現代瀏覽器建議改用 addEventListener 與 CSS opacity 重構。
  • 提示內容支援 HTML 標籤,可直接在 title 中嵌入 <img>。
copyright © 萬盛學電腦網 all rights reserved