Created
July 24, 2014 13:55
-
-
Save joews/71d4f2a2af1746c37328 to your computer and use it in GitHub Desktop.
D3 opacity tween vs CSS transform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<style> | |
html, body { | |
height: 100%; | |
} | |
.wrapper { | |
width: 500px; | |
} | |
.block { | |
float: left; | |
width: 50px; | |
height: 50px; | |
opacity: 1; | |
transition: opacity 1s; | |
} | |
.block.out { | |
opacity: 0; | |
} | |
button { | |
margin: 10px 10px 10px 0; | |
} | |
</style> | |
</head> | |
<body> | |
<div class="buttons"> | |
<button data-action="css">CSS fade</button> | |
<button data-action="d3">d3 tween fade</button> | |
</div> | |
<div class="wrapper"></div> | |
<script src="http://d3js.org/d3.v3.js"></script> | |
<script> | |
var visible = true; | |
var n = 5000, | |
blockSize = '50px'; | |
var color = d3.scale.category20(); | |
var el = d3.select('.wrapper'); | |
var blocks = el.selectAll('.block') | |
.data(d3.range(n)) | |
.enter() | |
.append('div') | |
.attr('class', 'block') | |
.style('background-color', function(d, i) { return color(d) }) | |
function toggleFadeTween() { | |
blocks.transition().duration(1000) | |
.style('opacity', +!visible); | |
visible = !visible; | |
} | |
function toggleFadeCss() { | |
blocks | |
.style('opacity', undefined) | |
.classed({ out: visible }); | |
visible = !visible; | |
} | |
d3.select('[data-action="css"]').on('click', toggleFadeCss); | |
d3.select('[data-action="d3"]').on('click', toggleFadeTween); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment