【问题标题】:Adding mouseover effects with d3.tip使用 d3.tip 添加鼠标悬停效果
【发布时间】:2016-02-25 13:14:37
【问题描述】:

有没有办法用d3.tip添加鼠标悬停效果

假设我有这个

var tip = d3.tip()
    .attr("class", "d3-tip")
    .html(function(d) { 
        return d.properties.xy
    });


svg.call(tip);
var feature = g.selectAll("circle")
      .data(data.features)
      .enter()
      .append("circle")
      .attr("r", function (d) {
          d.properties.xy
      })
      .style("fill", "red")
      .attr("fill-opacity", 0.5)
      .on("mouseover", tip.show)
      .on("mouseout", tip.hide);

这给了我一个通过d3.tip 的工具提示。但是,如果我想要一些效果,就像鼠标悬停一样:

feature.on("mouseover",function(d) { 
       d3.select(this)
      .transition()
      .ease("elastic")
      .duration(500)
      .attr('r', function (d){ 
          return (10 * d.properties.xy)
      })
      .style("stroke", "black")
      .style("stroke-width", 2)
     });

有没有办法将这两种方法结合起来?

JSfiddle 这里缺少的是鼠标悬停时带有 d3.tip 的工具提示,就像 example 一样!

【问题讨论】:

    标签: javascript dom d3.js mouseover


    【解决方案1】:

    问题是因为提示不知道要显示什么元素。所以不是:

    tip.show
    

    将你希望显示的元素传递给它:

    tip.show(d)
    

    所以你的函数看起来像这样:

     feature.on("mouseover",function(d) { 
                d3.select(this)
                .transition()
                .ease("elastic")
                .duration(1000)
                .attr('r', function(d) {
                return (d.value * 5)
                })
                .style("stroke", "green")
                .style("stroke-width", 2)
                .style("fill", "red")
                tip.show(d)
                });
    

    更新小提琴:https://jsfiddle.net/reko91/qc2m52zf/5/

    当你这样做时:

    .on('mouseover,tip.show)
    

    相当于

    .on('mouseover',function(d){ tip.show(d)});
    

    【讨论】:

    • 还没有实现鼠标移出,所以你必须这样做,简单:))
    • 太棒了。谢谢你一直陪着我。让我们删除问题下面的所有cmets?!
    • 没问题,第一次问的时候很忙,所以花了一天左右的时间才正确回复:)
    猜你喜欢
    • 1970-01-01
    • 2015-04-22
    • 2020-08-16
    • 2013-08-31
    • 1970-01-01
    • 2013-11-23
    • 2016-04-26
    • 2013-05-06
    • 1970-01-01
    相关资源
    最近更新 更多