【问题标题】:Display a varying popup image on text mouseover with javascript使用 javascript 在文本鼠标悬停时显示不同的弹出图像
【发布时间】:2012-02-23 02:30:30
【问题描述】:

我正在构建一个包含餐厅菜单的页面,该页面将包含几个不同的 html 表格,这些表格将在每个 <tr> 的第一个 <td> 中存储项目名称。我希望当用户将鼠标悬停在项目名称(每个<tr> 中的第一个<td>)上时运行一个javascript 函数,该函数将在与特定项目名称对应的框中显示一个图像弹出窗口。

总共将有大约 40 个不同的项目名称需要具有这种鼠标悬停效果以显示与项目名称相关的图像的快速弹出窗口,然后在悬停效果不再有效时淡出.但是,某些项目名称可能没有可用的图像。

我的问题:

我不确定执行此操作的最佳解决方案是什么或如何执行此操作。我通过 Google 看到了一些不同的技术,它们允许在“鼠标悬停”较小的图像或链接时弹出图像,但是 <td> 中的“鼠标悬停”文本是否也可以这样?

  • 我应该只使用这样的东西吗:
    <td onmouseover="popup('<img src='/image/location/image.jpg>'')" onmouseout="popout()">1st Item Name</td>
    然后使用这个:
    <script type="text/javascript"> var pop = document.getElementById('popup'); </script

  • 或者是否有可能我可以使用带有 javascript 数组的 (table / td) id 名称来在 &lt;td&gt;'s 中将正确的图像调用到它们各自的名称中

  • 我无法想到/知道的任何其他方式?

这是我到目前为止的表格的 html

    <div id="first_food_table">
    <table border="0" bordercolor="" style="background-color:" width="750">
    <tr>
      <td>1st item name</td> <!--Image popup should be displayed when mouse-over text-->
      <td>blah</td>
      <td>blahblah</td>
    </tr>
    <tr>
      <td>2nd item name</td><!-- Different image popup here as well -->
      <td>blah</td>
      <td>blahblah</td>
    </tr>
    <tr>
      <td>3rd item name</td> <!-- Again a different image popup here as well-->
      <td>blah</td>
      <td>blahblah</td>
    </tr>
    <tr>
      <td>4th item</td> <!-- And so on and so forth -->
      <td>blah</td>
      <td>blahblah</td>
    </tr>
    </table>        
    </div> 
    <div id="second_food_table>
    <table>
    <tr>
      <td>1st item name</td>
      <td>Table Cell</td>
      <td>Table Cell</td>
    </tr>
    <tr>
      <td>2nd item name</td>
      <td>Table Cell</td>
      <td>Table Cell</td>
    </tr>
    <tr>
      <td>3rd item name</td>
      <td>Table Cell</td>
      <td>Table Cell</td>
    </tr>
    <tr>
      <td>4th item name</td>
      <td>Table Cell</td>
      <td>Table Cell</td>
    </tr>
    </table>
    </div>

请说明您将使用或知道执行此任务的方法。还有任何可用于执行此操作或在小弹出窗口中显示图像的 javascript 函数,然后在鼠标移出时消失。

一如既往,感谢您的帮助和洞察力!

【问题讨论】:

  • “我通过 Google 看到了一些不同的技术,当“鼠标悬停”较小的图像或链接时,这些技术允许弹出图像,但使用“mousing- over" text in a ?" - 是的,完全相同的代码应该适用于 td(或 div 或 span 或任何其他元素类型)。

标签: javascript html css javascript-events


【解决方案1】:

我最近对这类事情使用的方法是将数据属性附加到触发事件的元素。所以在这种情况下你的TD。这是描述数据属性使用的 HTML 5 标准的链接

http://www.w3.org/TR/html5/elements.html#embedding-custom-non-visible-data-with-the-data-attributes

你的 td 中可以有这样的东西

<td data-imgsrc="imgfoler/specificimg.jpg">The item name</td>

然后在您的 javascript 中,您将获得如下属性的值:

var imgsrc = element.getAttribute('data-imgsrc');

我强烈建议你学习一点 jQuery 来将这一切联系在一起,它会让你的生活变得无比轻松。否则,您可以继续使用纯 JavaScript。

在 jQuery 中(轻松包含褪色框)

HTML

<td data-imgsrc="imgfoler/specificimg.jpg">The item name</td>

jQuery

$(document).ready(function(){
    $('td[data-imgsrc]').mouseenter(function() {
        var imgsrc = $(this).attr('data-imgsrc');
        $('img#id_of_your_image_element').attr('src',imgsrc).show();
    });
    $('td[data-imgsrc]').mouseleave(function() {
        $('img#id_of_your_image_element').fadeOut(200);
    });
});

或者使用纯 javascript

HTML

// You need to add an onclick handler on every td
<td data-imgsrc="imgfoler/specificimg.jpg" onmouseover="swapimg(this);">
    The item name
</td>

JS

function swapimg(element) {
    var imgsrc = element.getAttribute('data-imgsrc');
    var imgelement = document.getElementById('id_of_your_image_element');
    imgelement.setAttribute('src',imgsrc);

    // No fading out here, if you want fading just use jQuery. Fading
    // in native JS is a pain in the behind.
}

【讨论】:

  • 感谢您的回复!我见过使用过的 JQuery,只是我自己从未深入使用过它。现在我的理解是,在加载其他所有内容(图像、横幅等)之前,Jquery 无法开始加载。如果我在 TD 中使用data-imgsrc="imgfolder/image.jpg",那么整个页面中引用的所有图像都必须“加载” " 在鼠标悬停效果之前的背景中会起作用吗?再次感谢您的帮助,我将继续研究 Jquery。
  • 确实,如果您想要立即加载图像,则必须在后台加载它们,但这不是 jQuery 特定的,因为您也需要对普通 javascript 执行此操作。可以在加载所有内容之前使用 jQuery(只要加载了 jQuery),它只是 $(document).ready() 函数,它是一个安全网,可以防止您操作尚不存在的 DOM 元素。这是一篇关于在文档就绪状态之前使用jQuery的好文章encosia.com/dont-let-jquerys-document-ready-slow-you-down
  • 就像我在回答中所说的那样,如果你想淡化使用 jQuery。让它适用于原生 javascript 中的所有浏览器是非常令人沮丧的。相信我,我已经试过了。当然 jQuery 不是你唯一的选择,有很多不同的框架可以为你做一些很酷的事情,比如褪色。 Prototype、MooTools 等,但我个人认为,jQuery 的学习曲线最小。
  • 是的,你肯定说服我继续使用 jquery 主要是因为它似乎可以为你节省这个项目或其他项目。在函数中引用'img#id_of_your_image_element' 是什么意思我应该设置&lt;td id=""&gt; 还是需要一个html div 设置才能正确显示图像。我尝试在'img#id_of_your_image_element' 内部使用'td',并且所有TD 在鼠标悬停时都会消失,并且没有显示图像,并且当mouseenter 不活动时它们不会返回。我明白为什么它不起作用,但不确定在那里使用什么。
  • img#id_of_of_your_image 是您要显示图像的实际位置的 ID。如果您希望它是一个弹出窗口,它可能是一个包含在绝对定位的 div 或类似内容中的图像元素。如果您添加 html 以显示您的图像显示在哪个元素中,我将附上我的答案以说明您将如何做到这一点。
猜你喜欢
  • 1970-01-01
  • 2012-07-25
  • 1970-01-01
  • 1970-01-01
  • 2010-12-11
  • 2017-07-19
  • 1970-01-01
  • 2012-12-16
相关资源
最近更新 更多