悬停在页面弹出窗口中

2024-06-02 11:43:50 发布

您现在位置:Python中文网/ 问答频道 /正文

举个简单的例子:

import folium

map_1 = folium.Map(location=[45.372, -121.6972], zoom_start=12,
                   tiles='Stamen Terrain')
folium.Marker([45.3288, -121.6625], popup='Mt. Hood Meadows').add_to(map_1)
map_1

你能让一个弹出窗口出现只要把鼠标放起来? 对叶子有这种可能吗?在


Tags: importmaplocationstartmarker例子tilespopup
3条回答

你不可能从叶上轻易做到的。但是由于folium确实创建了sportlejs代码,所以您可以修改输出使其正常工作。为此,您必须将this answer中所述的代码添加到生成的html中:

    marker.bindPopup("Popup content");
    marker.on('mouseover', function (e) {
        this.openPopup();
    });
    marker.on('mouseout', function (e) {
        this.closePopup();
    });

如果您有许多标记,这可能会成为一个困难的任务,但您可以通过python来完成(尽管它看起来并不漂亮)。伪代码:

^{pr2}$

这些代码打开生成的html文件,查找所有标记,并为每个标记添加代码。在

在下面的示例中,popup在鼠标悬停时打开,而不是在单击时打开,并在用户鼠标悬停时将其隐藏:

map_1.save('map_1.html')
import re
import fileinput

with open("map_1.html") as inf:
   txt = inf.read()

#Find all the markers names given by folium
markers = re.findall(r'\bmarker_\w+', txt)
markers = list(set(markers))

for linenum,line in enumerate( fileinput.FileInput("map_1.html",inplace=1) ):
    pattern = markers[0] + ".bindPopup"
    pattern2 = markers[0] + ".on('mouseover', function (e) {this.openPopup();});"
    pattern3 = markers[0] + ".on('mouseout', function (e) {this.closePopup();});"

    if pattern in line:
       print(line.rstrip())
       print(pattern2)
       print(pattern3)
    else:
       print(line.rstrip())

你的问题是关于folium,我不这么认为,但是我认为您可以添加一些Javascript(使用jquery,我想这是非常容易的),来模拟单击事件onmouseover或{}

  var test = document.getElementById("test");

  test.addEventListener("mouseenter", handlerClickFunction);

相关问题 更多 >