0

我有一个<object>加载外部 svg 文件的位置:

<object type="image/svg+xml" data="system.svg" id="Graph_Container" class="GraphContainmentArea">
    Your browser does not support SVG 
</object>

我正在使用以下库的 SVG 平移和缩放功能:https ://github.com/ariutta/svg-pan-zoom 。但是,仅将svg-pan-zoom.js文件添加到我的页面似乎不足以向object代码中包含的 svg 添加任何类型的功能:

<?xml version="1.0" encoding="UTF-8"?>

<iw:iwidget id="GraphContainer" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:iw="http://www.ibm.com/xmlns/prod/iWidget" supportedModes="view edit"
 lang="en" iScope="GraphContainerScope" title="Graph Container Widget">

<iw:content mode="view">
        <![CDATA[     
            <link rel="stylesheet" href="/Widgets/GraphContainerWidget/css/graphcontainer.css" />
            <script src="/Widgets/GraphContainerWidget/js/jquery.min.js"></script>
            <div id="GraphContainer_IWID_" class="search-area" style="display:block;">
                <div class="content-area" id="content-area_IWID_">
                    <div class="RightPanel" id="rightPanel_IWID_">
                        <table id='main-content_IWID_' class="links-included">
                            <tr>
                                <object type="image/svg+xml" data="" id="_IWID_Graph_Container" class="GraphContainmentArea">
                                    Your browser does not support SVG 
                                </object>
                            </tr>
                            <tr><td></td></tr>
                            <tr><td></td></tr>
                            <tr>
                                <td id="_IWID_Graph_Footer" class="FooterContainmentArea">Optional Footer</td>
                            </tr>
                        </table>
                    <div id='footer_IWID_' class="footer">
                    </div>
            </div>
        </div>
    </div>
    <script src="/Widgets/GraphContainerWidget/js/svg-pan-zoom.js"></script>
    <script type="text/javascript">
    var _IWID_attributesItemSet = iContext.getiWidgetAttributes();
    var _IWID_iDescriptor = iContext.getiDescriptor();

    var SVG_GRAPH_URL = _IWID_attributesItemSet.getItemValue("wid_import_field");
    var SVG_FOOTER = _IWID_attributesItemSet.getItemValue("wid_footer_field");
    var SVG_MULTI = _IWID_attributesItemSet.getItemValue("wid_multigraphs_field");

        console.log("Viewmode SRC URL: " + SVG_GRAPH_URL);
try {
    if (SVG_GRAPH_URL !== null && SVG_GRAPH_URL !== "") {
        var finished_URL = httpcheck() + SVG_GRAPH_URL;
        $("#Graph_Container").attr("data", finished_URL);

        setTimeout(function() {
            svgPanZoom("#_IWID_Graph_Container", {
                zoomEnabled: true,
                controlIconsEnabled: true
            });
        }, 2000);
    } else {
        alert("No URL passed to graph container!")
    }
}
catch(e) {
    console.log(e.name + "-" + e.description + "-" + e.message);
}

function httpcheck() {
    if (SVG_GRAPH_URL.substring(0,7) == "http://" || SVG_GRAPH_URL.substring(0,8) == "https://") {
        return "";
    } else {
        return "http://";
    }
}

    </script>
    ]]>
</iw:content>

注意:除了 SVG-Pan-Zoom.js 之外,jQuery 是我唯一使用的库 关于如何解决此问题/改进代码的任何提示?
我得到的当前错误是svgPan is not defined.

上面的代码更新了

4

1 回答 1

3

很难确定问题的原因,因为他们的示例可能无法复制

注意:我在我的应用程序上测试了“svg-pan-zoom”,它工作正常。

您的问题的可能原因:

  1. SVG您的页面需要在“相同域”中工作或支持 “CORS”

  2. 您正在使用“文件 URI 方案”,但这会导致“Web 浏览器”中的安全问题

  3. 克隆存储库的时间可能会出现问题。

解决方案

  1. 有关跨域 (CORS) 的问题,请阅读此链接:https ://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

  2. 对于“文件 URI”的问题,我建议安装“Xampp”(Apache 和 PHP)“Node.js”以使用“HTTP localhost”

  3. 对于“git下载”的问题,您需要git clone --recursive git@github.com:ariutta/svg-pan-zoom.git或者如果您已经克隆了它,那么git submodule update --init --recursive

示例宽度“数据 URI”

注意:仅在 Firefox 中有效,因为在 Chrome 中“数据 URI 方案”不发送“原始标头”

注意:在 Chrome 中使用“http URI 方案”加载您的 SVG

注意:我使用“数据 URI”,因为 jsfiddle 不支持发送“资源​​”

<script src="svg-pan-zoom.js"></script>
<object type="image/svg+xml" id="svgId" data="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIj8+Cjxzdmcgd2lkdGg9IjI2NiIgaGVpZ2h0PSIxNzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CiA8IS0tIENyZWF0ZWQgd2l0aCBNZXRob2QgRHJhdyAtIGh0dHA6Ly9naXRodWIuY29tL2R1b3BpeGVsL01ldGhvZC1EcmF3LyAtLT4KIDxnPgogIDx0aXRsZT5iYWNrZ3JvdW5kPC90aXRsZT4KICA8cmVjdCBmaWxsPSIjZmZmIiBpZD0iY2FudmFzX2JhY2tncm91bmQiIGhlaWdodD0iMTczIiB3aWR0aD0iMjY4IiB5PSItMSIgeD0iLTEiLz4KICA8ZyBkaXNwbGF5PSJub25lIiBvdmVyZmxvdz0idmlzaWJsZSIgeT0iMCIgeD0iMCIgaGVpZ2h0PSIxMDAlIiB3aWR0aD0iMTAwJSIgaWQ9ImNhbnZhc0dyaWQiPgogICA8cmVjdCBmaWxsPSJ1cmwoI2dyaWRwYXR0ZXJuKSIgc3Ryb2tlLXdpZHRoPSIwIiB5PSIwIiB4PSIwIiBoZWlnaHQ9IjEwMCUiIHdpZHRoPSIxMDAlIi8+CiAgPC9nPgogPC9nPgogPGc+CiAgPHRpdGxlPkxheWVyIDE8L3RpdGxlPgogIDxyZWN0IGlkPSJzdmdfNCIgaGVpZ2h0PSI2NCIgd2lkdGg9IjE1My45OTk5OTYiIHk9IjQwIiB4PSI0MyIgb3BhY2l0eT0iMC41IiBmaWxsLW9wYWNpdHk9Im51bGwiIHN0cm9rZS1vcGFjaXR5PSJudWxsIiBzdHJva2Utd2lkdGg9IjEuNSIgc3Ryb2tlPSIjMDAwIiBmaWxsPSIjZmZmIi8+CiA8L2c+Cjwvc3ZnPg==">
    Your browser does not support SVG or not loaded (Work only in Gecko - Firefox) 
</object>

<script>
window.onload = function() {
    svgPanZoom("#svgId", {
        zoomEnabled: true,
        controlIconsEnabled: true
    });
};
</script>

在线示例:http: //jsfiddle.net/L4u6ap2w/

于 2014-10-06T23:29:44.303 回答