Blockly 于 2025 年 11 月 10 日移至 Raspberry Pi Foundation!请阅读博文和常见问题解答。
固定大小的工作区
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
将 Blockly 放入网页的最简单方法是将其注入到空的“div”标记中。
1. 获取代码
以最适合您应用的方式获取代码。
2. 定义区域
在网页正文中的某个位置添加一个空的 div 并设置其大小:
<div id="blocklyDiv" style="height: 480px; width: 600px;"></div>
3. 注入工作区
定义工具箱结构:
const toolbox = {
"kind": "flyoutToolbox",
"contents": [
{
"kind": "block",
"type": "controls_if"
},
{
"kind": "block",
"type": "controls_repeat_ext"
},
{
"kind": "block",
"type": "logic_compare"
},
{
"kind": "block",
"type": "math_number"
},
{
"kind": "block",
"type": "math_arithmetic"
},
{
"kind": "block",
"type": "text"
},
{
"kind": "block",
"type": "text_print"
},
]
}
最后,调用以下内容将 Blockly 注入到您定义的 div 中。
const workspace = Blockly.inject('blocklyDiv', {toolbox: toolbox});
workspace 变量目前未使用,但稍后当用户想要保存块或生成代码时,它将变得非常重要。如果将多个 Blockly 实例注入到同一网页上,请确保每个返回的工作区都存储在不同的变量中。
现在,您可以在浏览器中测试该网页。您应该会看到 Blockly 的编辑器填充了 div,工具箱中有七个块。以下是实时演示。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-11-07。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-11-07。"],[],["To add Blockly to a webpage, first, obtain the necessary code. Next, create an empty `div` element in the page's body and set its dimensions (e.g., `id=\"blocklyDiv\"`, `height: 480px`, `width: 600px`). Define the toolbox structure using JSON format, specifying the block types. Finally, use `Blockly.inject('blocklyDiv', {toolbox: toolbox})` to insert Blockly into the `div`, storing the result in a `workspace` variable for later use in saving blocks or code generation.\n"]]