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