如何在鼠标移出标记时使用计时器自动关闭Leaflet弹出窗口。

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

How to autoclose a Leaflet popup with timer when mouse move out marker

问题

Here's the translated code portion:

可以为Leaflet中的标记设置弹出窗口的自动关闭定时器

var marker1 = L.marker([46.26734, 12.328876], { icon: myIcon4 }).on('mouseover', function (e) {
    var popup = L.popup().setLatLng(e.latlng).setContent('Luoghi, Cose, Strade<br>Diga del Vajont').openOn(mappa_percorsi);
}).on('click', function (e) {
    var popup = L.popup().setLatLng(e.latlng).setContent('<a class="image-popup-no-margins hover-title" href="https://atorinfriul.it/html/gpx/Luoghi_Cose_Strade__Diga_Vajont_NOT_MINIMIZED.php" Target="_blank" title="Diga del Vajont"> <img loading="lazy" src="https://atorinfriul.it/html/images/luoghi_cose_strade/Fumetto-diga-del-vajont.jpg" style="width:150px;" alt="Responsive image"></a> <div class="hover-image"><img loading="lazy" src="https://atorinfriul.it/html/images/luoghi_cose_strade/Fumetto-diga-del-vajont.jpg" alt="Responsive image"></div>').openOn(mappa_percorsi);
}).addTo(LuoghiCoseStrade);

在此示例中悬停在标记上会打开一个带有消息Luoghi, Cose, Strade - Diga del Vajont的弹出窗口单击标记后会显示一个带有图像的新弹出窗口并在图像上悬停时会打开一个放大的图像模态窗口并单击在新标签页上打开图像

问题是如果用户悬停在标记上打开弹出窗口),然后移动到地图的其余部分弹出窗口会一直保持打开直到悬停在新标记上或在地图上单击

这就是为什么我想添加一个定时器

我尝试了几种方法都没有效果

放置此代码后我可以关闭弹出窗口但在单击后失去了在辅助弹出窗口带有图像的弹出窗口中导航的可能性

marker1.on('mouseout', function (e) {
    mappa_percorsi.closePopup();
});

我尝试使用此代码但出现错误我的JavaScript和Leaflet知识有限无法解决)。

setTimeout(function () { popup.close(); }, 5000);

欢迎提出任何建议

以下是一个几乎工作的示例以更好地解释

请注意,这是您提供的代码的翻译部分,如果您需要其他方面的帮助,请随时提问。

英文:

It's possible to set an autoclose timer for a popup on a marker in leaflet.

var marker1 = L.marker([46.26734, 12.328876], {icon: myIcon4}).on('mouseover', function(e){var popup = L.popup().setLatLng(e.latlng).setContent('Luoghi, Cose, Strade<br>Diga del Vajont').openOn(mappa_percorsi);}).on('click', function(e){var popup = L.popup().setLatLng(e.latlng).setContent('<a class="image-popup-no-margins hover-title" href="https://atorinfriul.it/html/gpx/Luoghi_Cose_Strade__Diga_Vajont_NOT_MINIMIZED.php"     Target="_blank" title="Diga del Vajont">   <img loading="lazy"  src="https://atorinfriul.it/html/images/luoghi_cose_strade/Fumetto-diga-del-vajont.jpg" style="width:150px;" alt="Responsive image"></a> <div class="hover-image"><img loading="lazy"  src="https://atorinfriul.it/html/images/luoghi_cose_strade/Fumetto-diga-del-vajont.jpg" alt="Responsive image"></div>').openOn(mappa_percorsi);}).addTo(LuoghiCoseStrade);

In this example, overing marker, open a popup with this message "Luoghi, Cose, Strade - Diga del Vajont". <br>Clicking on marker, a new popup is show, with an image inside it, and overing image, open a modal of image zoomed up and clicking open image on a new tab.

Problem is, that if an user over the marker (opening popup) then move on rest of map, popup stay on until a new marker is overed or a click on the map is done.

That's why I want to add a timer.

I have tried severals solutions without benefits<br>
Putting this code, I obtain to close popup, but loose possibility, after clicking to navigate in secondary popup (that one with image).

marker1.on(&#39;mouseout&#39;, function (e) {mappa_percorsi.closePopup();});

I tryed to use this, but get an error (my js and leaflet knowledge is too limited to solve)

setTimeout(function() { popup.close();}, 5000);

Any suggestion is wellcome.

Thanks

Following is a almost working example to try to explain better
thanks

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

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

       var marker = L.marker([46.26734, 12.328876], {icon: myIcon2}).on(&#39;mouseover&#39;, function(e){var popup = L.popup().setLatLng(e.latlng).setContent(&#39;Luoghi, Cose, Strade&lt;br&gt;Diga del Vajont&#39;).openOn(mymap);}).on(&#39;click&#39;, function(e){var popup = L.popup().setLatLng(e.latlng).setContent(&#39;&lt;a class=&quot;image-popup-no-margins hover-title&quot; href=&quot;https://atorinfriul.it/html/gpx/Luoghi_Cose_Strade__Diga_Vajont_NOT_MINIMIZED.php&quot;     Target=&quot;_blank&quot; title=&quot;Diga del Vajont&quot;&gt;   &lt;img loading=&quot;lazy&quot;  src=&quot;https://atorinfriul.it/html/images/luoghi_cose_strade/Fumetto-diga-del-vajont.jpg&quot; style=&quot;width:150px;&quot; alt=&quot;Responsive image&quot;&gt;&lt;/a&gt; &lt;div class=&quot;hover-image&quot;&gt;&lt;img loading=&quot;lazy&quot;  src=&quot;https://atorinfriul.it/html/images/luoghi_cose_strade/Fumetto-diga-del-vajont.jpg&quot; alt=&quot;Responsive image&quot;&gt;&lt;/div&gt;&#39;).openOn(mymap);}).addTo(mymap);

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

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;it&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;ie=edge&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://unpkg.com/leaflet@1.8.0/dist/leaflet.css&quot; integrity=&quot;sha512-hoalWLoI8r4UszCkZ5kL8vayOGVae1oxXe/2A4AO6J9+580uKHDO3JdHb7NzwwzK5xr/Fs0W40kiNHxM9vyTtQ==&quot; crossorigin=&quot;&quot; /&gt;
&lt;script src=&quot;https://unpkg.com/leaflet@1.8.0/dist/leaflet.js&quot; integrity=&quot;sha512-BB3hKbKWOc9Ez/TAwyWxNXeoV9c1v6FIeYiBieIWkpLjauysF18NzgR1MBNBXf8/KABdlkX68nAhlwcDFLGPCQ==&quot; crossorigin=&quot;&quot;&gt;&lt;/script&gt;
&lt;style &gt;
.mappa_global{max-width: 1350px;
width: 100%;
min-height: 85vh;
height: 100%;
border-radius: 25px;
padding-left: 2%;
padding-right: 2%;
margin: 0px auto;
position: relative;
z-index: 1;}
&lt;/style&gt;
&lt;/head&gt;
&lt;!-- indispensabile onload per caricare lo script dei video nella hero --&gt;
&lt;body  class=&quot;my_width&quot;&gt;
&lt;div&gt;
&lt;div class=&quot;p25_bis&quot;&gt;
&lt;div id=&quot;map_canvas&quot; class=&quot;mappa_global&quot; class=&quot;lazyload&quot; &gt;&lt;/div&gt;
&lt;/div&gt;
&lt;script&gt;
var mbAttr = &#39;Map data &amp;copy; &lt;a href=&quot;https://www.openstreetmap.org/copyright&quot;&gt;OpenStreetMap&lt;/a&gt; contributors, Imagery &#169; &lt;a href=&quot;https://www.mapbox.com/&quot;&gt;Mapbox&lt;/a&gt;&#39;;
var mbUrl = &#39;https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw&#39;;
var Streets = L.tileLayer(mbUrl, {id: &#39;mapbox/streets-v11&#39;, tileSize: 512, zoomOffset: -1, attribution: mbAttr});
var Osm = L.tileLayer(&#39;https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png&#39;, {
maxZoom: 19,
attribution: &#39;&amp;copy; &lt;a href=&quot;http://www.openstreetmap.org/copyright&quot;&gt;OpenStreetMap&lt;/a&gt;&#39;
});
var Esri_NatGeoWorldMap = L.tileLayer(&#39;https://server.arcgisonline.com/ArcGIS/rest/services/NatGeo_World_Map/MapServer/tile/{z}/{y}/{x}&#39;, {
attribution: &#39;Tiles &amp;copy; Esri &amp;mdash; National Geographic, Esri, DeLorme, NAVTEQ, UNEP-WCMC, USGS, NASA, ESA, METI, NRCAN, GEBCO, NOAA, iPC&#39;,
maxZoom: 16
});
var OpenTopoMap = L.tileLayer(&#39;https://{s}.tile.opentopomap.org/{z}/{x}/{y}.png&#39;, {
maxZoom: 17,
attribution: &#39;Map data: &amp;copy; &lt;a href=&quot;https://www.openstreetmap.org/copyright&quot;&gt;OpenStreetMap&lt;/a&gt; contributors, &lt;a href=&quot;http://viewfinderpanoramas.org&quot;&gt;SRTM&lt;/a&gt; | Map style: &amp;copy; &lt;a href=&quot;https://opentopomap.org&quot;&gt;OpenTopoMap&lt;/a&gt; (&lt;a href=&quot;https://creativecommons.org/licenses/by-sa/3.0/&quot;&gt;CC-BY-SA&lt;/a&gt;)&#39;
});
var Stamen_Toner = L.tileLayer(&#39;https://stamen-tiles-{s}.a.ssl.fastly.net/toner/{z}/{x}/{y}{r}.{ext}&#39;, {
attribution: &#39;Map tiles by &lt;a href=&quot;http://stamen.com&quot;&gt;Stamen Design&lt;/a&gt;, &lt;a href=&quot;http://creativecommons.org/licenses/by/3.0&quot;&gt;CC BY 3.0&lt;/a&gt; &amp;mdash; Map data &amp;copy; &lt;a href=&quot;https://www.openstreetmap.org/copyright&quot;&gt;OpenStreetMap&lt;/a&gt; contributors&#39;,
subdomains: &#39;abcd&#39;,
minZoom: 0,
maxZoom: 20,
ext: &#39;png&#39;
});
var mymap = L.map(&#39;map_canvas&#39;, {
center: [46.26734, 12.328876],
zoom: 9,
layers: [Stamen_Toner]
});
var baseLayers = {
&#39;Toner&#39;: Stamen_Toner,
&#39;OpenTopoMap&#39;: OpenTopoMap,
&#39;OpenStreetMap&#39;: Osm,
&#39;Streets&#39;: Streets,
&#39;National Geographic&#39;: Esri_NatGeoWorldMap
};
var layerControl = L.control.layers(baseLayers).addTo(mymap);
var myIcon2 = L.icon({
iconUrl: &#39;https://unpkg.com/leaflet@1.8.0/dist/images/marker-icon.png&#39;,
iconSize: [40, 40], // size of the icon
});
// arrivo Percorso
&lt;/script&gt;
&lt;/div&gt;
&lt;!-- jQuery 1.7.2+ or Zepto.js 1.0+ --&gt;
&lt;script src=&quot;https://atorinfriul.netsons.org/html/js/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;!-- Magnific Popup core JS file --&gt;
&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

答案1

得分: 0

你差一点就做到了。如果我理解正确,您想在离开图层时仅关闭鼠标悬停的弹出窗口。您可以定义一个变量来检查哪个弹出窗口已打开,然后编写一个在鼠标离开时关闭它的函数。

var whichPopup = 0;
var marker = L.marker([46.26734, 12.328876], {icon: myIcon2}).on('mouseover', function(e){
    popup = L.popup().setLatLng(e.latlng).setContent('Luoghi, Cose, Strade&lt;br&gt;Diga del Vajont').openOn(mymap);
    whichPopup = 1;
}).on('click', function(e){
    var popup2 = L.popup().setLatLng(e.latlng).setContent('&lt;a class="image-popup-no-margins hover-title" href="https://atorinfriul.it/html/gpx/Luoghi_Cose_Strade__Diga_Vajont_NOT_MINIMIZED.php" Target="_blank" title="Diga del Vajont"&gt; &lt;img loading="lazy" src="https://atorinfriul.it/html/images/luoghi_cose_strade/Fumetto-diga-del-vajont.jpg" style="width:150px;" alt="Responsive image"&gt;&lt;/a&gt; &lt;div class="hover-image"&gt;&lt;img loading="lazy" src="https://atorinfriul.it/html/images/luoghi_cose_strade/Fumetto-diga-del-vajont.jpg" alt="Responsive image"&gt;&lt;/div&gt;').openOn(mymap);
    whichPopup = 0;
}).addTo(mymap);

marker.on('mouseout', function (e) {
    if (whichPopup == 1){
        setTimeout(function() { mymap.closePopup();}, 5000);
    }
});
英文:

You almost did it. If I get you right you want to close only the mouseover popup when leaving the layer. you can define a variable to check which popup is opened and then write a function to close it on mouse leave.

var whichPopup = 0;
var marker = L.marker([46.26734, 12.328876], {icon: myIcon2}).on(&#39;mouseover&#39;, function(e){popup = L.popup().setLatLng(e.latlng).setContent(&#39;Luoghi, Cose, Strade&lt;br&gt;Diga del Vajont&#39;).openOn(mymap);whichPopup = 1;}).on(&#39;click&#39;, function(e){var popup2 = L.popup().setLatLng(e.latlng).setContent(&#39;&lt;a class=&quot;image-popup-no-margins hover-title&quot; href=&quot;https://atorinfriul.it/html/gpx/Luoghi_Cose_Strade__Diga_Vajont_NOT_MINIMIZED.php&quot;     Target=&quot;_blank&quot; title=&quot;Diga del Vajont&quot;&gt;   &lt;img loading=&quot;lazy&quot;  src=&quot;https://atorinfriul.it/html/images/luoghi_cose_strade/Fumetto-diga-del-vajont.jpg&quot; style=&quot;width:150px;&quot; alt=&quot;Responsive image&quot;&gt;&lt;/a&gt; &lt;div class=&quot;hover-image&quot;&gt;&lt;img loading=&quot;lazy&quot;  src=&quot;https://atorinfriul.it/html/images/luoghi_cose_strade/Fumetto-diga-del-vajont.jpg&quot; alt=&quot;Responsive image&quot;&gt;&lt;/div&gt;&#39;).openOn(mymap);whichPopup = 0}).addTo(mymap);
marker.on(&#39;mouseout&#39;, function (e) {
if (whichPopup == 1){
setTimeout(function() { mymap.closePopup();}, 5000);
}
});

huangapple
  • 本文由 发表于 2023年5月20日 20:54:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295336.html
匿名

发表评论

匿名网友

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

确定