javascript - 在 SVG 图形的相交部分添加悬停事件

标签 javascript jquery css svg

我已经创建了一个 fiddle http://jsfiddle.net/bhyt7L5z/

我有 5 个相互相交的椭圆。我想在相交区域捕获悬停事件并在工具提示中显示相应的数据。 像这样的东西:

    $('paths').hover(function(e) {
        console.log('show content related to intersection');
    });

任何人都可以指导我如何执行此操作。

JavaScript 中是否有提供交互式椭圆维恩图的图表

问候

最佳答案

只需为每个椭圆附加 .attr('id','ellipse1') 即可使用

 $('#ellipse1').hover(function(e) {
        console.log('show content related to intersection');
    });

来自你 jsfiddle 的代码:

var width = 450,
   height = 400


    var svg = d3.select("body").append("svg")
        .attr("width", width)
        .attr("height", height);


svg.append("svg:ellipse")
.attr("cx", 200)
.attr("cy", 200)
.attr("rx", 50)
.attr("ry",100)
.style("fill", 'rgba(108,255,108,0.4)')
.style("stroke", "#777")
.style("stroke-width", '1px')
.style("transform", 'rotate(30deg)')
.style("transform-origin", "50% 50%")
.attr("id","ellipse1")
.attr("data-toggle","tooltip")
.attr("title","ellipse1")


svg.append("svg:ellipse")
.attr("cx", 200)
.attr("cy", 200)
.attr("rx", 50)
.attr("ry",100)
.style("fill", 'rgba(168,255,168,0.4)')
.style("stroke", "#777")
.style("stroke-width", '1px')
.style("transform", 'rotate(70deg)')
.style("transform-origin", "50% 50%")
.attr("id","ellipse2")
.attr("data-toggle","tooltip")
.attr("title","ellipse2")

svg.append("svg:ellipse")
.attr("cx", 200)
.attr("cy", 200)
.attr("rx", 50)
.attr("ry",100)
.style("fill", 'rgba(148,255,148,0.4)')
.style("stroke", "#777")
.style("stroke-width", '1px')
.style("transform", 'rotate(110deg)')
.style("transform-origin", "50% 50%")
.attr("id","ellipse3")
.attr("data-toggle","tooltip")
.attr("title","ellipse3")

svg.append("svg:ellipse")
.attr("cx", 200)
.attr("cy", 200)
.attr("rx", 50)
.attr("ry",100)
.style("fill", 'rgba(128,255,128,0.4)')
.style("stroke", "#777")
.style("stroke-width", '1px')
.style("transform", 'rotate(140deg)')
.style("transform-origin", "50% 50%")
.attr("id","ellipse4")
.attr("data-toggle","tooltip")
.attr("title","ellipse4")
 $('[data-toggle="tooltip"]').tooltip({container: 'body'});   
svg { 
    border:1px dotted #000; 
}
<script src="http://d3js.org/d3.v3.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<div id="content"></div>

关于javascript - 在 SVG 图形的相交部分添加悬停事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36030578/

相关文章:

javascript - 使用数组和循环每隔几秒更改一次标题

javascript - 如何将 JSON 数据加载到 Bootstrap 表中?

php - 如何使用下面的代码切换类?

javascript - FabricJS:大网格对象模糊

javascript - Handlebars 转义字符串

jquery - 使用 HTML5 FileReader 读取本地镜像文件到 img

javascript - 查询 jquery 自动完成模块

css - 使用 CSS2/3、mixins 和绘图 API/HTML5 canvas 的 OpenLaszlo 现代组件集

悬停图像灰度 + <p> 仅在悬停时可见

javascript - SoundJS 如何/可以加载由其他 Preloader(例如 Pixi.loaders.Loader())加载的声音文件?