在D3中使用闪烁功能
后,我在一段时间后收到错误消息.transition()
.duration(250)
.delay( 0)
.each(blink);
function blink() {
var line = d3.select(this);
(function repeat() {
line = line.transition()
.style("opacity", function(d) { return(1);})
.transition()
.style("opacity", function(d) { return(0);})
.each("end", repeat);})();
}
答案 0 :(得分:2)
正如我在评论中所说:
你创建了一个嵌套的过渡,每17ms,你创建另一个过渡,反过来每17ms发射一次......
要解决此问题,请删除嵌套:
<!DOCTYPE html>
<html>
<head>
<script data-require="d3@3.5.3" data-semver="3.5.3" src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.js"></script>
</head>
<body>
<script>
var svg = d3.select('body')
.append('svg')
.attr('width', 300)
.attr('height', 300);
svg
.append('line')
.attr('y1', 150)
.attr('y2', 150)
.attr('x1', 0)
.attr('x2', 300)
.style('fill', 'none')
.style('stroke', 'steelblue');
svg.selectAll("line")
.each(blink);
function blink(){
var self = d3.select(this);
self
.transition()
.duration(250)
.style("opacity", function(d) {
return self.style("opacity") === "1" ? "0" : "1";
})
.each("end", blink);
}
</script>
</body>
</html>
当然,如果你只是想让某些东西眨眼,你可以这样做with straight css:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<style>
.blink {
animation: blink-animation 1s steps(5, start) infinite;
-webkit-animation: blink-animation 1s steps(5, start) infinite;
}
@keyframes blink-animation {
to {
visibility: hidden;
}
}
@-webkit-keyframes blink-animation {
to {
visibility: hidden;
}
}
</style>
</head>
<body>
<script>
var svg = d3.select('body')
.append('svg')
.attr('width', 300)
.attr('height', 300);
svg
.append('line')
.attr('class', 'blink')
.attr('y1', 150)
.attr('y2', 150)
.attr('x1', 0)
.attr('x2', 300)
.style('fill', 'none')
.style('stroke', 'steelblue');
</script>
</body>
</html>