DIV CSS 佈局教程網

JS 滑鼠懸停圖片顯示跑馬燈式文字說明
編輯:關於JavaScript     

滑鼠移到圖片上,跟著游標出現一個跑馬燈滾動的提示框;移開就消失。這個效果結合了 marquee 標籤與絕對定位的浮動提示層。

一、程式碼

<html>
<head>
<title>js 實現滑鼠懸停圖片上時的滾動文字說明</title>
<body>
<script language="JavaScript">
<!-- Begin
function showtip2(current, e, text) {
  if (document.all && document.readyState == "complete") {
    // 用 marquee 產生跑馬燈
    document.all.tooltip2.innerHTML =
      '<marquee style="border:1px solid black">' + text + '</marquee>';
    // 定位在滑鼠座標右下方 10px
    document.all.tooltip2.style.pixelLeft =
      event.clientX + document.body.scrollLeft + 10;
    document.all.tooltip2.style.pixelTop =
      event.clientY + document.body.scrollTop + 10;
    document.all.tooltip2.style.visibility = "visible";
  }
  else if (document.layers) {          // 舊 Netscape 分支
    document.tooltip2.document.nstip.document.write('<b>' + text + '</b>');
    document.tooltip2.document.nstip.document.close();
    document.tooltip2.document.nstip.left = 0;
    currentscroll = setInterval("scrolltip()", 100);
    document.tooltip2.left = e.pageX + 10;
    document.tooltip2.top  = e.pageY + 10;
    document.tooltip2.visibility = "show";
  }
}

function hidetip2() {
  if (document.all)
    document.all.tooltip2.style.visibility = "hidden";
  else if (document.layers) {
    clearInterval(currentscroll);
    document.tooltip2.visibility = "hidden";
  }
}
//  End -->
</script>

<div id="tooltip2"
     style="position:absolute;visibility:hidden;
            clip:rect(0 150 50 0);width:150px;
            background-color:gold;z-index:10"></div>

<div align="CENTER" name="divTest"
     onmouseover="showtip2(this,event,'歡迎來到');"
     onmouseout="hidetip2();"
     style="cursor:hand">
  <table border="0" cellpadding="0" cellspacing="0"><tr><td>
    <img src="/images/banner.gif" border="0" style="cursor:pointer;" />
  </table>
</div>
</body>
</html>

二、幾個關鍵技巧

1. 座標要加上 scroll 偏移。event.clientX / clientY 是相對於視窗的座標,而 position:absolute 元素的定位是相對於文件。頁面捲動後兩者會不一致,所以必須加上 document.body.scrollLeft / scrollTop 才能對齊。這是最容易搞錯的地方。(若改用 position:fixed,則反而不該加 scroll 值。)

2. clip 用來裁切範圍。style 中的 clip:rect(0 150 50 0) 限制提示框只顯示左上 150×50 的區域,超出部分被裁掉,配合 marquee 就有「文字從框中滾過」的效果。

3. readyState 檢查。document.readyState == "complete" 確保在文件完全載入後才操作,避免拿到 null。

三、需要淘汰的部分

<marquee> 已從標準中移除。它曾是 IE 私有標籤,永遠不會被主流標準接納,且不支援 prefers-reduced-motion,對部分使用者可能造成不適。若真要跑馬燈,請用 CSS animation 並搭配 prefers-reduced-motion 停用。

document.layers 分支可以刪除。那是 Netscape 4 的 API,其中的 scrolltip() 函式甚至沒有定義。

style="cursor:hand" 應改為 cursor:pointer,前者是 IE 私有值。

四、現代寫法

如果只是想顯示提示文字,原生 title 屬性就夠了(雖然樣式無法客製且延遲較長)。需要客製時用 CSS:

<style>
.tip { position: relative; }
.tip::after {
  content: attr(data-tip);
  position: absolute; top: 100%; left: 0;
  background: gold; border: 1px solid #000;
  padding: 4px 8px; white-space: nowrap;
  opacity: 0; transition: opacity .2s;
  pointer-events: none;
}
.tip:hover::after, .tip:focus-visible::after { opacity: 1; }
</style>

<div class="tip" data-tip="歡迎來到" tabindex="0">
  <img src="/images/banner.gif" alt="橫幅">
</div>

這樣寫完全不需要 JS,也不會有定位追不上游標的問題。若內容較長需要換行,把 white-space: nowrap 改為 normal 並加上 max-width 即可。

最後提醒:不要把關鍵資訊只放在 hover 提示裡。觸控裝置沒有 hover,鍵盤使用者也需要 tabindex 才能聚焦;重要說明應該直接放在頁面上。

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