因为最近在入门 html css javascript , 也会跟着书实践一下,代码都是很小的代码片段, 过后就丢弃了。 但是将来想用类似效果的时候就又忘记了,就想着在已有的 nextjs + react + tailwindcss 技术栈的网站上支持一下这个功能。
看到好多网站都有类似的效果,但是由于本人是个前端小白,所以在这里请教一下各位大神,有没有好的方案,尽可能简单,毕竟它就是用来写一点点代码片段,预览一下效果的“小玩具”。
感谢!
![]() |
1
vfs OP ![]() 看到这个网站做的就非常好: https://playcode.io/html5 , 不知道做到这样有多大难度
|
2
hausen 2 天前
感觉这种用 cursor 能做
|
3
wanf1020 2 天前
最简单的 innerHTML
|
![]() |
4
rimworld 2 天前
iframe 不就可以
|
5
radishzz 2 天前
嵌入 codepen ?
|
![]() |
6
molvqingtai 2 天前 ![]() |
![]() |
7
quietDown 2 天前
@molvqingtai 第一件见的这个,有点意思
|
![]() |
8
Plumbiu 2 天前
写过一篇博客: https://blog.plumbiu.top/posts/blog/implement-playground
简单点,下面这行代码就能用 ```ts import React, { createElement } from 'react' import { transform, Options } from 'sucrase' type Scope = Record<string, any> const transfromOptions: Options = { transforms: ['jsx', 'flow', 'imports'], } function evalCode(code: string, scope: Scope) { const _require = (k: keyof Scope) => { return scope[k] } const scopeKeys = Object.keys(scope) const scopeValues = scopeKeys.map((key) => scope[key]) const _exports: Record<string, any> = {} const fn = new Function('exports', 'require', ...scopeKeys, code) fn(_exports, _require, ...scopeValues) return _exports.default } interface PlaygroundPreviewProps { code: string scope?: Scope } export function Preview({ code, scope }: PlaygroundPreviewProps) { const evalScope: Scope = { react: React, React: React, ...(scope ?? {}), } const mainCode = transform(code, transfromOptions).code return createElement(evalCode(mainCode, evalScope)) } ``` |
![]() |
9
Plumbiu 2 天前
又看了一眼,单纯 html 可以用 innerHTML ,安全点用 iframe 做。
如果有 css 样式隔离,可以用 iframe 喝 ShadowRoot 。React 的 ShadowRoot 可以用这段代码 https://github.com/Plumbiu/blog/blob/main/src/components/function/Shadow.tsx |
![]() |
10
importmeta 2 天前 ![]() 楼上的都不是前端, VS Code 安这个 Live Server 插件就完事.
|
![]() |
11
importmeta 2 天前
@importmeta 我审题错误了... 嵌入 codepen codesanbox 都行
|
![]() |
12
pikko 2 天前
你要的就是漏上的 codepen / codesanbox
|
13
xe2vforesu 2 天前
iframe 就能实现,试试我这个
https://www.easydo.top/apps/webNew |
14
gsw945 2 天前 via Android
|
![]() |
15
UnluckyNinja 1 天前
尽可能简单那就是 codepen/jsfiddle 嵌入,最简单最省事。
|
![]() |
16
siweipancc 1 天前 via iPhone
娱乐圈……
|
![]() |
17
bapp 1 天前 via Android
data:text/html,<body oninput="i.srcdoc=h.value+'<style>'+c.value+'</style><script>'+j.value+'</script>'"><style>textarea,iframe{width:100%;height:50%}body{margin:0}textarea{width:33.33%;font-size:18}</style><textarea placeholder=HTML id=h></textarea><textarea placeholder=CSS id=c></textarea><textarea placeholder=JS id=j></textarea><iframe id=i>
|