Add scrollbar to svg container

雨燕双飞 提交于 2019-12-05 02:15:26

You can take a look at this jsfiddle. The .get-oc-c container shows the scroll bar if needed:

.get-oc-c {
  overflow: auto !important;
}

and the SVG chart element is wrapped in a div that is resized to accomodate the full chart:

function wrapChart() {
  ...
  $("svg").wrap("<div id='svgContainer'></div>");
  ...
}

#svgContainer {
  overflow: visible;
}

The wrapChart method is called in the updatedEvent. The chart move option is disabled to remove the arrows on the sides and to prevent panning:

var orgChart = new getOrgChart(peopleElement, {
  enableMove: false,
  ...
});

The original display seems to work but getting the correct size values for the wrapper element is difficult (the expression used in the jsfiddle is very empirical), and it gets even more complicated when the window is resized, when the links are expanded/collapsed and when the chart is zoomed. Some resizing uses animations, so the calculations would have to account for the delay before getting the final values.

The jsfiddle shows some simple code to restore the scroll position after expanding/collapsing nodes but it would need to be improved. I haven't written code to account for window resizing and for zooming.

Given the amount of work needed to make the scroll bars behave correctly, it is probably better to use the panning and moving features supplied by the component. You could also contact the creators of the component and ask them to add the scroll bar option.

The important css rules missing here are:

max-height and overflow-y

overflow-y should be set to auto and max-height to a height the SVG should never cross (which is the height we will trigger scrollbar)

A 'static' approach will be to use media queries to set the expected sizes for different screens which will likewise trigger scrollbar appropriately on those sizes. Of course it may not work well if client is on a size you didn't think of.

I will personally recommend using a scroll plugin to handle responsiveness/auto-resizing to display scroll bars when needed though. CSS was traditionally built to support static HTML content but these days, we have a lot of dynamic content (not strictly from server side) and it's always a headache to catch and fix all issues. e.g. the appearance of a scrollbar can cause the width of your DOM to change but there's no way to watch for it via CSS. Also in some browsers traditional scroll bar is unnecessarily fat.

Some example plugins include:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!