2

<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>サイズはまったく変更されません。

どうしたの?

4

1 に答える 1

4

SVGは、正しい「viewBox」ケースを使用する静的なケースでは大文字と小文字を区別します。動的な場合はそうではありません。

    svg.setAttributeNS(null, "viewbox", "0 0 400 400");

する必要があります

    svg.setAttributeNS(null, "viewBox", "0 0 400 400");
于 2012-11-11T13:50:50.000 に答える