当前位置: 首页 > news >正文

HTML网页代码预览器

HTML网页代码预览器

可以用于学习和实验HTML和CSS,比较方便。源码参考自网络。

功能

实时预览:当你在左侧的“代码编辑器”中输入代码时,右侧的“预览窗口”会实时显示你的网页效果(注意,不能体现嵌入的JavaScript运行效果)。

清空代码:如果你想要重新开始,只需点击页脚的“清空代码”按钮。

屏显切换:想要更大的编辑空间?点击“屏显切换”,如果你想要退出全屏模式,只需再次点击“屏显切换”或者使用浏览器的全屏退出功能。

下面给出一段输入测试代码(你当然可以输入其它网页代码试试):

<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>测试</title>
    <style>
      body {
        margin: 0;
        padding: 20px;
        color: red;
      }
      .container {
        width: 100%;
      }
    </style>
  </head>
  <body>
    <div>
      <p>测试</p>
      <img src="test.png" />
      <!-- 注释 -->
      <ul>
        <!-- 注释 -->
        <li>项目1</li>
        <li>项目2</li>
      </ul>
    </div>

    <div class="container">内容</div>
  </body>
</html>

运行效果

这个HTML网页代码预览器,由两个文件组成(请将两个文件在同一个文件夹中):

(1)html文件源码如下:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta charset="UTF-8">
    <title>HTML网页代码预览器</title>
    <link rel="stylesheet" href="./normalize.css">
    <style>
        body {
            font-family: 'Arial', sans-serif;
            display: flex;
            height: 100vh;
            background-color: #f4f4f9;
        }
        header {
            background-color: #6a1b9a;
            color: white;
            text-align: center;
            padding: 1em 0;
            position: fixed;
            width: 100%;
            z-index: 1000;
        }
        main {
            display: flex;
            width: 100%;
            margin-top: 5em;
            padding: 1em;
        }
        .code-editor {
            width: 50%;
            height: calc(100vh - 12em);
            padding: 1em;
            font-size: 16px;
            resize: none;
            overflow-y: auto;
            tab-size: 4;
            background-color: #fff;
            border: 1px solid #ddd;
            outline: none;
            border-radius: 5px;
        }
        .preview-window {
            width: 50%;
            height: calc(100vh - 12em);
            border: 1px solid #ddd;
            background-color: #fff;
            border-radius: 5px;
            margin-left: 1em;
        }
        footer {
            background-color: #6a1b9a;
            color: white;
            text-align: center;
            padding: 0.5em 0;
            position: fixed;
            bottom: 0;
            width: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        button {
            margin: 0 0.5em;
            padding: 0.5em 1em;
            cursor: pointer;
            background-color: #ffab91;
            border: none;
            border-radius: 5px;
            color: white;
        }
        button:hover {
            background-color: #ef5350;
        }
        .toast {
            position: fixed;
            top: 10px;
            left: 50%;
            transform: translateX(-50%);
            background-color: #333;
            color: white;
            padding: 0.5em 1em;
            border-radius: 5px;
            z-index: 1001;
            opacity: 0;
            transition: opacity 0.5s ease-in-out;
        }
        .design-by {
            position: absolute;
            bottom: 10px;
            right: 10px;
            font-size: 12px;
            color: #999;
        }
    </style>
</head>
<body>
    <header>
        <h1>HTML网页代码预览器</h1>
    </header>
    <main>
        <textarea id="codeEditor" class="code-editor" placeholder="在此处编写您的HTML代码..."></textarea>
        <iframe id="previewWindow" class="preview-window"></iframe>
    </main>
    <footer>
        <button aria-label="清空代码" onclick="clearCode()">清空代码</button>
        <button aria-label="屏显切换" onclick="toggleFullScreen()">屏显切换</button>
    </footer>
    <div id="toast" class="toast"></div>
    <div class="design-by">设计:</div>
 
    <script>
        (function() {
            const codeEditor = document.getElementById('codeEditor');
            const previewWindow = document.getElementById('previewWindow').contentDocument.body;
            const toast = document.getElementById('toast');
 
            // 将函数挂载到window对象
            window.clearCode = clearCode;
            window.toggleFullScreen = toggleFullScreen;

            function showToast(message) {
                toast.textContent = message;
                toast.style.opacity = 1;
                setTimeout(() => {
                    toast.style.opacity = 0;
                }, 3000);
            }
 
            function updatePreview() {
                try {
                    previewWindow.innerHTML = codeEditor.value;
                } catch (error) {
                    console.error("预览更新时出错:", error);
                    showToast("预览更新时出错,请检查代码!");
                }
            }
 
            function clearCode() {
                codeEditor.value = '';
                updatePreview();
                showToast("代码已清空!");
            }
 
 
            function toggleFullScreen() {
                if (!document.fullscreenElement) {
                    document.documentElement.requestFullscreen().catch(err => {
                        console.error(`启用全屏模式时出错: ${err.message} (${err.name})`);
                        showToast("启用全屏模式时出错!");
                    });
                } else {
                    if (document.exitFullscreen) {
                        document.exitFullscreen();
                    }
                }
            }
 
            codeEditor.addEventListener('input', () => {
                updatePreview();
            });
 
            window.onload = () => {
                updatePreview();
            };
        })();
    </script>
</body>
</html>

(2) normalize.css文件

上面的html文件用到的normalize.css文件,内容如下:

/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */
html {
	line-height: 1.15;
	-webkit-text-size-adjust: 100%
}

body {
	margin: 0
}

main {
	display: block
}

h1 {
	font-size: 2em;
	margin: .67em 0
}

hr {
	box-sizing: content-box;
	height: 0;
	overflow: visible
}

pre {
	font-family: monospace,monospace;
	font-size: 1em
}

a {
	background-color: transparent
}

abbr[title] {
	border-bottom: none;
	text-decoration: underline;
	text-decoration: underline dotted
}

b,strong {
	font-weight: bolder
}

code,kbd,samp {
	font-family: monospace,monospace;
	font-size: 1em
}

small {
	font-size: 80%
}

sub,sup {
	font-size: 75%;
	line-height: 0;
	position: relative;
	vertical-align: baseline
}

sub {
	bottom: -.25em
}

sup {
	top: -.5em
}

img {
	border-style: none
}

button,input,optgroup,select,textarea {
	font-family: inherit;
	font-size: 100%;
	line-height: 1.15;
	margin: 0
}

button,input {
	overflow: visible
}

button,select {
	text-transform: none
}[type=button],[type=reset],[type=submit],button {
	-webkit-appearance: button
}[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner,button::-moz-focus-inner {
	border-style: none;
	padding: 0
}[type=button]:-moz-focusring,[type=reset]:-moz-focusring,[type=submit]:-moz-focusring,button:-moz-focusring {
	outline: 1px dotted ButtonText
}

fieldset {
	padding: .35em .75em .625em
}

legend {
	box-sizing: border-box;
	color: inherit;
	display: table;
	max-width: 100%;
	padding: 0;
	white-space: normal
}

progress {
	vertical-align: baseline
}

textarea {
	overflow: auto
}[type=checkbox],[type=radio] {
	box-sizing: border-box;
	padding: 0
}[type=number]::-webkit-inner-spin-button,[type=number]::-webkit-outer-spin-button {
	height: auto
}[type=search] {
	-webkit-appearance: textfield;
	outline-offset: -2px
}[type=search]::-webkit-search-decoration {
	-webkit-appearance: none
}

::-webkit-file-upload-button {
	-webkit-appearance: button;
	font: inherit
}

details {
	display: block
}

summary {
	display: list-item
}[hidden],template {
	display: none
}

请按前面所说将两个文件在同一个文件夹中。现在,你可以用浏览器打开html文件,输入测试代码试试了。

相关文章:

  • 【Linux】Ubuntu 24.04 LTS 安装 Hadoop-3.4.1
  • TCP | 序列号和确认号 [逐包分析] | seq / ack 详解
  • 视频播放器(watermelon Player)vue2使用体验(教程版)
  • Android Compose 框架按钮与交互组件模块源码深度剖析(二)
  • windows清除电脑开机密码,可保留原本的系统和资料,不重装系统
  • others-rustdesk远程
  • 带你从入门到精通——自然语言处理(十. BERT)
  • LeetCode hot 100 每日一题(13)——73. 矩阵置零
  • ubuntu22.04安装搜狗输入法保姆教程~
  • 《虚拟战场的对决》
  • DeepSeek R1 本地部署指南 (3) - 更换本地部署模型 Windows/macOS 通用
  • 通俗易懂搞懂@RequestParam 和 @RequestBody
  • 【LetMeFly】牛客-美团暑期2025-20250322-前两题和第三题的思路
  • AVL-树
  • 数据结构——第五章:树与二叉树
  • 建筑安全员考试:“高效记忆” 关键词引领的学习捷径
  • LLM - 重排序(Rerank)
  • 【2025 深圳大学-腾讯云程序设计竞赛(热身赛)】题解
  • Minine源码设计逻辑解析
  • Cursor从小白到专家
  • 云南舞蹈大家跳暨2025年牟定“三月会”昨天开幕
  • 对排出黑烟车辆出具合格报告,广州南沙一检测公司被罚十万元
  • “80后”李岩已任安徽安庆市领导
  • 中国与肯尼亚签署共同发展经济伙伴关系框架协定
  • 百台新车首秀上海车展,跨国车企联手中国技术开启智能化下半场
  • 猿辅导武汉公司一员工猝死,死者亲属:他原计划5月2日举行婚礼