我在<rect>
中有一个SVG <div>
,想要拖动它并调整大小。当我在HTML中静态创建元素时,如下所示:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/start/jquery-ui.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('div').draggable({
handle: 'rect'
}).resizable({
aspectRatio: 1.0
});
});
</script>
</head>
<body>
<div style="width:400px; height:400px; border:solid thin #888; padding:10px; border-radius:4px; background-color:#ccc;">
<svg xmlns="http://www.w3.org/2000/svg" version="1.0" viewBox="0 0 400 400">
<rect x="0" y="0" width="200" height="200" style="fill:#FF0000" />
</svg>
</div>
</body>
一切正常。
<rect>
与<div>
一起拖动并调整大小,但是当我像这样动态生成元素时:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/themes/start/jquery-ui.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('body').append('<div style="width:400px; height:400px; border:solid thin #888; padding:10px; border-radius:4px; background-color:#ccc;">');
var svg = document.createElementNS('http://www.w3.org/2000/svg', "svg");
svg.setAttributeNS(null, "viewbox", "0 0 400 400");
$('div').append(svg);
var square = document.createElementNS('http://www.w3.org/2000/svg', "rect");
square.setAttributeNS(null, "width", "200");
square.setAttributeNS(null, "height", "200");
square.setAttributeNS(null, "x", "0");
square.setAttributeNS(null, "y", "0");
square.setAttributeNS(null, "style", "fill:#FF0000");
$('svg').append(square);
$('div').draggable({
handle: 'rect'
}).resizable({
aspectRatio: 1.0
});
});
</script>
</head>
<body>
</body>
</html>
只有拖动才能继续适用于<div>
和<rect>
。调整大小仅适用于<div>
,<rect>
根本不会更改大小。
怎么了?
答案 0 :(得分:4)
在使用正确的“viewBox”情况的静态情况下,SVG区分大小写。在动态情况下,你没有。
svg.setAttributeNS(null, "viewbox", "0 0 400 400");
应该是
svg.setAttributeNS(null, "viewBox", "0 0 400 400");