【问题标题】:Remove Javascript Tooltip Offset For Nicer Hover Grow Effect删除 Javascript 工具提示偏移以获得更好的悬停增长效果
【发布时间】:2010-12-03 21:36:46
【问题描述】:

我正在努力完成某件事,我将不胜感激所提供的任何和所有帮助!

我有一个工具提示代码,在图像悬停时,图像会出现在工具提示中,并通过 JS 偏移位置。我不想在工具提示中偏移悬停图像的位置,而是希望工具提示中的图像显示为没有偏移,EXACTLY like the hover effect you see in this example。我需要工具提示的位置根本不偏移,正好在图像的顶部。我不知道如何实现这一点,因为示例代码使用 css,我需要它在 JS 中使用此工具提示。

这里是工具提示代码:

too = tooo = bi = an = null; document.onmousemove = document.onmouseover = toolt;
function toolt(a) {
if(bi) {
    y = bi.offsetTop+bi.height-20;
    x = bi.offsetLeft+bi.width-12;
} else {
    y = (document.all) ? window.event.y + document.body.scrollTop  : a.pageY;
    x = (document.all) ? window.event.x + document.body.scrollLeft : a.pageX;
}
a = window.innerHeight ? window.innerHeight : document.body.offsetHeight;
b = window.innerWidth ? window.innerWidth : document.body.offsetWidth;
if (too != null) {
    if(too.offsetWidth+x+35-document.body.scrollLeft > b) x=document.body.scrollLeft+b-too.offsetWidth-35; too.style.left = (x+12)+"px";
    if(too.offsetHeight+y+40-document.body.scrollTop > a) y=y-too.offsetHeight-30; too.style.top = (y+20)+"px"; }
}
function htoo(i) { if(too)  too.style.display  = "none"; if(bi) bi.style.display = an ? "block" : "none"; tooo = false;}
function stoo(i) { if(tooo) tooo.style.display = "none"; if(bi) bi.style.display = an ? "block" : "none"; too = tooo = document.getElementById(i); too.style.display = "block"; if(bi = document.getElementById(i+"b")) bi.style.display="block";
}

我怎样才能用给定的代码完成这个,有什么想法吗?

【问题讨论】:

  • 你链接的页面中的JS代码你看了吗?
  • 是的,我有,但我只需要删除提供的工具提示代码中的偏移量。工具提示中的 JS 对我来说太复杂了。我只想编辑上面提供的代码以显示工具提示在图像上的位置而不是偏移它。这有意义吗?

标签: javascript hover tooltip position offset


【解决方案1】:

您当然可以将其调整得更好,但这里有一个概念验证
注意:您的图片样式应为position: relative p>

用法(Demo

FancyZoom("container");

构造函数

function FancyZoom(container, options) {
  container = container || document;
  if (typeof container == "string") {
    container = document.getElementById(container);
  }

  function isNodeName(el, name) {
    return el.nodeName.toLowerCase() === name;
  }    

  function getTarget(e) {
    e = e || window.event;
    return e.target || e.srcElement;
  }

  // image {mouseover} event handler (delegated)
  container.onmouseover = function(e) {
    var el = getTarget(e);
    if (isNodeName(el, "img")) {
      zoomIn(el);
      // image {mouseout} event handler
      el.onmouseout = function(e) {
        zoomOut(el);
        el.onmouseout = null;
      };  
    }
  };
}

动画 (emile.js)

function zoomIn(thumb) {
    thumb.style.zIndex = 10;
    emile( thumb,
      "width:"  + 174 + "px;" +
      "height:" + 174 + "px;" +
      "margin-left:" + -37 + "px;" +
      "margin-top:"  + -37 + "px;",
      { duration: 200 }
    );
}

function zoomOut(thumb) {
    thumb.style.zIndex = 1;
    emile( thumb,
      "width:"  + 100 + "px;" +
      "height:" + 100 + "px;" +
      "margin-left:" + 0 + "px;" +
      "margin-top:"  + 0 + "px;",
      { duration: 100 }
    );

}

测试平台:IE7+、FF2+、Safari 4+、Chrome、Opera

【讨论】:

  • 您也可以通过简单的公式计算边距:mleft = -(large_w-small_w)/2
  • 哇,galambalazs,真快。但这不使用工具提示代码。工具提示代码变量名称是可靠的,因为很多东西都严重依赖它,x,y 变量也是如此。我只是不知道如何编辑提供的工具提示代码以完全删除偏移量,因此工具提示的位置不会偏移并跟随鼠标,而是在大悬停效果的同一位置。从技术上讲,它正在移除偏移量,因此工具提示不会以预定义的像素数量跟随鼠标,而是会出现在与悬停图像相同的位置。
  • 你不能指望任何人知道你的变量是什么意思,比如abitootoootoolt...
  • 我还告诉过您删除整页偏移所需的内容。制作图像position:relative,然后在mouseover 上调整它们的尺寸和边距。边距公式在我的第一条评论中。不要偷懒。我已经为你做了辛苦的工作。 :)
  • 问题是,不仅仅是图像,在图像悬停时,工具提示会显示图像和它下面的一些 html 文本,类似于新的谷歌图像。我正在使用 walter zorn 的 wz_tooltip.js,在这里找到:web.archive.org/web/20080103025045/www.walterzorn.com/tooltip/… 但上面的 .js 会覆盖这些值。我联系了作者,会在一周内回复,可能会在一周内发布整个项目的样本。感谢 galambalazs 到目前为止所做的一切!
猜你喜欢
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多