更清洁的方式来使用HTML SVG元素?

时间:2016-04-17 20:12:50

标签: html svg

我有几个不同HTML元素的SVG元素。正如您在下面的示例中所看到的,它们会占用大量空间并使html变得混乱。

我想知道是否有人对清理它的最佳方法有任何建议。我可以为每个SVG元素使用部分HTML视图,但这有点过分,我只能想到。

<div id="canvasControl" class="cnvsControl" hidden="hidden" style="height:36px; background-color:black; position:relative;margin-top:-36px;z-index:9999;opacity: 0.2;">
    <div class="imageUpload canvasControlButton alignLeft" style="height:36px; position:relative;" title="Open File" id="btnOpenFile" name="btnOpenFile" onclick="snes_readfile(this)">
        <label for="btnFileImport" style="position:absolute;top:5px;">
            <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="25" viewBox="0 0 512 512">
                <g id="icomoon-ignore"></g>
                <defs>
                    <path id="openFolder1" d="M416 480l96-256h-416l-96 256zM64 192l-64 288v-416h144l64 64h208v64z"></path>
                </defs>
                <use fill="#fff" xlink:href="#openFolder1"></use>
            </svg>
        </label>
        <input id="btnFileImport" type="file" onchange="snes_readfile(this)" />
    </div>
    <button class="canvasControlButton alignRight" title="Full screen" id="btnFullScreen" name="btnFullScreen">
        <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="25" viewBox="0 0 512 512">
            <g id="icomoon-ignore"></g>
            <defs>
                <path id="outFullArrow1" d="M512 0h-208l80 80-96 96 48 48 96-96 80 80z"></path>
                <path id="outFullArrow2" d="M512 512v-208l-80 80-96-96-48 48 96 96-80 80z"></path>
                <path id="outFullArrow3" d="M0 512h208l-80-80 96-96-48-48-96 96-80-80z"></path>
                <path id="outFullArrow4" d="M0 0v208l80-80 96 96 48-48-96-96 80-80z"></path>
            </defs>
            <use fill="#fff" xlink:href="#outFullArrow1"></use>
            <use fill="#fff" xlink:href="#outFullArrow2"></use>
            <use fill="#fff" xlink:href="#outFullArrow3"></use>
            <use fill="#fff" xlink:href="#outFullArrow4"></use>
        </svg>
    </button>
    <button class="canvasControlButton alignRight" title="Bigger Screen" id="btnChangeScreenSize" name="btnChangeScreenSize">
        <svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="25" viewBox="0 0 512 512">
            <g id="icomoon-ignore"></g>
            <defs>
                <path id="outArrow1" d="M512 0v208l-80-80-96 96-48-48 96-96-80-80zM224 336l-96 96 80 80h-208v-208l80 80 96-96z"></path>
            </defs>
            <use fill="#fff" xlink:href="#outArrow1"></use>
        </svg>
    </button>

1 个答案:

答案 0 :(得分:2)

您可以创建单独的SVG文件,然后将其链接到HTML文档,如下所示:

创建名为

的SVG文件

one.svg:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="25" viewBox="0 0 512 512">
            <g id="icomoon-ignore"></g>
            <defs>
                <path id="openFolder1" d="M416 480l96-256h-416l-96 256zM64 192l-64 288v-416h144l64 64h208v64z"></path>
            </defs>
            <use fill="#fff" xlink:href="#openFolder1"></use>
        </svg>

two.svg

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="25" viewBox="0 0 512 512">
        <g id="icomoon-ignore"></g>
        <defs>
            <path id="outFullArrow1" d="M512 0h-208l80 80-96 96 48 48 96-96 80 80z"></path>
            <path id="outFullArrow2" d="M512 512v-208l-80 80-96-96-48 48 96 96-80 80z"></path>
            <path id="outFullArrow3" d="M0 512h208l-80-80 96-96-48-48-96 96-80-80z"></path>
            <path id="outFullArrow4" d="M0 0v208l80-80 96 96 48-48-96-96 80-80z"></path>
        </defs>
        <use fill="#fff" xlink:href="#outFullArrow1"></use>
        <use fill="#fff" xlink:href="#outFullArrow2"></use>
        <use fill="#fff" xlink:href="#outFullArrow3"></use>
        <use fill="#fff" xlink:href="#outFullArrow4"></use>
    </svg>

three.svg

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100%" height="25" viewBox="0 0 512 512">
        <g id="icomoon-ignore"></g>
        <defs>
            <path id="outArrow1" d="M512 0v208l-80-80-96 96-48-48 96-96-80-80zM224 336l-96 96 80 80h-208v-208l80 80 96-96z"></path>
        </defs>
        <use fill="#fff" xlink:href="#outArrow1"></use>
    </svg>

然后将您的HTML编辑为:

<div id="canvasControl" class="cnvsControl" hidden="hidden" style="height:36px; background-color:black; position:relative;margin-top:-36px;z-index:9999;opacity: 0.2;">
    <div class="imageUpload canvasControlButton alignLeft" style="height:36px; position:relative;" title="Open File" id="btnOpenFile" name="btnOpenFile" onclick="snes_readfile(this)">
        <label for="btnFileImport" style="position:absolute;top:5px;">
            <object>
            <embed scr="one.svg">
         </object>
        </label>
    <input id="btnFileImport" type="file" onchange="snes_readfile(this)" />
</div>
<button class="canvasControlButton alignRight" title="Full screen" id="btnFullScreen" name="btnFullScreen">
   <object>
        <embed scr="two.svg">
     </object>
</button>
<button class="canvasControlButton alignRight" title="Bigger Screen" id="btnChangeScreenSize" name="btnChangeScreenSize">
    <object>
        <embed scr="three.svg">
     </object>
</button>

希望它有所帮助,如果它能解决您的问题,请不要忘记验证答案。