如何修复JavaScript中按Enter和点击之间的不同行为?

huangapple go评论65阅读模式
英文:

how to fix different behavior between pressing enter and click in javascript?

问题

我正在处理一个带有图像地图的页面。JavaScript 中有一个名为 openBubble 的函数,用于打开一个弹出框。当用户点击图像地图时,它会正确地打开弹出框(使用脚本中提供的 X 和 Y 坐标)。但是,当我切换到图像地图并按回车键时,弹出框会在页面的左上角打开。我希望弹出框在用户点击图像地图时以相同的方式打开。请查看下面的代码:

<div id="myWrapper">
    <img id="myImage" src="http://tinypic.com/images/goodbye.jpg" usemap="#myMap" alt="" class="map" />
    <map name="myMap" id="myMap">
        <area id="left-eye" coords="118,36,148,80" shape="rect" href="javascript:void(0);">
        <area id="mouth" coords="121,84,198,118" shape="rect">
    </map>
    <div id="myBubble">
        <div id="myBubbleContent"></div>
        <div id="myBubbleCloseButton">✕</div>
    </div>
</div>

CSS 部分:

#myWrapper {
    position: relative;
    font-family: Arial, Helvetica, sans-serif;
}
#myImage {
    display: block;
}
#myBubble {
    background: #fff;
    border: 1px solid #aaa;
    padding: .5rem 1rem;
    display: none;
    top: 1.5rem;
    left: 1.5rem;
    width: 40%;
}
#myBubble.shown {
    position: fixed;
    display: block;
    cursor: pointer;
    font-size: 0.8rem;
    margin-top: 0.2rem;
    filter: drop-shadow(0 2px 4px rgba(0,0,0,0.5));
    width: 20%;
}
#myBubble.shown:before {
    content: "";
    position: absolute;
    top: 20px;
    left: -30px;
    z-index: 1;
    border: solid 15px transparent;
    border-right-color: #FFF;
}
#myBubble img {
    display: block;
    width: 100%;
}
#myBubbleCloseButton {
    position: absolute;
    top: 0;
    right: 0;
    padding: .5rem;
    line-height: 1;
    cursor: pointer;
    color: #1A8782;
}
#myBubbleCloseButton:hover {
    color: #1A8782;
}

请注意,我只提供了 HTML 和 CSS 部分的翻译,JavaScript 部分没有进行翻译。如果需要 JavaScript 部分的翻译,请提供相应的文本。

英文:

I am working on a page with image maps. The javascript has a function openBubble that opens a popup box. When user clicks on the image map it opens the popup correctly (using the coordinates X and Y provided in the script). But when I tab to the image map and hit enter, the popup box opens on left side top of the page. I want the popup box to open the same way it opens when user clicks on the image map. Please see my code below -

https://jsfiddle.net/xkgshcyd/

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

 $(function() {
    $(&#39;.map&#39;).maphilight();
  });
var myData = {
    &quot;left-eye&quot;: {
        &quot;title&quot;: &quot;&quot;,
        &quot;image&quot;:&quot;&quot;,
        &quot;description&quot;: &quot;Lorem ipsum A dolor sin amet. .&quot;
    },
    &quot;mouth&quot;: {
        &quot;title&quot;: &quot;&quot;,
       
        &quot;description&quot;: &quot;Lorem ipsum B dolor sin amet. Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.vLorem ipsum B dolor sin amet.vLorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.&lt;/p&gt;Lorem ipsum B dolor sin amet.&quot;
    },
   
};

var areas         = document.getElementsByTagName(&#39;area&#39;),
    bubble        = document.getElementById(&#39;myBubble&#39;),
    bubbleContent = document.getElementById(&#39;myBubbleContent&#39;),
    bubbleClose   = document.getElementById(&#39;myBubbleCloseButton&#39;);

// On click of an area, open popup
for(var i=0, l=areas.length; i&lt;l; i++) {
  areas[i].addEventListener(&#39;click&#39;, openBubble, false);
}

// On click of close button, close popup
bubbleClose.addEventListener(&#39;click&#39;, closeBubble, false);

function openBubble() {
  var content = myData[this.id];
  bubbleContent.innerHTML = &#39;&lt;h3&gt;&#39; + content.title + &#39;&lt;/h3&gt;&#39;
                          + &#39;&lt;img src=&quot;&#39; + content.image + &#39;&quot; alt=&quot;&quot; /&gt;&#39;
                          + &#39;&lt;p&gt;&#39; + content.description + &#39;&lt;/p&gt;&#39;;
  bubble.style.top = (event.clientY - 20) + &quot;px&quot;;                        
  bubble.style.left = (event.clientX - 200) + &quot;px&quot;;                        
  bubble.className = &#39;shown&#39;;
}





function closeBubble() {
  bubble.className = &#39;&#39;;
}

<!-- language: lang-css -->

#myWrapper{ position: relative; font-family: Arial, Helvetica, sans-serif; }
#myImage{ display: block; }
#myBubble{  background: #fff; border: 1px solid #aaa; padding: .5rem 1rem; display: none; top: 1.5rem; left: 1.5rem; width: 40%;  }
#myBubble.shown{position: fixed; display: block;cursor: pointer;font-size: 0.8rem;
    margin-top: 0.2rem; filter: drop-shadow(0 2px 4px rgba(0,0,0,0.5)); width:20%; }
    #myBubble.shown:before{
    content: &quot;&quot;;
    position: absolute;
    top: 20px;
    left:-30px;
    z-index: 1;
    border: solid 15px transparent;
    border-right-color: #FFF;
    
}
#myBubble img{ display: block; width: 100%;
}
#myBubbleCloseButton{ position: absolute; top: 0; right: 0; padding: .5rem;  line-height: 1; cursor: pointer; color: #1A8782; }
#myBubbleCloseButton:hover{  color: #1A8782;  }

<!-- language: lang-html -->

&lt;script src=&quot;https://cdn.rawgit.com/davidjbradshaw/image-map-resizer/master/js/imageMapResizer.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;https://cdnjs.cloudflare.com/ajax/libs/maphilight/1.4.0/jquery.maphilight.min.js&quot;&gt;&lt;/script&gt;

&lt;div id=&quot;myWrapper&quot;&gt;

   &lt;img id=&quot;myImage&quot; src=&quot;http://tinypic.com/images/goodbye.jpg&quot; usemap=&quot;#myMap&quot; alt=&quot;&quot; class=&quot;map&quot; /&gt;


 &lt;map name=&quot;myMap&quot; id=&quot;myMap&quot;&gt;
  &lt;area id=&quot;left-eye&quot;  coords=&quot;118,36,148,80&quot; shape=&quot;rect&quot; href=&quot;javascript:void(0);&quot;&gt;
  &lt;area id=&quot;mouth&quot;  coords=&quot;121,84,198,118&quot; shape=&quot;rect&quot;&gt;
&lt;/map&gt;

   &lt;div id=&quot;myBubble&quot;&gt;
      &lt;div id=&quot;myBubbleContent&quot;&gt;&lt;/div&gt;
      &lt;div id=&quot;myBubbleCloseButton&quot;&gt;✕&lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

以下是翻译好的部分:

一个在概念上比较简单的方法是将地图的坐标复制到 myData 对象中,然后使用它们代替 clientXclientY。这样做的缺点是你需要考虑父图像的位置,因此你需要存储图像的标识以便能够检索坐标。然而,更大的缺点是代码重复可能会导致同步问题,即如果你在地图中更改坐标或父图像的标识,那么你还需要在 myData 对象中进行相应的更改。

优点是点击事件的处理现在与 tab + enter 事件相同。

相关代码片段:

数据对象

var myData = {
    "left-eye": {
        "title": "",
        "image": "",
        "topleft": [118,36],
        "parentimg": "myImage",
        "description": "Lorem ipsum A dolor sin amet. .",
    },
    "mouth": {
        "title": "",
        "topleft": [121,84],
        "parentimg": "myImage",       
        "description": "Lorem ipsum B dolor sin amet. Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.vLorem ipsum B dolor sin amet.vLorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.</p>Lorem ipsum B dolor sin amet."
    },
   
};

点击/tab+enter 处理程序

function openBubble() {
  var content = myData[this.id];
  bubbleContent.innerHTML = '<h3>' + content.title + '</h3>'
                          + '<img src="' + content.image + '" alt="" />'
                          + '<p>' + content.description + '</p>';
  parent_box = document.getElementById(content.parentimg).getBoundingClientRect();
  bubble.style.top = (content.topleft[1] + parent_box.top - 20) + "px";                        
  bubble.style.left = (content.topleft[0] + parent_box.left + 20) + "px";                        
  bubble.className = 'shown';
}

JSFiddle: https://jsfiddle.net/1u6n5rvw/

英文:

A conceptually simple way is to copy the coordinates of the map into the myData object and use those instead of clientX and clientY. This has the disadvantage that you need to account for the position of the parent image, therefore you need to store the id of the image to be able to retrieve the coordinates. A bigger disadvantage however is code duplication which may lead to synchronization problems, i.e., if you change the coordinates in the map, or the id of the parent image, then you also need to change them in the myData object.

The advantage is that the handling for a click event is now the same as for a tab+enter event.

Relevant code fragments:

Data object

var myData = {
    &quot;left-eye&quot;: {
        &quot;title&quot;: &quot;&quot;,
        &quot;image&quot;:&quot;&quot;,
        &quot;topleft&quot;: [118,36],
        &quot;parentimg&quot;: &quot;myImage&quot;,
        &quot;description&quot;: &quot;Lorem ipsum A dolor sin amet. .&quot;,
    },
    &quot;mouth&quot;: {
        &quot;title&quot;: &quot;&quot;,
        &quot;topleft&quot;: [121,84],
        &quot;parentimg&quot;: &quot;myImage&quot;,       
        &quot;description&quot;: &quot;Lorem ipsum B dolor sin amet. Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.vLorem ipsum B dolor sin amet.vLorem ipsum B dolor sin amet.Lorem ipsum B dolor sin amet.&lt;/p&gt;Lorem ipsum B dolor sin amet.&quot;
    },
   
};

Click/tab+enter handler

function openBubble() {
  var content = myData[this.id];
  bubbleContent.innerHTML = &#39;&lt;h3&gt;&#39; + content.title + &#39;&lt;/h3&gt;&#39;
                          + &#39;&lt;img src=&quot;&#39; + content.image + &#39;&quot; alt=&quot;&quot; /&gt;&#39;
                          + &#39;&lt;p&gt;&#39; + content.description + &#39;&lt;/p&gt;&#39;;
  parent_box = document.getElementById(content.parentimg).getBoundingClientRect();
  bubble.style.top = (content.topleft[1] + parent_box.top - 20) + &quot;px&quot;;                        
  bubble.style.left = (content.topleft[0] + parent_box.left + 20) + &quot;px&quot;;                        
  bubble.className = &#39;shown&#39;;
}

JSFiddle: https://jsfiddle.net/1u6n5rvw/

huangapple
  • 本文由 发表于 2023年7月31日 21:25:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804090.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定