How do I scroll in an overflowing flexbox child, when my cursor is anywhere on my webpage?

I’ve been trying to build a web developer portfolio website, and am working on a layout in which my body is a flexbox with two main elements:

  • On the left, a basic navigation bar

  • On the right, a content box with section IDs that tie to the navigation bar.

What’s working: The content box overflows the parent container as desired, and I can scroll through the content smoothly with my scroll wheel if my cursor is over the content box. With smooth scroll enabled, I can click on the IDs in the nav bar, and fluidly scroll to the relevant sections.

My problem: I want the content box to scroll when I move the scroll wheel, no matter where the cursor is on the webpage – e.g. in the navigation bar, in the margins – as opposed to only scrolling if the cursor is in the content box. Once enabled, I want to maintain smooth scrolling for navigation bar clicks and scroll wheel scrolls.

I’m hoping someone can help me work this out, as I am unfortunately out of ideas! It feels like there may be a simple solution here, that I can’t see yet. : )

What I have tried:

I tried adding event listeners to the content box element to 1) prevent the default scroll and 2) force the content box to scroll no matter where the cursor is. While this does enable me to scroll the content box with the cursor outside of the content box as desired, and maintains smooth scrolling for navigation bar clicks, I lose smooth scrolling for scroll wheel scrolls, which is undesirable.

I was expecting to be able to scroll the content box from anywhere, and maintain smooth scrolling for both nav bar clicks and scroll wheel scrolls.

I’ve included the faulty script in my HTML snip, so that you can uncomment it and see what I mean.

HTML:

<!DOCTYPE html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="./smoothscroll.css">
</head>

<body>

    <div class="staticSection">
        <nav>
            <ul>
                <li><a href="#about">ABOUT</a></li>
                <li><a href="#experience">EXPERIENCE</a></li>
                <li><a href="#projects">PROJECTS</a></li>
            </ul>
        </nav>
    </div>
    
    <div class = "scrollSection" id="srollSection">
        <div id="about">
            <h1>About</h1>
        </div>
        <div id="experience">
            <h1>Experience</h1>
        </div>
        <div id="projects">
            <h1>Projects</h1>
        </div>
    </div>

</body>

<!-- Faulty JS Script. I tried to force the wheel events into the scroll section. This does successfully force the scroll section to scroll when the cursor is not in the scroll section (e.g. in the nav bar), but the scrolls are very disjoint and non-fluid, resulting in a very poor user experience -->

<!-- <script>

    let mainContent = document.querySelector('.scrollSection');

    function preventDefaultScroll(event){
        event.preventDefault();
    }

    function enableFlexScroll(event){
        mainContent.scrollTop += event.deltaY;
    }

    document.addEventListener('wheel', preventDefaultScroll, { passive: false });
    document.addEventListener('wheel', enableFlexScroll, { passive: false });

</script> -->

</html>

CSS:

/* Flex container */

body{
    display: flex;
    height: 100vh;
    margin: 0 10%;
    overflow: hidden;
}

/* LHS Navigation */

.staticSection{
    width: 25%;
    background-color: antiquewhite;
}

/* RHS Scrolling Content */

.scrollSection{
    width: 75%;
    overflow-y: auto; 
    scroll-behavior: smooth;
}

#about, #experience, #projects{
    height: 500px;
    background-color: darkgray;
}

New contributor

Matt Mogan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật