[英]Make <svg> scrollable depending on number of <rect> elements
I'm trying to make my <svg>
element that contains an unknown amount of <rect>
elements fill the screen's width, evenly apart and become scrollable on a smaller device, however, despite my <div>
having an overflow, there's still too much "whitespace" to the right of the elements, how can I remove this and make the <rect>
elements only use as much space as needed?我正在尝试使包含未知数量
<rect>
元素的<svg>
元素填充屏幕的宽度,均匀分开并在较小的设备上变得可滚动,但是,尽管我的<div>
有溢出,但仍然存在元素右侧有很多“空白”,我怎样才能删除它并使<rect>
元素只使用所需的空间?
.wrapper { overflow-x: auto; max-width: 100%; } svg { overflow: hidden; vertical-align: middle; }
<div class="wrapper"> <svg height="30" version="1.1" viewbox="0 0 530 15" width="100%" xmlns="http://www.w3.org/2000/svg"> <rect aria-expanded="false" fill-opacity="1" height="30" width="12" rx="3" ry="3" x="0" y="0"></rect> <rect aria-expanded="false" fill-opacity="1" height="30" width="12" rx="3" ry="3" x="0" y="10.25"></rect> <rect aria-expanded="false" fill-opacity="1" height="30" width="12" rx="3" ry="3" x="0" y="20.25"></rect> </svg> </div>
For this I would use the event wheel and depending on the event.deltax I reset the viewBox of the svg element:为此,我将使用事件轮并根据 event.deltax 重置 svg 元素的 viewBox:
let svg = document.querySelector("svg"); //variable used to reset the value of the viewBox attribute let Y = 0; svg.addEventListener("wheel", on_wheel); function on_wheel(event) { //prevent the scroling event.preventDefault(); Y += event.deltaY; svg.setAttribute("viewBox", `-4 ${Y} 20 30`); }
svg { border:solid }
<svg viewbox="-4 0 20 30" width="200"> <rect height="30" width="12" rx="3" ry="3" x="0" y="0"></rect> <rect height="30" width="12" rx="3" ry="3" x="0" y="40"></rect> <rect height="30" width="12" rx="3" ry="3" x="0" y="80"></rect> </svg>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.