这是我使用 jQuery 快速组合起来的一个解决方案。它可以很容易地修改为使用 Vanilla Javascript,但为了快速编写它,我使用了一个框架。
它基本上由一个相对定位的<div>组成,锚点作为绝对定位的地块。这使我们可以使用坐标将绘图定位在相对 <div> 内的任何我们喜欢的位置。
您可以创建一些图标来代表图表上的不同图,并将它们分配给 anchor 元素。
为了让事情变得简单易用,我使用了一个 JSON 对象来存储每个 plot,以便您可以从第 3 方使用它来源。
// change this to fit your needs
var plots = [
{"width":30,"height":30, "top": 150, "left": 100, "content": "Lorem"},
{"width":20,"height":20, "top": 100, "left": 20, "content": "Ipsum"},
{"width":50,"height":50, "top": 20, "left": 20, "content": "Dolor"}
];
// store our 2 divs in variables
var $content = $("#content");
var $map= $("#map");
// Iterate through each plot
$.each(plots, function() {
// store 'this' in a variable (mainly because we need to use it inside hover)
var plot = this;
// create a new anchor and set CSS properties to JSON values
var $plot = $("<a />")
.css({
'width': plot.width,
'height': plot.height,
'top': plot.top,
'left': plot.left
})
.hover(function() {
// replace content of target div
$content.html(plot.content);
});
// Add the plot to the placeholder
$map.append($plot);
});
希望我写的通俗易懂:)
请注意,以上所有内容都可以仅使用 HTML/CSS 来实现,只需检查源代码以查看究竟创建了什么。
Here's a demo