I have a Modal dialog that you can see at this link:
https://codepen.io/rgraph/full/KKOyYYK
<script src="https://www.rgraph.net/libraries/RGraph.common.core.js"></script>
<script src="https://www.rgraph.net/libraries/RGraph.modaldialog.js"></script>
<script src="https://www.rgraph.net/libraries/RGraph.bar.js"></script>
<canvas id="cvs" width="700" height="300"></canvas>
<p>
tf ty fty fty f ty fty
tf ty fty fty f ty fty
tf ty fty fty f ty fty
tf ty fty fty f ty fty
tf ty fty fty f ty fty
tf ty fty fty f ty fty
tf ty fty fty f ty fty
tf ty fty fty f ty fty
tf ty fty fty f ty fty
... Add more text to force a
... scrollbar to appear
</p>
<script>
bar = new RGraph.Bar({
id: 'cvs',
data: [8,4,6,5,3,4,5],
options: {
}
}).draw();
bar.canvas.onclick = function (e)
{
ModalDialog.show('string:<div style="text-align: center"><h1>This is a dialog!</h1><br /><button style="font-size: 20pt" onclick="ModalDialog.close()">OK</button></div>');
};
</script>
Now, when you click the chart and show the dialog the page is still scrollable (depending on your screen/browser size).
Previously, I have had a DIV that covers the scrollbars by using an iframe to show a page and then the page that contains the iframe tag shows the dialog – which then covers the iframe and it’s scrollbars.
But is there a way to disable scrolling with a single (or a few) CSS properties without this iframe method?
2
Answers
You could use the following JS when your modal shows:
then you can reset the behavior to default "scroll" when closing the modal.
This code solves the issue by making the
<canvas>
responsive, which removes the horizontal scroll. The canvas element adjusts to the available screen width, thanks to setting thewidth: 100%
and limiting themax-width
to a specific value (700px in this example). This ensures that the canvas scales according to the screen size without overflowing horizontally.A vertical scroll may still appear if the content within the
<body>
exceeds the viewport height (vh), but it will only activate when there’s enough content to go beyond the visible area. The paragraph (<p>
) here contains placeholder text, simulating this situation.Here’s a breakdown of the CSS for the responsive canvas: