在SVG中如何显示鼠标提示框

本文介绍了一个SVG图形中的提示框如何正确地响应元素的平移和缩放操作,通过JavaScript实现动态调整位置及大小,确保提示框始终准确显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SVG代码如下:

<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"

  "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">

<svg width="100%" height="100%" onload ="getSVGDoc(evt)"  onzoom="ZoomControl()">

  <script xlink:href="tool_tip.js" type="text/javascript"/>

  <g id="testG" onmouseover = "ShowTooltip(evt, 'Welcome To Here!')" onmouseout = "HideTooltip(evt)">

    <rect x="100" y="100" width="100" height="50" style="fill:rgb(120,255,255);"/>

  </g>

  <g id="tooltip" style="pointer-events: none">

    <rect id="ttr" x="0" y="0" rx="5" ry="5" width="100" height="16"

      style="visibility: hidden"/>

    <text id="ttt" x="0" y="0" style="visibility: hidden">dyn. Text</text>

  </g>

</svg>

SVG中使用的脚步文件tool_tip.js如下:

var svgdoc,svgroot,ttrelem,tttelem,tt;


function getSVGDoc(load_evt)
{
  svgdoc = load_evt.target.ownerDocument;
  svgroot = svgdoc.documentElement;
  ttrelem = svgdoc.getElementById("ttr");
  tttelem = svgdoc.getElementById("ttt");
  // tt=svgdoc.getElementById("tooltip");
}


function ShowTooltip(e, txt)
{
  var posx, posy, curtrans, ctx, cty;
 
  posx = e.clientX;
  posy = e.clientY;
  curtrans = svgroot.currentTranslate;
  ctx = curtrans.x;
  cty = curtrans.y;
 
  tttelem.childNodes.item(0).data = txt;
  ttrelem.setAttribute("x", posx-ctx);
  ttrelem.setAttribute("y", posy-cty-20);
  tttelem.setAttribute("x", posx-ctx+5);
  tttelem.setAttribute("y", posy-cty-8);
  ttrelem.setAttribute("width", tttelem.getComputedTextLength() + 10);
  tttelem.setAttribute("style", "fill: #0000CC; font-size: 11px; visibility: visible");
  ttrelem.setAttribute("style", "fill: #FFFFCC; stroke: #000000; stroke-width: 0.5px; visibility: visible");
}


function HideTooltip()
{
  // tt.style.setProperty("visibility","hidden","");
  ttrelem.setAttribute("style", "visibility: hidden");
  tttelem.setAttribute("style", "visibility: hidden");
}


function ZoomControl()
{
  var curzoom;
  curzoom = svgroot.currentScale;
  svgdoc.getElementById("tooltip").setAttribute("transform","scale("+1/curzoom+")");
}

IE6中显示如下图:

在这里解决的由于平移和缩放后造成提示框相应平移和缩放的bug

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值