【发布时间】:2017-08-14 10:58:47
【问题描述】:
所以我正在使用 svg 开发地图,我希望能够根据用户输入放大到我的 svg 中的预定义区域(州或国家/地区)。我找到了一个教程,它可以做到这一点并为其设置动画,这里是:https://css-tricks.com/interactive-data-visualization-animating-viewbox/ 他们的codepen也在这里:https://codepen.io/sdras/pen/512230ed732f9b963ad3e50c8d4dcbb8
现在我在用自己的 svg 复制它时遇到了问题。当我告诉它放大某个区域时,它会放大另一个区域。我试过复制他们代码的确切结构,但没有成功。我开始认为错误出在我的 SVG 本身。
HTML:
<svg id="foo" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 1440 776">
那么这里复制粘贴的代码太多了。
SASS:
html
body
svg
width: 100vw
height: 100vh
JQuery:
var test = document.getElementById("_x32_");
console.log(test);
var s = test.getBBox();
//check the console for the SVGRect object
console.log( s );
//we store the values from the object as our new viewBox string
var newView = "" + s.x + " " + s.y + " " + s.width + " " + s.height;
console.log(newView);
//we then set the new viewBox string as the viewBox attribute on the SVG
var foo = document.getElementById("foo");
console.log(foo);
//foo.setAttribute("viewBox", newView);
CodePen 在这里:https://codepen.io/mat148/pen/xqWMXR
【问题讨论】:
标签: javascript jquery html css svg