
【Bubble】Xのように、スクロールすると隠れるフッターメニューを作ろう
X(旧Twitter)をアプリでみていると、あまりに自然すぎてなかなか気が付かないんですが、スクロールするとフッターメニューが消えていますよね。で、上にスクロールしたらまた再表示されますよね。
あれすごい便利。
これをBubbleでもやってみよう!
Bubble実装方法
まずはいつもどおりFloating groupを設置してください。
floating groupについては割愛します。
vertically float relative toはBottomにしてください。フッターメニューなので、当然ね。
で、次にhtmlエレメントを設置します。
これは画面のどこでもいいですが、ブラウザに読み込ませる必要があるのでVisible on page loadについてはチェックをいれておいてください。
僕はhtmlが乱雑になっているとややこしいのでpopupにすべて収納しています。
で、そこに以下のコードをコピペしてください。
<style>
#scroll-footer {
transform: translateY(0) !important;
transition: transform 0.22s ease !important;
}
html.scroll-footer-hidden #scroll-footer:not(:focus-within) {
transform: translateY(calc(100% + 16px)) !important;
pointer-events: none;
}
@media (prefers-reduced-motion: reduce) {
#scroll-footer {
transition: none !important;
}
}
</style>
<script>
(() => {
// コードが再実行された場合は、以前の監視を解除する
window.scrollFooterController?.abort();
const controller = new AbortController();
window.scrollFooterController = controller;
const root = document.documentElement;
const HIDE_DISTANCE = 24; // 下へ何px進んだら隠すか
const SHOW_DISTANCE = 12; // 上へ何px戻ったら表示するか
const TOP_ZONE = 40; // 先頭から何pxまで常に表示するか
// 上端・下端でのバウンドを、スクロール範囲内に補正する
const getY = () => {
const page = document.scrollingElement || root;
const maxY = Math.max(0, page.scrollHeight - root.clientHeight);
return Math.min(maxY, Math.max(0, window.scrollY));
};
let previousY = getY();
let direction = 0;
let distance = 0;
root.classList.remove('scroll-footer-hidden');
window.addEventListener('scroll', () => {
const currentY = getY();
const difference = currentY - previousY;
previousY = currentY;
if (currentY <= TOP_ZONE) {
root.classList.remove('scroll-footer-hidden');
direction = 0;
distance = 0;
return;
}
if (difference === 0) return;
const nextDirection = difference > 0 ? 1 : -1;
// 方向が変わったら、移動距離を数え直す
if (nextDirection !== direction) {
direction = nextDirection;
distance = 0;
}
distance += Math.abs(difference);
if (direction === 1 && distance >= HIDE_DISTANCE) {
root.classList.add('scroll-footer-hidden');
distance = 0;
} else if (direction === -1 && distance >= SHOW_DISTANCE) {
root.classList.remove('scroll-footer-hidden');
distance = 0;
}
}, { passive: true, signal: controller.signal });
})();
</script> Floating groupにはID Attributeの部分に
scroll-footer
と入力してください。
#は入力しないでください。
これで完成です。あっという間ですね。

htmlエレメントのなかにはhtml以外も埋め込める
Bubbleのhtmlエレメントは、htmlエレメントという名前ですが、CSSもJavascriptも書き込めます。
実際、この記事で紹介したhtmlエレメントには、CSSもJavascriptも含まれています。
スクロール速度などを調整するには、このなかにある数字を調整してください。
スクロール関連の他記事
スクロール一つとっても、拘ろうと思えばどこまでも拘れます。
これは、Bubbleじゃできないよぉというところもhtmlエレメントによってできたりします!もし分からないことあったら、ご連絡ください!
最後に
Bubbleの受託開発行っています!
一つのWebシステムにつき、60万円で開発いたします!
もちろん、開発途中のものでも、ご相談いただければお受けいたします。
X:
ぜひお願いします!