在G标签内调整SVG rect元素的大小

Faq*_*uir 2 html javascript css jquery svg

我在网站上使用了SVG,但是在调整大小时遇到​​了一些麻烦。它显示在网站的标题内,在实现SVG(使用普通图像)之前,我正在调整图像大小,如下所示:

$("#block_175 img").height($("#header").height() / 2);
Run Code Online (Sandbox Code Playgroud)

现在,由于我使用的是SVG,因此我尝试了几种调整其大小的方法,但似乎找不到解决方法。这是我构建SVG的方法:

<div id='block_175' class=''>
<svg width="100" height="100">
    <g id="cross_svg">
        <rect id="Rectangle-1"  x="0" y="0" width="48" height="2" fill="transparent"></rect>
        <rect id="Rectangle-2"  x="0" y="14" width="48" height="2" fill="transparent"></rect>
        <rect id="Rectangle-4"  x="0" y="28" width="48" height="2" fill="transparent"></rect>
    </g>    
</svg> 
Run Code Online (Sandbox Code Playgroud)

CSS:

svg 
{
    width: 52px;
    height: 52px;
    z-index: 99999;
    transition: all .3s ease;
    cursor: pointer;
}

svg g 
{
    transition: all .3s ease;
    width: 100px;
    height: 100px;
    display: block;
    position: absolute;
    left: 50%;
    top: 50%;
    margin: auto;
    cursor: pointer;
}

svg rect 
{
    transition: all .3s ease;
    fill: #ffffff;
}

svg g 
{
    width: 100px;
    height: 100px;
}
Run Code Online (Sandbox Code Playgroud)

JS:

svg.on('click', function()
        {
            if (i) 
            {
                setTimeout(function()
                {
                    $(this).find("g").addClass('gotcha');

                    line_first.css(
                    {
                        'transform':'rotate(45deg)',
                        'left':'50%',
                        'top':'50%',
                        'width':'200px',
                        // This line BELONGS to @dervondenbergen :D 
                        // Enjoy your propriety my friend.
                        'transform-origin': 'left bottom'
                    })
                    line_third.css(
                    {
                        'transform':'rotate(-45deg) translate(-50%,-50%)',
                        'left':'50%',
                        'top':'50%'
                    })
                    line_second.css('opacity','0')
                },005)
            } 
            else 
            {
                setTimeout(function()
                {
                    line_first.attr('style', '');
                    line_third.attr('style', '');
                    line_second.css('opacity','1')
                },005)
            }

            i =! i; 

            $("#mobile_menu").slideToggle();
        });
Run Code Online (Sandbox Code Playgroud)

因此,恢复:我需要SVG的高度为#header的一半,但我不知道如何调整整个SVG的大小。

谢谢。

编辑1:(谢谢帕维尔·加特纳)

将CSS更改为此

    svg 
{

        width: 100%;
        height: 100%;
        z-index: 99999;
        transition: all .3s ease;
        cursor: pointer;
    }

    svg g 
    {
        transition: all .3s ease;
        width: 100%;
        height: 100%;
        display: block;
        position: absolute;
        left: 50%;
        top: 50%;
        margin: auto;
        cursor: pointer;
    }

    svg rect 
    {
        transition: all .3s ease;
        fill: #ffffff;
    }
Run Code Online (Sandbox Code Playgroud)

现在我正在调整容器div的大小,而不是SVG本身,但是它也不起作用。G元素不断溢出容器分区...

Dan*_*rzo 5

您缺少应设置为的“ viewbox”属性:

 <svg width='100' height='100' viewBox='0 0 100 100'>...</svg>
Run Code Online (Sandbox Code Playgroud)

这样,您可以将SVG视为图像,并以此调整其大小。

如果没有视图框,则更改宽度和高度时,SVG将不会按比例缩小。

以下是带有视图框的SVG和没有视图框的SVG的行为比较:http : //jsfiddle.net/k14qd88j/