TipsBlocks Web Technique Library

PRACTICAL WEB TIP

← ライブラリへ戻る

The Little Box — 最初のひとくち

菓子のパッケージが開くように動く、冒頭へ戻るボタン。

デザインプレビュー

まず形と大きさを確認。実際の動作は下のLive Demoで試せます。

造形とホバー
サイトでの実寸イメージ

Live Demo

枠内の「動作を試す」を押すか、枠の中をスクロールしてください。右下にボタンが現れ、クリックすると先頭へ戻ります。

Source Code

必要なコードをコピーして実装に利用できます。

HTML
<!-- TB-2080 / Back to Top: insert before closing body (inside footer for TB-2083) -->
<button class="tb-btt bt2080" data-backtop type="button" aria-label="ページの先頭、チョコレートの紹介へ戻る"><span class="mini-flap"></span><span class="box-brand">CACAO<br>LETTER</span><span class="box-return">BACK TO<br>PAGE ONE</span></button>
CSS
/* TB-2080 : paste only this CSS into the target page. */
.tb-btt{border:0;cursor:pointer;z-index:30;-webkit-tap-highlight-color:transparent;touch-action:manipulation;transition:transform .35s,opacity .3s,background .3s,color .3s,box-shadow .3s;pointer-events:none;opacity:0;visibility:hidden}.tb-btt.is-visible{pointer-events:auto;opacity:1;visibility:visible}.tb-btt:focus-visible{outline:3px solid #f9af4b;outline-offset:5px}.only-at-end{opacity:1;visibility:visible;pointer-events:auto}
.bt2080{position:fixed;right:20px;bottom:22px;width:111px;height:104px;background:#a3495c;border:4px double #e8b4a3;color:#fff5e8;box-shadow:0 11px 25px #4f2b3155;transform:rotate(5deg);display:flex;flex-direction:column;align-items:center;justify-content:space-evenly}
.mini-flap{position:absolute;top:-12px;left:0;width:100%;height:20px;background:#d68e95;clip-path:polygon(0 100%,50% 0,100% 100%);transition:transform .4s;transform-origin:bottom}
.box-brand{font:bold 14px/1.05 Georgia,serif;letter-spacing:.03em}
.box-return{font:800 8px/1.3 Arial;letter-spacing:.1em}
.bt2080:hover{background:#b76a7d;transform:translateY(-8px) rotate(0)}
.bt2080:hover .mini-flap{transform:rotateX(75deg)}
@media (max-width:700px){.bt2080{width:86px;height:88px;right:12px;bottom:12px}.box-brand{font-size:12px}.box-return{font-size:7px}}
@media(prefers-reduced-motion:reduce){.tb-btt,.tb-btt *{animation-duration:.01ms!important;transition-duration:.01ms!important}}
JavaScript
(()=>{'use strict';const control=document.querySelector('[data-backtop]');if(!control)return;const reduced=matchMedia('(prefers-reduced-motion: reduce)');const footer=control.classList.contains('only-at-end');let busy=false;function refresh(){busy=false;const max=Math.max(1,document.documentElement.scrollHeight-window.innerHeight);const p=Math.min(1,scrollY/max);document.documentElement.style.setProperty('--page-progress',p.toFixed(4));if(!footer){const show=scrollY>145;control.classList.toggle('is-visible',show);control.tabIndex=show?0:-1;control.setAttribute('aria-hidden',String(!show));}const number=document.querySelector('[data-progress-number]');if(number)number.textContent=Math.round(p*100)+'%';}function onscroll(){if(!busy){busy=true;requestAnimationFrame(refresh)}}addEventListener('scroll',onscroll,{passive:true});addEventListener('resize',onscroll,{passive:true});control.addEventListener('click',()=>window.scrollTo({top:0,behavior:reduced.matches?'instant':'smooth'}));refresh();})();

How to Use

HTMLのボタンをbody末尾付近へ設置し、CSSを読み込んでください。JavaScriptもdeferで読み込みます。実ページでスクロール後の表示・先頭復帰・スマートフォンでの位置を確認してください。

Use Case

クラフトチョコレートブランド

Technical Notes

ホバー非対応端末でも主要操作ができるようにし、キーボードフォーカス時の表示と動き軽減設定を確認してください。

Mobile:モバイル幅でもクリック・タップ可能な大きさを確認。

Browser:現行Chrome・Firefox・Edge・Safariを想定。

Reduced Motion:prefers-reduced-motionに配慮。