简体   繁体   English

单击链接/项目时如何制作弹出文本?

[英]How to make popup text when you click on the link/item?

I am sorry for extremely straight forward question for web developers... would really appreciate your help here! 对于网站开发人员提出的非常直截了当的问题,我们深表歉意……非常感谢您的帮助!

I have an image that I split into sections using <\\map> 我有一个图像,使用<\\map>将该图像分为几部分

Example: 例:

<map name="guessing_game">

<area alt="" title="" href="#1" shape="poly" coords="709,202,714,187,687,161,655,138,628,90,621,59,630,35,640,19,661,12,686,12,705,17,722,31,729,36,751,22,774,18,801,26,817,33,832,65,821,109" />


 <area alt="" title="" href="#2" shape="poly" coords="275,363,280,350,240,317,202,268,189,227,200,193,219,178,247,178,264,175,285,193,295,204,313,189,342,183,369,186,392,202,400,231,395,267,363,309" />

<area alt="" title="" href="#3" shape="poly" coords="554,364,557,351,520,318,473,260,468,227,471,203,481,187,506,178,521,174,548,180,562,189,569,203,579,201,594,186,626,182,651,186,668,207,676,235,668,263,646,299" />
</map>

I would like to make a popup effect the same as in this example 我想使弹出效果与此示例相同

So if someone will click on the link/aria they will get a different dialog.... with different position of the popup bubble. 因此,如果有人单击链接/咏叹调,他们将获得一个不同的对话框...,弹出气泡的位置不同。

Unfortunately I ran into the issue that if I try to use multiple text variations.... using the example above it will always return the same text no matter what link/aria I will click on. 不幸的是,我遇到了一个问题,如果我尝试使用多个文本变体...。使用上面的示例,无论我单击哪个链接/ aria,它都将始终返回相同的文本。

 <style> /* Popup container - can be anything you want */ .popup { position: relative; display: inline-block; cursor: pointer; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } /* The actual popup */ .popup .popuptext1 { visibility: hidden; width: 160px; background-color: #555; color: #fff; text-align: center; border-radius: 6px; padding: 8px 0; position: absolute; z-index: 1; bottom: 125%; left: 50%; margin-left: -810px; margin-bottom: 320px; } .popup .popuptext2 { visibility: hidden; width: 160px; background-color: #555; color: #fff; text-align: center; border-radius: 6px; padding: 8px 0; position: absolute; z-index: 1; bottom: 125%; left: 50%; margin-left: -520px; margin-bottom: 300px; } /* Popup arrow */ .popup .popuptext::after { content: ""; position: absolute; top: 100%; left: 50%; margin-left: -5px; border-width: 5px; border-style: solid; border-color: #555 transparent transparent transparent; } /* Toggle this class - hide and show the popup */ .popup .show { visibility: visible; -webkit-animation: fadeIn 1s; animation: fadeIn 1s; } /* Add animation (fade in the popup) */ @-webkit-keyframes fadeIn { from {opacity: 0;} to {opacity: 1;} } @keyframes fadeIn { from {opacity: 0;} to {opacity:1 ;} } </style> <script> // When the user clicks on div, open the popup function myFunction() { var popup = document.getElementById("myPopup"); popup.classList.toggle("show"); } function myFunction() { var popup = document.getElementById("myPopup2"); popup.classList.toggle("show"); } </script> <img src="./image.jpg" alt="Picture" style="width:auto;max-width:100%" usemap="#guessing_game"> <map name="guessing_game"> <div class="popup" onclick="myFunction()"> <span class="popuptext1" id="myPopup"><strong>Header</strong><br> First variation</span> <area alt="Header" title="Header" shape="poly" coords="122,200,125,184,94,159,63,129,42,91,31,59,38,40,59,10,89,10,111,12,132,24,140,38,153,25,174,15,210,18,230,28,245,55,246,87,224,124,178,161" /> </div> <div class="popup" onclick="myFunction()"> <span class="popuptext2" id="myPopup2"><strong>Header 2</strong><br>Second text variation </span> <area alt="Header 2" title="Header 2" shape="poly" coords="417,200,419,183,389,160,336,97,327,61,339,26,356,14,380,11,404,13,418,20,432,34,450,28,477,17,508,19,535,41,536,81,525,112" /> </div> 

please help... I am extremely sorry for such a basic question... I am sure there is a huge and simple mistake in the CSS or javascript function 请帮助...对于这样的基本问题,我感到非常抱歉。我确定CSS或javascript函数中存在一个巨大而简单的错误

You should be passing in the context of this to your onclick handler 你应该在的情况下通过this对您的onclick处理程序

onclick="myFunction(this)

function myFunction(el) {
console.log(el);
    var popup = el;
    popup.classList.toggle("show");
}

Thank you so much unicorn2! 非常感谢unicorn2!

Now if I create separate function for different dialogs it will work just fine... 现在,如果我为不同的对话框创建单独的功能,它将可以正常工作...

Here is how I do it now... First I create different classes for different positions: 这是我现在的操作方法...首先,我为不同的职位创建不同的类:

 .popup .popuptext1 { visibility: hidden; width: 160px; background-color: #555; color: #fff; text-align: center; border-radius: 6px; padding: 8px 0; position: absolute; z-index: 1; bottom: 125%; left: 50%; margin-left: -810px; margin-bottom: 320px; } .popup .popuptext2 { visibility: hidden; width: 160px; background-color: #555; color: #fff; text-align: center; border-radius: 6px; padding: 8px 0; position: absolute; z-index: 1; bottom: 125%; left: 50%; margin-left: -520px; margin-bottom: 300px; } 

Then I create functions for different dialogs 然后我为不同的对话框创建函数

 function dialog1() { var popup = document.getElementById("dialog1"); popup.classList.toggle("show"); } function dialog2() { var popup = document.getElementById("dialog2"); popup.classList.toggle("show"); } 

And after that I apply it to the image 之后,我将其应用于图像

 <div class="popup" onclick="dialog1()"> <span class="popuptext1" id="dialog1"><strong>Header 1</strong><br> Text that is different from the second one!</span> <area alt="Header" title="Header" shape="poly" coords="122,200,125,184,94,159,63,129,42,91,31,59,38,40,59,10,89,10,111,12,132,24,140,38,153,25,174,15,210,18,230,28,245,55,246,87,224,124,178,161" /> </div> <div class="popup" onclick="dialog2()"> <span class="popuptext2" id="dialog2"><strong>Header 2</strong><br>Thext that is not the same as the first one</span> <area alt="Header2" title="Header2" shape="poly" coords="417,200,419,183,389,160,336,97,327,61,339,26,356,14,380,11,404,13,418,20,432,34,450,28,477,17,508,19,535,41,536,81,525,112" /> </div> 

I wish I knew some better slick way of doing it... than creating different functions for different dialogs... but I am too unexperienced yet. 我希望我知道一些更好的方式...比为不同的对话框创建不同的功能...但是我还没有经验。

Thank you <3 I hope this will help some newbies like me in future! 谢谢<3,我希望这将对将来像我这样的新手有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM