使用 JavaScript 在文本鼠标悬停时显示不同的弹出图像

发布于 2025-01-08 10:28:43 字数 2559 浏览 3 评论 0原文

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

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

我的问题:

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

  • 我应该使用这样的东西吗:
    第一个项目名称
    然后使用这个:
    <脚本类型=“text/javascript”> var pop = document.getElementById('popup');

  • 或者我是否可以使用(表/td)id名称和javascript数组来将正确的图像调用到中各自的名称中的

  • < p>或者我无法想到/知道的任何其他方式?

这是我到目前为止的表的 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 函数,该窗口将在鼠标移开时消失。

I am building a page with a restaurant menu that will consist of a few different html tables that will store the item name in the first <td> in each <tr>. I would like a JavaScript function to run when the user hovers the mouse over the item name (the first <td> in each <tr>) that will display an image popup in a box that corresponds to the particular item name.

All together there will be roughly 40 different item names that need to have this mouse-over effect to display a quick pop up of the image that relates to the item name and then fade back out when the hover effect is no longer active. Some item names may not have an image available though.

My question:

I am unsure what the best possible solution is to perform this or how to go about performing this. I have seen a few different techniques through Google that allow a image pop up when "mousing-over" a smaller image or a link, but would the same be possible with "mousing-over" text in a <td>?

  • Should I just use something like this:
    <td onmouseover="popup('<img src='/image/location/image.jpg>'')" onmouseout="popout()">1st Item Name</td>
    Then use this:
    <script type="text/javascript"> var pop = document.getElementById('popup'); </script

  • Or is it possible I could use (table / td) id names with a javascript array to call the correct images into their respective names in the <td>'s

  • Or any other way that I am unable to think of / know about?

Here is the html I have so far for the table(s):

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

Please explain which method you would use or you know of to perform this task. Also any JavaScript functions that are available to perform this or to display the image in a small popup window that will then fade away on mouseout.

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

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

发布评论

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

评论(1

沩ん囻菔务 2025-01-15 10:28:43

我最近用于此类事情的方法是将数据属性附加到触发事件的元素。所以在这种情况下你的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.
}

The way I have been using recently for these sort of things is to append a data- attibute to the element that is firing the event. So in this case your TD. Here is a link to the HTML 5 standard which describes the use of data attributes

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

You could have something like this in your td

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

Then in your javascript you get the value of the attribute like this:

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

I strongly recommend you learn a bit of jQuery to link this all together is it will make your life infinitely easier. Otherwise you can continue on in plain javascript.

in jQuery (includes fading box easily)

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

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