删除 Javascript 工具提示偏移以获得更好的悬停增长效果

发布于 2024-10-05 18:39:20 字数 1523 浏览 5 评论 0原文

我正在努力完成一些事情,并将感谢所提供的任何和所有帮助!

我有一个工具提示代码,当鼠标悬停在图像上时,图像会通过 JS 出现在工具提示中并具有偏移位置。我希望工具提示中的图像没有偏移地显示,而不是偏移工具提示中悬停图像的位置,与您在此示例中看到的悬停效果完全相同。我需要工具提示的位置根本不偏移,只需正好位于图像顶部即可。我不确定如何实现这一点,因为示例代码使用 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";
}

如何使用给定的代码来完成此操作,有什么想法吗?

I'm trying to accomplish something and will appreciate any and all help provided!

I have a tooltip code that on hover of an image, the image appears in the tooltip with an offset position via JS. Instead of offsetting the position of the hovered image in the tooltip, I want the image in the tooltip be displayed with no offsets, EXACTLY like the hover effect you see in this example. I need the position of the tooltip to not be offset at all, just be exactly on top of the image. I'm not sure how to accomplish this since the example code uses css and I need it working with this tooltip in JS.

Here's the tooltip code:

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";
}

How can I accomplish this with the given code, any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦醒灬来后我 2024-10-12 18:39:20

您当然可以将其调整得更好,但这是一个概念证明
注意:您的图像样式应为位置:相对

用法 (演示)

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

You sure can tweak it to be better, but here is a proof of concept.
Note: Your images' style should be position: relative

Usage (Demo)

FancyZoom("container");


Constructor

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;
      };  
    }
  };
}

Animation (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 }
    );

}

Tested On: IE7+, FF2+, Safari 4+, Chrome, Opera

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文