Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="section" id="section1">
    <h1>Section 1</h1>
</div>
<div class="section" id="section2">
    <h1>Section 2</h1>
</div>
<div class="section" id="section3">
    <h1>Section 3</h1>
</div>

<div class="scroll-info" id="scrollInfo">
    <span id="currentSectionNumber">1</span> / <span id="totalSections">3</span> Sections
</div>

<div class="scroll-down" id="scrollDown" data-action="down">
    <span>Scroll Down</span>
    <div class="arrow">↓</div>
  
 
</div>
              
            
!

CSS

              
                /* 既存の CSS スタイルはそのまま使用します */
body, html {
    margin: 0;
    padding: 0;
    scroll-behavior: smooth; /* スムーズスクロールを有効化 */
}

.section {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 3em;
    scroll-margin-top: 50px; /* スクロール時の位置調整 */
}

#section1 {
    background-color: #ffcccc;
}

#section2 {
    background-color: #ccffcc;
}

#section3 {
    background-color: #ccccff;
}

.scroll-info {
    position: fixed;
    top: 20px;
    right: 20px;
    background-color: rgba(255, 255, 255, 0.8);
    padding: 10px;
    border: 1px solid #ccc;
    border-radius: 5px;
    font-size: 1.2em;
}

.scroll-down {
    position: fixed;
    bottom: 20px;
    right: 20px;
    cursor: pointer;
    text-align: center;
    font-size: 1.2em;
    z-index: 1000;
}

.scroll-down .arrow {
    font-size: 2em;
    margin-top: 5px;
}



              
            
!

JS

              
                document.addEventListener('DOMContentLoaded', function () {
    const scrollDownButton = document.getElementById('scrollDown');
    const sections = document.querySelectorAll('.section');
    const currentSectionNumberElement = document.getElementById('currentSectionNumber');
    const totalSectionsElement = document.getElementById('totalSections');

    // 初期化時にセクションの数を表示
    totalSectionsElement.textContent = sections.length;

    function updateButton() {
        const lastSection = sections[sections.length - 1];
        const lastSectionRect = lastSection.getBoundingClientRect();
        let currentSectionIndex = -1;

        sections.forEach((section, index) => {
            const rect = section.getBoundingClientRect();
            if (rect.top <= window.innerHeight / 2 && rect.bottom >= window.innerHeight / 2) {
                currentSectionIndex = index + 1; // インデックスを1から始めるために+1を追加
            }
        });

        if (currentSectionIndex === -1) {
            currentSectionIndex = 1; // ページ初期読み込み時のデフォルト
        }

        currentSectionNumberElement.textContent = currentSectionIndex;

        if (lastSectionRect.bottom <= window.innerHeight && currentSectionIndex === sections.length) {
            scrollDownButton.classList.remove('visible'); // スクロールダウンボタンを非表示にする
            scrollDownButton.innerHTML = '<span>Scroll Top</span><div class="arrow">↑</div>';
            scrollDownButton.dataset.action = 'up';
            scrollDownButton.addEventListener('click', function () {
                window.scrollTo({ top: 0, behavior: 'smooth' });
            });
            scrollDownButton.classList.add('visible'); // ページ末尾でのみスクロールトップボタンを表示する
        } else {
            scrollDownButton.classList.remove('visible'); // スクロールダウンボタンを非表示にする
            scrollDownButton.innerHTML = '<span>Scroll Down</span><div class="arrow">↓</div>';
            scrollDownButton.dataset.action = 'down';
            scrollDownButton.addEventListener('click', function () {
                let currentSectionIndex = -1;

                sections.forEach((section, index) => {
                    const rect = section.getBoundingClientRect();
                    if (rect.top <= window.innerHeight / 2 && rect.bottom >= window.innerHeight / 2) {
                        currentSectionIndex = index;
                    }
                });

                if (currentSectionIndex >= 0 && currentSectionIndex < sections.length - 1) {
                    const nextSection = sections[currentSectionIndex + 1];
                    const offset = nextSection.offsetTop;
                    window.scrollTo({ top: offset, behavior: 'smooth' });
                } else if (currentSectionIndex === sections.length - 1) {
                    const lastSection = sections[currentSectionIndex];
                    const offset = lastSection.offsetTop;
                    window.scrollTo({ top: offset, behavior: 'smooth' });
                }
            });
        }
    }

    document.addEventListener('scroll', function () {
        updateButton();
    });

    // 初期状態を設定
    updateButton();
});

//20240615 0242
//以下 改善点
//スマホサイズになった時にScrollDownが表示さないようにする
//スマホサイズになった時はページ末尾になった時にScrollTOPが表示されるようにする
              
            
!
999px

Console