<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>固定サイドバー</title>
    <style>
        body {
            margin: 0;
            font-family: Arial, sans-serif;
        }
        .header {
            background: #333;
            color: white;
            padding: 15px;
            text-align: center;
            position: fixed;
            width: 100%;
            top: 0;
            left: 0;
            z-index: 1000;
        }
        .container {
            display: flex;
            max-width: 1200px;
            margin: 80px auto 0;
            padding: 20px;
            align-items: flex-start;
        }
        .content {
            flex: 3;
            padding: 20px;
        }
        .sidebar {
            flex: 1;
            padding: 20px;
            background: #f4f4f4;
            height: auto;
        }
        .blog-post {
            padding: 15px;
            border-bottom: 1px solid #ddd;
            height: 200px;
        }
        .sticky {
            position: fixed;
            top: 60px;
            width: calc(25% - 40px);
            left: auto;
            right: calc((100% - 1200px) / 2);
        }
    </style>
</head>
<body>
    <div class="header">メインメニュー</div>
    <div class="container">
        <div class="content">
            <div class="blog-post">ブログ記事 1</div>
            <div class="blog-post">ブログ記事 2</div>
            <div class="blog-post">ブログ記事 3</div>
            <div class="blog-post">ブログ記事 4</div>
            <div class="blog-post">ブログ記事 5</div>
            <div class="blog-post">ブログ記事 6</div>
            <div class="blog-post">ブログ記事 7</div>
            <div class="blog-post">ブログ記事 8</div>
            <div class="blog-post">ブログ記事 9</div>
        </div>
        <div class="sidebar" id="sidebar">
            <h3>カテゴリー</h3>
            <ul>
                <li>カテゴリ 1</li>
                <li>カテゴリ 2</li>
                <li>カテゴリ 3</li>
                <li>カテゴリ 4</li>
                <li>カテゴリ 5</li>
            </ul>
        </div>
    </div>
    <script>
        window.addEventListener('scroll', function() {
            const sidebar = document.getElementById('sidebar');
            const header = document.querySelector('.header');
            const sidebarTop = sidebar.offsetTop;
            const headerHeight = header.offsetHeight;
            const scrollTop = window.scrollY;
            const container = document.querySelector('.container');
            const containerRight = (window.innerWidth - container.offsetWidth) / 2;

            if (scrollTop >= sidebarTop - headerHeight) {
                sidebar.classList.add('sticky');
                sidebar.style.right = `${containerRight}px`;
            } else {
                sidebar.classList.remove('sticky');
                sidebar.style.right = "auto";
            }
        });
    </script>
</body>
</html>
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.