jQuery-ui resizable:调整大小不适用于动态创建的SVG元素 [英] jQuery-ui resizable: Resizing doesn't work for dynamically created SVG element

查看:79
本文介绍了jQuery-ui resizable:调整大小不适用于动态创建的SVG元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在<div>内有一个SVG <rect>,想要拖动并调整其大小.当我像这样在HTML中静态创建元素时:

I have an SVG <rect> inside a <div> and want to drag and resize it. When I create the elements statically in the HTML like so:

<!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>一起拖动并调整大小,但是当我像这样动态生成元素时:

everything works. The <rect> is dragged and resized together with the <div>, but when I generate the elements dynamically like so:

<!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>完全不改变大小.

only dragging continues to work for both the <div> and the <rect>. Resizing works only for the <div>, the <rect> doesn't change size at all.

怎么了?

推荐答案

在使用正确的"viewBox"大小写的静态情况下,SVG区分大小写.在动态情况下则不需要.

SVG is case sensitive in the static case you use the correct "viewBox" case. In the dynamic case you don't.

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

应该是

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

这篇关于jQuery-ui resizable:调整大小不适用于动态创建的SVG元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆