Mouse Clock 1.2.0: container option; editor preview stays inside the play area

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tangsongdayoandClaude Opus 5.5 committed 2026-11-20 15:56:19 +08:00
1 parent 3f9ac3e1de
commit 7bf38ae86a
3 files changed
+55 -23

No files matched your search

+2 -1
View File
@@ -102,6 +102,7 @@ Medium、方格子、Notion、Facebook 這類平台不允許文章放自己的 J
| `data-lang` | `zh-TW` | 日期語言:`zh-TW`(2026年 十一月 20日 星期五)或 `en`(FRIDAY 20 NOVEMBER 2026) | | `data-lang` | `zh-TW` | 日期語言:`zh-TW`(2026年 十一月 20日 星期五)或 `en`(FRIDAY 20 NOVEMBER 2026) |
| `data-z-index` | `9999` | 疊放順序;時鐘被網站的選單蓋住時調大 | | `data-z-index` | `9999` | 疊放順序;時鐘被網站的選單蓋住時調大 |
| `data-reduced-motion` | `hide` | 使用者在系統開啟「減少動態」時:`hide` 不顯示,`show` 照常顯示 | | `data-reduced-motion` | `hide` | 使用者在系統開啟「減少動態」時:`hide` 不顯示,`show` 照常顯示 |
| `data-container` | (無) | 1.2.0 起。填 CSS 選擇器(例如 `#clock-box`)時,時鐘只在那個區塊裡顯示、不會跑出區塊,滑鼠或手指在區塊裡才會跟著動,離開後停在原地。不填就是跟著整個頁面(一般用法)。線上編輯器的試玩區就是用這個 |
### 用 `window.MouseClockOptions` 設定(1.1.0 起) ### 用 `window.MouseClockOptions` 設定(1.1.0 起)
@@ -124,7 +125,7 @@ window.MouseClockOptions = { hourColor: '#b5532f', lang: 'en' };
<button onclick="MouseClock.stop()">關閉時鐘</button> <button onclick="MouseClock.stop()">關閉時鐘</button>
``` ```
`MouseClock.start(設定)` 的設定名稱和上表相同,只是改成駝峰寫法(`data-hour-color` → `hourColor`)。重複呼叫 `start()` 會先關掉舊的再開新的。 `MouseClock.start(設定)` 的設定名稱和上表相同,只是改成駝峰寫法(`data-hour-color` → `hourColor`)。重複呼叫 `start()` 會先關掉舊的再開新的。`container` 除了選擇器,也可以直接給元素,例如 `MouseClock.start({ container: document.getElementById('clock-box') })`;區塊需要有高度。
--- ---
+7 -5
View File
@@ -59,7 +59,7 @@
main { min-width: 0; } main { min-width: 0; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; margin-bottom: 20px; } .card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; margin-bottom: 20px; }
.card h2 { margin: 0 0 8px; font-size: 1.15rem; } .card h2 { margin: 0 0 8px; font-size: 1.15rem; }
.play { min-height: 260px; display: grid; place-items: center; text-align: center; border: 2px dashed var(--line); border-radius: 12px; color: var(--muted); padding: 20px; } .play { min-height: 320px; position: relative; touch-action: pan-y; display: grid; place-items: center; text-align: center; border: 2px dashed var(--line); border-radius: 12px; color: var(--muted); padding: 20px; }
.play strong { color: var(--text); font-size: 1.1rem; display: block; } .play strong { color: var(--text); font-size: 1.1rem; display: block; }
.bgswitch { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 10px; font-size: .9rem; } .bgswitch { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 10px; font-size: .9rem; }
.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin: 4px 0 10px; } .tabs { display: flex; gap: 6px; flex-wrap: wrap; margin: 4px 0 10px; }
@@ -73,7 +73,7 @@
@media (max-width: 760px) { @media (max-width: 760px) {
.layout { grid-template-columns: minmax(0, 1fr); padding: 12px 16px; } .layout { grid-template-columns: minmax(0, 1fr); padding: 12px 16px; }
.panel { position: static; max-height: none; } .panel { position: static; max-height: none; }
.play { min-height: 220px; } .play { min-height: 380px; }
} }
</style> </style>
</head> </head>
@@ -142,10 +142,11 @@
<main> <main>
<section class="card" aria-labelledby="playTitle"> <section class="card" aria-labelledby="playTitle">
<h2 id="playTitle">試玩區</h2> <h2 id="playTitle">試玩區</h2>
<div class="play"> <div class="play" id="play">
<div> <div>
<strong>移動滑鼠,或用手指在畫面上滑動</strong> <strong>移動滑鼠,或用手指在畫面上滑動</strong>
時鐘會跟著你。電腦在游標右下方,手機在手指上方。 時鐘會跟著你。電腦在游標右下方,手機在手指上方。<br>
這裡的時鐘只在框內預覽;放到你的網站上會跟著整個頁面移動。
</div> </div>
</div> </div>
<div class="bgswitch" role="radiogroup" aria-label="預覽底色"> <div class="bgswitch" role="radiogroup" aria-label="預覽底色">
@@ -193,7 +194,7 @@
<a href="https://code.tsdylab.com/tangsongdayo/mouse-clock">原始碼與授權</a> ・ <span id="ver"></span> <a href="https://code.tsdylab.com/tangsongdayo/mouse-clock">原始碼與授權</a> ・ <span id="ver"></span>
</footer> </footer>
<script src="mouse-clock.js?v=1.1.0" data-manual></script> <script src="mouse-clock.js?v=1.2.0" data-manual></script>
<script> <script>
(function () { (function () {
'use strict'; 'use strict';
@@ -317,6 +318,7 @@
function restart() { function restart() {
var o = clone(state); var o = clone(state);
o.reducedMotion = 'show'; // 編輯器一律顯示預覽 o.reducedMotion = 'show'; // 編輯器一律顯示預覽
o.container = $('play'); // 只在試玩區內預覽(輸出的程式碼不含這項)
MC.start(o); MC.start(o);
} }
+46 -17
View File
@@ -1,5 +1,5 @@
/*! /*!
* 滑鼠立體時鐘 Mouse Clock 1.1.0 * 滑鼠立體時鐘 Mouse Clock 1.2.0
* https://code.tsdylab.com/tangsongdayo/mouse-clock * https://code.tsdylab.com/tangsongdayo/mouse-clock
* *
* 原作:Silly Clock 3 by Kurt Grigg * 原作:Silly Clock 3 by Kurt Grigg
@@ -25,7 +25,8 @@
dateSpeed: 0.015, // 日期環旋轉速度(原版 0.06) dateSpeed: 0.015, // 日期環旋轉速度(原版 0.06)
lang: 'zh-TW', // 日期語言:'zh-TW' 或 'en' lang: 'zh-TW', // 日期語言:'zh-TW' 或 'en'
zIndex: 9999, zIndex: 9999,
reducedMotion: 'hide' // 使用者系統設定「減少動態」時:'hide' 不顯示、'show' 照常顯示 reducedMotion: 'hide', // 使用者系統設定「減少動態」時:'hide' 不顯示、'show' 照常顯示
container: null // 1.2.0:只在這個元素(或 CSS 選擇器)裡顯示,時鐘不會跑出它的範圍
}; };
var NAMES = { var NAMES = {
@@ -54,6 +55,10 @@
return null; return null;
} }
var box = o.container;
if (typeof box === 'string') box = document.querySelector(box);
if (box && box.nodeType !== 1) box = null;
var names = NAMES[o.lang] || NAMES['zh-TW']; var names = NAMES[o.lang] || NAMES['zh-TW'];
var now = new Date(); var now = new Date();
var D = names.format(now.getFullYear(), names.months[now.getMonth()], now.getDate(), names.days[now.getDay()]).split(''); var D = names.format(now.getFullYear(), names.months[now.getMonth()], now.getDate(), names.days[now.getDay()]).split('');
@@ -62,22 +67,36 @@
var n = Face.length, Split = 360 / n, Dsplit = 360 / D.length; var n = Face.length, Split = 360 / n, Dsplit = 360 / D.length;
var R = o.size, HandLen = R / 4.5, HandY = -7, HandX = -2.5; var R = o.size, HandLen = R / 4.5, HandY = -7, HandX = -2.5;
var xmouse = window.innerWidth / 2, ymouse = window.innerHeight / 2, currStep = 0; var xmouse = window.innerWidth / 2, ymouse = window.innerHeight / 2, currStep = 0;
// 容器模式:時鐘所有字(含日期環、字寬)與容器邊緣的距離
var padL = Math.max(R * 1.5, R + 7) + 4, padR = Math.max(R * 1.5 + 10, R + 16) + 4;
var oldPosition = null;
if (box) {
if (getComputedStyle(box).position === 'static') { oldPosition = box.style.position; box.style.position = 'relative'; }
xmouse = box.clientWidth / 2; ymouse = box.clientHeight / 2;
}
function clampAxis(v, size) {
if (size < padL + padR) return (padL + size - padR) / 2;
return Math.min(Math.max(v, padL), size - padR);
}
var isTouch = !!(window.matchMedia && window.matchMedia('(hover: none)').matches); var isTouch = !!(window.matchMedia && window.matchMedia('(hover: none)').matches);
function zeros(len) { var a = []; for (var i = 0; i < len; i++) a.push(0); return a; } // 一般模式從左上角飛進來(原版行為);容器模式從容器中央開始,不會跑出範圍
var y = zeros(n), x = zeros(n), Y = zeros(n), X = zeros(n); function zeros(len, v) { var a = []; for (var i = 0; i < len; i++) a.push(v || 0); return a; }
var Dy = zeros(D.length), Dx = zeros(D.length), DY = zeros(D.length), DX = zeros(D.length); var sy = box ? clampAxis(ymouse, box.clientHeight) : 0, sx = box ? clampAxis(xmouse, box.clientWidth) : 0;
var y = zeros(n, sy), x = zeros(n, sx), Y = zeros(n, sy), X = zeros(n, sx);
var Dy = zeros(D.length, sy), Dx = zeros(D.length, sx), DY = zeros(D.length, sy), DX = zeros(D.length, sx);
var root = document.createElement('div'); var root = document.createElement('div');
root.className = 'mouse-clock'; root.className = 'mouse-clock';
root.setAttribute('aria-hidden', 'true'); root.setAttribute('aria-hidden', 'true');
var css = document.createElement('style'); var css = document.createElement('style');
css.textContent = css.textContent =
'.mouse-clock>div{position:fixed;top:0;left:0;pointer-events:none;text-align:center;line-height:1;' + '.mouse-clock>div{position:' + (box ? 'absolute' : 'fixed') + ';top:0;left:0;pointer-events:none;text-align:center;line-height:1;' +
'will-change:transform;z-index:' + o.zIndex + ';margin:0;padding:0;}' + 'will-change:transform;z-index:' + o.zIndex + ';margin:0;padding:0;}' +
'.mouse-clock .mc-face,.mouse-clock .mc-date{width:10px;height:10px;font:10px Arial,sans-serif;}' + '.mouse-clock .mc-face,.mouse-clock .mc-date{width:10px;height:10px;font:10px Arial,sans-serif;}' +
'.mouse-clock .mc-hand{width:16px;height:16px;font:bold 16px Arial,sans-serif;}'; '.mouse-clock .mc-hand{width:16px;height:16px;font:bold 16px Arial,sans-serif;}';
root.appendChild(css); root.appendChild(css);
if (box) root.style.cssText = 'position:absolute;inset:0;overflow:hidden;pointer-events:none;';
function make(chars, cls, color) { function make(chars, cls, color) {
return chars.map(function (c) { return chars.map(function (c) {
@@ -94,23 +113,31 @@
var elH = make(H, 'mc-hand', o.hourColor); var elH = make(H, 'mc-hand', o.hourColor);
var elM = make(M, 'mc-hand', o.minuteColor); var elM = make(M, 'mc-hand', o.minuteColor);
var elS = make(S, 'mc-hand', o.secondColor); var elS = make(S, 'mc-hand', o.secondColor);
document.body.appendChild(root); (box || document.body).appendChild(root);
function place(el, top, left) { el.style.transform = 'translate(' + left + 'px,' + top + 'px)'; } function place(el, top, left) { el.style.transform = 'translate(' + left + 'px,' + top + 'px)'; }
// Pointer Events 分辨滑鼠和手指(手機點擊後補發的滑鼠事件不會干擾) // Pointer Events 分辨滑鼠和手指(手機點擊後補發的滑鼠事件不會干擾)
// 容器模式:只接受容器範圍內的指標,座標換成相對容器
function aim(cx, cy, dx, dy) {
if (box) {
var r = box.getBoundingClientRect();
if (cx < r.left || cx > r.right || cy < r.top || cy > r.bottom) return;
cx -= r.left + box.clientLeft; cy -= r.top + box.clientTop;
}
ymouse = cy + dy;
xmouse = cx + dx;
}
function onPointer(e) { function onPointer(e) {
var touch = e.pointerType !== 'mouse'; var touch = e.pointerType !== 'mouse';
if (isTouch && !touch) return; if (isTouch && !touch) return;
ymouse = e.clientY + (touch ? o.touchOffsetY : o.offsetY); aim(e.clientX, e.clientY, touch ? o.touchOffsetX : o.offsetX, touch ? o.touchOffsetY : o.offsetY);
xmouse = e.clientX + (touch ? o.touchOffsetX : o.offsetX);
} }
// 手指滑動捲頁時瀏覽器會中斷 pointermove,改由 touchmove 接手 // 手指滑動捲頁時瀏覽器會中斷 pointermove,改由 touchmove 接手
function onTouch(e) { function onTouch(e) {
var t = e.touches[0]; var t = e.touches[0];
if (!t) return; if (!t) return;
ymouse = t.clientY + o.touchOffsetY; aim(t.clientX, t.clientY, o.touchOffsetX, o.touchOffsetY);
xmouse = t.clientX + o.touchOffsetX;
} }
document.addEventListener('pointerdown', onPointer, { passive: true }); document.addEventListener('pointerdown', onPointer, { passive: true });
document.addEventListener('pointermove', onPointer, { passive: true }); document.addEventListener('pointermove', onPointer, { passive: true });
@@ -138,15 +165,16 @@
function tick() { function tick() {
if (document.hidden) return; // 分頁在背景時不計算 if (document.hidden) return; // 分頁在背景時不計算
var i; var i, ty = ymouse, tx = xmouse;
Dy[0] = Math.round(DY[0] += (ymouse - DY[0]) * o.speed); if (box) { ty = clampAxis(ty, box.clientHeight); tx = clampAxis(tx, box.clientWidth); }
Dx[0] = Math.round(DX[0] += (xmouse - DX[0]) * o.speed); Dy[0] = Math.round(DY[0] += (ty - DY[0]) * o.speed);
Dx[0] = Math.round(DX[0] += (tx - DX[0]) * o.speed);
for (i = 1; i < D.length; i++) { for (i = 1; i < D.length; i++) {
Dy[i] = Math.round(DY[i] += (Dy[i - 1] - DY[i]) * o.speed); Dy[i] = Math.round(DY[i] += (Dy[i - 1] - DY[i]) * o.speed);
Dx[i] = Math.round(DX[i] += (Dx[i - 1] - DX[i]) * o.speed); Dx[i] = Math.round(DX[i] += (Dx[i - 1] - DX[i]) * o.speed);
} }
y[0] = Math.round(Y[0] += (ymouse - Y[0]) * o.speed); y[0] = Math.round(Y[0] += (ty - Y[0]) * o.speed);
x[0] = Math.round(X[0] += (xmouse - X[0]) * o.speed); x[0] = Math.round(X[0] += (tx - X[0]) * o.speed);
for (i = 1; i < n; i++) { for (i = 1; i < n; i++) {
y[i] = Math.round(Y[i] += (y[i - 1] - Y[i]) * o.speed); y[i] = Math.round(Y[i] += (y[i - 1] - Y[i]) * o.speed);
x[i] = Math.round(X[i] += (x[i - 1] - X[i]) * o.speed); x[i] = Math.round(X[i] += (x[i - 1] - X[i]) * o.speed);
@@ -162,6 +190,7 @@
document.removeEventListener('pointermove', onPointer); document.removeEventListener('pointermove', onPointer);
document.removeEventListener('touchmove', onTouch); document.removeEventListener('touchmove', onTouch);
if (root.parentNode) root.parentNode.removeChild(root); if (root.parentNode) root.parentNode.removeChild(root);
if (oldPosition !== null) box.style.position = oldPosition;
running = null; running = null;
} }
}; };
@@ -179,7 +208,7 @@
var current = document.currentScript; var current = document.currentScript;
// 1.1.0:在載入前設定 window.MouseClockOptions = {...} 也可以調整(data-* 屬性優先) // 1.1.0:在載入前設定 window.MouseClockOptions = {...} 也可以調整(data-* 屬性優先)
var preset = window.MouseClockOptions; var preset = window.MouseClockOptions;
window.MouseClock = { version: '1.1.0', start: start, stop: function () { if (running) running.stop(); }, defaults: DEFAULTS }; window.MouseClock = { version: '1.2.0', start: start, stop: function () { if (running) running.stop(); }, defaults: DEFAULTS };
// 加上 data-manual 就不自動啟動,改由 MouseClock.start({...}) 啟動 // 加上 data-manual 就不自動啟動,改由 MouseClock.start({...}) 啟動
if (!(current && current.dataset && current.dataset.manual !== undefined)) { if (!(current && current.dataset && current.dataset.manual !== undefined)) {