var h = d3.select('section#trs').node().getBoundingClientRect().top; d3.select(window).on("scroll",function(){ var t = d3.select('#trs').node().getBoundingClientRect().top; var trans= Math.min(Math.round(100*(h-t)/h)/100,0.98); d3.selectAll('.trans').style("background-color","rgba(51,51,51,"+ trans +")"); });