Skip to content

Instantly share code, notes, and snippets.

@oshikryu
Last active December 31, 2015 23:29
Show Gist options
  • Save oshikryu/8060638 to your computer and use it in GitHub Desktop.
Save oshikryu/8060638 to your computer and use it in GitHub Desktop.
An attempt to automatically scroll an svg when a d3 tree node is added
_scrollContainer: ->
targets = d3.select(d3.selectAll('g.node.selection').pop()).node()
return unless targets
selectionCoord = targets.pop().getBoundingClientRect()
selectedTarget = d3.select('g.selected')
selectedTarget = _.compact selectedTarget[0]
return unless selectedTarget.length > 0
selectedCoord = selectedTarget[0].getBoundingClientRect()
bottomCoord = Math.max selectionCoord.bottom, selectedCoord.bottom
topCoord = Math.min selectionCoord.top, selectedCoord.top
scrollHeight = @$('#tree-container')[0].scrollHeight - @$('#tree-container').height()
curScroll = @$('#tree-container').scrollTop()
if topCoord < 100
curScroll -= (topCoord + 100)
return curScroll
if bottomCoord > 340
curScroll += (bottomCoord - 340)
else
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment