LoginSignup
3
3

More than 3 years have passed since last update.

<1画面分スクロールさせるボタン>を追加するブックマークレット

Posted at
javascript:(()=>{const e=document.createElement("div");e.setAttribute("class","🔘"),e.textContent="",document.body.appendChild(e);const t=document.createElement("div");t.setAttribute("class","🔘"),t.textContent="",document.body.appendChild(t);let o=document.getElementsByClassName("🔘");o=[].slice.call(o);const n=.8*window.innerHeight,s=()=>{const e=o.indexOf(event.target);window.scrollBy({left:0,top:n*(1-2*e),behavior:"smooth"})};document.documentElement.style.cssText="scroll-behavior:smooth;";for(let e=0;e<2;e++)o[e].style.cssText="z-index:99999;position:fixed;"+`bottom:${60*e+100}px;`+"right:20px;color:#222;font-size:10pt;height:50px;width:50px;text-align:center;line-height:50px;border-radius:50px;border:1px solid #aaa;background:#eee;opacity:.5;user-select:none;-webkit-user-select:none;",o[e].addEventListener("click",s)})();

これは何?

実行すると右下にボタンが現れ、押すと1画面分スクロールします。
縦に長いサイトをスマホでみる時に使うと重宝するやつです。
フッタに広告やメニューなどが固定されている場合を考慮して、スクロール量と表示位置を調整するといいでしょう。

説明

次のようなことをやっています。

  • divを2つ用意。これをボタンとして使う。
  • スクロール量を、画面の高さの80%に指定しとく。
  • タップされたボタンのindexによって符号を決定しスクロールさせる、という関数をつくる。
  • ボタンにスタイルとイベントリスナーを指定する。
scrollButton.js
(() => {
// ------------------------------------------------------------------

// BTN_0
const btn_0 = document.createElement('div');
btn_0.setAttribute('class', '🔘');
btn_0.textContent = '';
document.body.appendChild(btn_0);
// BTN_1
const btn_1 = document.createElement('div');
btn_1.setAttribute('class', '🔘');
btn_1.textContent = '';
document.body.appendChild(btn_1);
// BTNs
let BTNs = document.getElementsByClassName('🔘');// HTMLCollection
BTNs = [].slice.call(BTNs);// => array

// px to scrollBy: スクロール量
const Y = window.innerHeight * 0.8;
// scroll
const scrolll = () => {
    const idx = BTNs.indexOf(event.target);
    // 🔘[0] => scroll_down
    // 🔘[1] => scroll_up
    window.scrollBy({
        left: 0,
        top: Y * (1 - 2 * idx),
        behavior: 'smooth'
    });
}
// smooth scroll
document.documentElement.style.cssText = 'scroll-behavior:smooth;';

// BTNs: style and EventListener
for(let i=0; i<2; i++){
    BTNs[i].style.cssText = 
        'z-index:99999;' +
        'position:fixed;' + 
        `bottom:${i * 60 + 100}px;` +
        'right:20px;' + 
        'color:#222;' + 
        'font-size:10pt;' + 
        'height:50px;' +
        'width:50px;' + 
        'text-align:center;' +
        'line-height:50px;' + 
        'border-radius:50px;' +
        'border:1px solid #aaa;' +
        'background:#eee;' +
        'opacity:.5;' + 
        'user-select:none;' + 
        '-webkit-user-select:none;';
    BTNs[i].addEventListener('click', scrolll);
}

// ------------------------------------------------------------------
})();

まとめ

  • 青空文庫のXHTML版で使うといいかも、と今ふと思いました。
  • Safari などブラウザによっては、するするとスムーズにスクロールしないことがあります。

最後までお読みいただき有り難うございます。お疲れ様でした。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3