$(document).ready(function () { initPage(); bindEvents(); sidebarScrollFix(); }); var aceEditor; var containExamples = true; function initPage() { initSideBar(); initEditor(); screenResize(); } function initSideBar() { var config = exampleConfig; var sideBar = $("ul#sidebar-menu"); for (var key in config) { sideBar.append(createSideBarMenuItem(key, config[key], containExamples)); } $(sideBar).ready(function () { initSelect(); }); } function screenResize() { window.onresize = function () { mapHeight(); }; } //初始化编辑器 function initCodeEditor() { if (!aceEditor) { aceEditor = ace.edit("editor"); aceEditor.setTheme("ace/theme/textmate"); //aceEditor.getSession().setMode("ace/mode/html"); aceEditor.getSession().setUseWrapMode(true); aceEditor.setShowPrintMargin(false); aceEditor.$blockScrolling = Infinity; } aceEditor.setValue($('#editor').val()); aceEditor.clearSelection(); aceEditor.moveCursorTo(0, 0); } //初始化编辑器以及预览内容 function initEditor() { loadExampleHtml(); initCodeEditor(); } function loadExampleHtml() { var locationParam = getLocationParam(); if (!locationParam) { return; } var href = window.location.toString(); var mapUrl = href.substr(0, href.lastIndexOf('/') + 1); mapUrl = mapUrl + locationParam + ".html"; if (!mapUrl) { return; } var html = $.ajax({ url: mapUrl, async: false, error: function (error) { alert("请在服务器环境下运行示范程序!"); html = ""; }, }).responseText; if (html && html != "") { $('#editor').val(html); loadPreview(html); } } function getLocationParam() { var param = window.location.toString(); if (param.indexOf("#") === -1) { return null; } param = param.split("#"); if (param && param.length > 0) { return param[1]; } } //运行代码 function run() { var iframeContent = $("#editor").val(); if (editor) { iframeContent = aceEditor.getValue(); } loadPreview(iframeContent); } //填充预览效果内容 function loadPreview(content) { var iFrame = createIFrame(), iframeDocument = iFrame.contentWindow.document; iframeDocument.open(); iframeDocument.write(content); iframeDocument.close(); iframeDocument.addEventListener('load', function () { mapHeight(); }); iFrame.onload = iFrame.onreadystatechange = ()=>{ if (iFrame.readyState && iFrame.readyState != "complete") { return; } else { $("#loadingbar").remove(); //清除加载动画 } } mapHeight(); } function createIFrame() { var preViewPane = $("#previewPane"); preViewPane.empty(); let s = `