D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jgllanos
Full window
Github gist
slider time test
Built with
blockbuilder.org
<!DOCTYPE html> <head> <meta charset="utf-8"> <script src="https://d3js.org/d3.v4.min.js"></script> <style> body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } .track, .track-inset, .track-overlay { stroke-linecap: round; } .track { stroke: #000; stroke-opacity: 0.3; stroke-width: 10px; } .track-inset { stroke: #ddd; stroke-width: 8px; } .track-overlay { pointer-events: stroke; stroke-width: 50px; stroke: transparent; cursor: crosshair; } .meanSliderHandle { fill: #fff; stroke: #000; stroke-opacity: 0.5; stroke-width: 1.25px; } </style> </head> <body> <script> // Feel free to change or delete any of the code you see in this editor! var svg = d3.select("body").append("svg") .attr("width", 960) .attr("height", 500); var data = [1, 2, 3, 4, 5]; function update(numCircles) { data = []; for (var i = 0; i < numCircles; i++){ data.push(i); } var circles = svg.selectAll("circle") .data(data); circles.enter() .append("circle") .attr("cy", 60) .attr("cx", function(d, i) {return i*100 + 30;}) .attr("r", function(d) {return d*d;}); circles.exit().remove(); } var sliderScale = d3.scaleLinear() .range([0,159]) .domain([1, 10]) .clamp(true); var slider = svg.append("g") .attr("class", "slider") .attr("transform", "translate(" + 50 + "," + 200 + ")"); slider.append("line") .attr("class", "track") .attr("x1", 0) .attr("x2", 159) .select(function() { return this.parentNode.appendChild(this.cloneNode(true)); }) .attr("class", "track-inset") .select(function() { return this.parentNode.appendChild(this.cloneNode(true)); }) .attr("class", "track-overlay") .call(d3.drag() .on("start.interrupt", function() { slider.interrupt(); }) .on("start drag", function() { var numCircles = sliderScale.invert(d3.event.x); sliderHandle.attr("cx", sliderScale(numCircles)); update(numCircles); })); var sliderHandle = slider.insert("circle", ".track-overlay") .attr("class", "sliderHandle") .attr("r", 9) .attr("cx", sliderScale(1)); update(5); </script> </body>
https://d3js.org/d3.v4.min.js