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,工具箱中有七個積木。請參閱這個即時示範。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間: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"]],["上次更新時間: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"]]