Skip to content

Instantly share code, notes, and snippets.

@andipollok
Last active December 29, 2015 16:09
Show Gist options
  • Select an option

  • Save andipollok/7695784 to your computer and use it in GitHub Desktop.

Select an option

Save andipollok/7695784 to your computer and use it in GitHub Desktop.
Button mouseover transitions

Button mouseover animations with d3 transitions, which offer much more possibilities than CSS animations. Bonus points for knowing why I selected these six countries for this demo!

<!DOCTYPE html>
<meta charset="utf-8">
<title></title>
<script src="http://d3js.org/d3.v3.min.js"></script>
<style>
svg {
font-family: Helvetica, Arial, sans-serif;
}
</style>
<body>
<script>
var width = 960,
height = 500;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var numberOfButtons = 6;
var x = d3.scale.linear()
.domain([0,numberOfButtons+1])
.range([0,width]);
var buttons = d3.range(numberOfButtons).map(function(d,i) {
return { id: i+1, x: x(i+1), y: height/2 }
});
var buttonGroup = svg.selectAll("g")
.data(buttons)
.enter().append("g")
.attr("transform", function(d) { return "translate("+d.x+","+d.y+")"; });
buttonGroup.append("circle")
.attr("r", 20)
.attr("fill", '#000')
.attr("cursor", "pointer")
.on("mouseover", function(d,i) {
d3.select(this).transition()
.ease("elastic")
.duration("500")
.attr("r", 35);
d3.select("#clipCircle"+i+" circle").transition()
.ease("cubic-out")
.duration("200")
.attr("r", 32);
d3.select("#text"+i).transition()
.ease("cubic-out")
.duration("200")
.attr("y", 12)
.attr("font-size", 32)
.attr("fill", "#333");
})
.on("mouseout", function(d,i) {
d3.select(this).transition()
.ease("quad")
.delay("100")
.duration("200")
.attr("r", 20);
d3.select("#clipCircle"+i+" circle").transition()
.ease("quad")
.delay("100")
.duration("200")
.attr("r", 0);
d3.select("#text"+i).transition()
.ease("cubic-out")
.duration("400")
.delay("100")
.attr("y", 7)
.attr("font-size", 20)
.attr("fill", "#FFF");;
} );
buttonGroup.append("clipPath")
.attr("id", function(d, i) { return "clipCircle" + i; })
.append("circle")
.attr("r", 0);
buttonGroup.append("image")
.attr("xlink:href", function(d,i) { return "images/flags.png"; })
.attr("id", function(d,i) { return "image"+i; })
.attr("clip-path", function(d,i) { return "url(#clipCircle"+i+")"; })
.attr("x", function(d,i) { return -i*80-40; })
.attr("y", -40)
.attr("width", numberOfButtons*80)
.attr("height", 80)
.attr("pointer-events", "none");
buttonGroup.append("text")
.text(function(d) { return d.id; })
.attr("text-anchor", "middle")
.attr("y", 7)
.attr("id", function(d, i) { return "text" + i; })
.attr("font-size", "20px")
.attr("fill", "#FFF")
.attr("pointer-events", "none");
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment