【问题标题】:How to blur an image on mouseover using D3?如何使用 D3 在鼠标悬停时模糊图像?
【发布时间】:2016-06-15 04:15:22
【问题描述】:

我正在使用 D3,我想在鼠标悬停时模糊图像。

我在图片后面直接放置了一个rect 元素,并在图片上设置了pointer-events: none,在矩形上设置了pointer-events: all。这可以很好地捕获鼠标悬停事件,但我无法使图像模糊。

这是我设置图像及其rect 元素的 D3 代码:

var newImages = images.enter()
   .append("g")
   .attr("class", "image");

newImages.append("svg:image")
  .attr('class', 'image-body')
  .attr('x', 0).attr('y', 20)
  .attr("xlink:href", function(d, i) {
    return "http://placekitten.com/150/200";
  });

newImages.append("rect")
  .attr('class', 'image-background')
  .attr('x', 0).attr('y', 20);

还有 CSS 不起作用:

.image-background {
    stroke: black;
    stroke-width: 1px;
    fill: none;
    cursor: pointer;
    pointer-events: all;
    width: 150px;
    height: 200px;
}
.image-background:hover {
    -webkit-filter: blur(1px);
    -moz-filter: blur(1px);
    -o-filter: blur(1px);
    -ms-filter: blur(1px);
    filter: blur(1px);
}
.image-body {
    pointer-events:none;
    width: 150px;
    height: 200px;
}

如果我将fill: blue 添加到image-background: hover,那看起来就好了。是否可以添加模糊滤镜?

这是我的 JSFiddle:https://jsfiddle.net/p4bfsvw9/2/

【问题讨论】:

  • 我们不能让图像像这样模糊d3.select('.image-body').transition().duration(2000).style("opacity", 0); 像这样jsfiddle.net/3bwujq71
  • @Cyril 降低了不透明度,但不会模糊......我想这可以作为后备,谢谢。

标签: css d3.js svg


【解决方案1】:

您可以使用 SVG 滤镜。

创建过滤器:

 var filter = svg.append("defs")
      .append("filter")
      .attr("id", "blur")
      .append("feGaussianBlur")
      .attr("stdDeviation", 1); 

使用过滤器:

newImages.on("mouseover",function(){
        d3.select(this).select("image").attr("filter", "url(#blur)");
      })
      .on("mouseout",function(){
        d3.select(this).select("image").attr("filter", null);
      });

参考:http://www.w3schools.com/svg/svg_fegaussianblur.asp

var data = [{
  id: 1
}];

var svg = d3.select(".container")
  .append("svg")
  .attr("width", 500)
  .attr("height", 500)
  .append("g")
  .attr("transform", "translate(10,10)");

var filter = svg.append("defs")
  .append("filter")
  .attr("id", "blur")
  .append("feGaussianBlur")
  .attr("stdDeviation", 1);

var images = svg.selectAll(".image")
  .data(data, function(d) {
    return d.id;
  });

var newImages = images.enter()
  .append("g")
  .attr("class", "image");

newImages.append("svg:image")
  .attr('class', 'image-body')
  .attr('x', 0)
  .attr('y', 20)
  .attr("xlink:href", function(d, i) {
    return "http://placekitten.com/150/200";
  })

newImages.on("mouseover",function(){
    console.log(this);
    d3.select(this).select("image").attr("filter", "url(#blur)");
  })
  .on("mouseout",function(){
    d3.select(this).select("image").attr("filter", null);
  });

newImages.append("rect")
  .attr('class', 'image-background')
  .attr('x', 0).attr('y', 20);

newImages.append("rect")
  .attr('class', 'image-text-background')
  .attr('x', 0).attr('y', 30);

newImages.append("text")
  .attr('class', 'image-text')
  .attr('x', 0).attr('y', 50)
  .text('hello, kitty');
.image-background {
  stroke: black;
  stroke-width: 1px;
  fill: none;
  cursor: pointer;
  pointer-events: all;
  width: 150px;
  height: 200px;
}
.image-body {
  pointer-events: none;
  width: 150px;
  height: 200px;
}
.image-text-background {
  fill: white;
  width: 75px;
  height: 30px;
  opacity: 0;
}
.image-text {
  opacity: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div class="container">
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 2017-05-04
    相关资源
    最近更新 更多