"Fix MacBook Scroll Glitches with Lenis & CSS"
"Fix MacBook Scroll Glitches with Lenis & CSS"

Force MacBook Trackpad to Scroll Horizontally Only with Lenis Scroll

Solve unwanted vertical scrolling on MacBook trackpads using Lenis Scroll, CSS overscroll, and optimized JS event handling.6 min


When designing a smooth scrolling experience in web development, tools like Lenis Scroll are incredibly useful. However, if your users rely on a MacBook trackpad, you might face a tricky challenge: unintended vertical scrolling when trying to scroll horizontally.

Often, this occurs because MacBook trackpads have sensitive multi-directional input detection. Even slight finger movement up or down can result in accidental vertical scrolling. So how can developers ensure horizontal scrolling prevails, preventing vertical scrolling unless the movement clearly calls for it?

Understanding Lenis Scroll and Why It Matters

Lenis Scroll has quickly become a favorite among developers for smooth, natural-feeling scroll animations. It helps developers control scroll speed, easing, and transitions effortlessly. Because Lenis uses optimized requestAnimationFrame loops, it ensures high performance and responsiveness.

Scrolling direction—vertical or horizontal—isn’t just aesthetic; it’s directly tied to user experience. When scrolling is intuitive, visitors have a better browsing experience and are more likely to remain engaged on your site. However, different input devices handle scrolling differently. A handheld mouse wheel behaves differently from a laptop’s touchpad, and Apple’s MacBook trackpad introduces unique behavior.

MacBook’s trackpad accurately detects multiple simultaneous movements, enabling advanced gestures and super-smooth swiping. The downside? This sensitivity may create conflict with specialized scroll libraries like Lenis, accidentally triggering unwanted vertical scrolling during horizontal movements.

Why MacBook Trackpads Cause Scrolling Issues with Lenis

MacBook trackpads are highly accurate and sensitive. A user’s slight deviation in gesture is interpreted as a command to scroll vertically and horizontally simultaneously. This precision is ideal in most cases but problematic with customized horizontal scrolling experiences.

When a Lenis Scroll page is intended primarily for horizontal scrolling, minor vertical motion input will negatively impact the intended scrolling experience. Imagine you’re scrolling through a horizontally designed product showcase. As you browse images, accidental vertical scrolling takes you off-course, disrupting the seamless experience and resulting in frustration and decreased engagement.

Resolving this issue demands more robust scroll control, specifically techniques that prevent or significantly limit vertical scrolling unless explicitly intended by the user.

Controlling Vertical Scrolling Effectively

Fortunately, a few clever techniques using CSS and JavaScript can provide significant control over scrolling interactions. Some effective solutions are:

  • Using CSS overscroll-behavior property to prevent default scrolling behaviors.
  • Employing event.preventDefault() to block vertical scroll interactions.
  • Modifying JavaScript scroll event listeners based on thresholds to detect intentional scrolling directions and ignore accidental inputs.

Let’s explore these approaches further and see how they specifically apply to Lenis Scroll.

1. Controlling Scroll with CSS

An essential property you should know is overscroll-behavior. Setting this CSS property to “none” prevents unwanted default behaviors:


.scroll-container {
  overscroll-behavior-y: none;
}

This simple trick ensures that subtle vertical movements don’t unintentionally trigger vertical scrolling.

2. Intercepting Scroll Events with JavaScript

For more granular control, intercept scroll events directly via JavaScript. By measuring scrolling interaction through WheelEvent, you can determine directional thresholds and enact precise scrolling behavior modifications:


document.querySelector('.scroll-container').addEventListener('wheel', (event) => {
  if(Math.abs(event.deltaX) < Math.abs(event.deltaY)){
    // More vertical than horizontal scroll: block vertical effects
    event.preventDefault();
  }
}, { passive: false });

This logic ensures vertical scroll events trigger only if they're clearly intentional and exceed horizontal scroll values significantly, ensuring horizontal priority.

3. Customizing Scroll Logic with Lenis Scroll

Lenis Scroll itself allows precise manipulation within its API, letting you set constraints or behavior guidelines easily. For horizontal pages, configure Lenis carefully:


const lenis = new Lenis({
  orientation: 'horizontal',
  smooth: true,
  smoothWheel: true,
  syncTouch: true,
});

lenis.on('scroll', (e) => {
  // Additional behavior adjustments, if required
});

Combining Lenis with custom JavaScript event listeners ensures optimal scrolling experience tailored specifically for MacBook trackpad users.

Implementing Lenis Scroll with Customized Settings

Integrating Lenis Scroll with custom settings is straightforward:

  • Include Lenis Scroll from CDN or via npm package.
  • Initialize Lenis directly and customize required properties as per your layout needs.
  • Adjust duration, easing curves, and damping to control the scrolling motion explicitly tailored to MacBook trackpad movements.

For detailed setup, here's an easy-to-follow initialization example:


// Smooth horizontal scrolling with Lenis
const lenis = new Lenis({
  duration: 1.2,
  easing: t => 1 - Math.pow(1 - t, 3),
  orientation: 'horizontal',
  gestureOrientation: 'horizontal',
  smoothWheel: true,
});

function runLoop(time) {
  lenis.raf(time);
  requestAnimationFrame(runLoop);
}

requestAnimationFrame(runLoop);

Customize the duration, easing, and other properties according to your design preferences, and test these values to find the perfect balance.

Testing and Optimization: Getting it Right for Everyone

Creating a smooth, predictable scrolling experience requires thorough testing. Don't limit yourself to your own device—it's crucial to test your modifications across various input devices and browsers. Tools like BrowserStack or Sauce Labs help ensure scrolling solutions behave consistently across multiple platforms.

Remember to frequently solicit feedback, pinpointing issues users encounter. Minor tweaks to scroll duration or sensitivity can dramatically improve responsiveness. Review your analytics or user feedback regularly to refine and optimize your scroll behavior effectively.

Common issues you might encounter include jittery scrolling or performance drops. Carefully adjusting easing functions or scroll thresholds usually resolves such problems quickly.

The Advantages of Horizontal Scrolling with Lenis

Implementing horizontal scrolling with Lenis Scroll properly optimized for MacBook trackpads has multiple benefits. Users enjoy smoother navigation, less distraction from unwanted movements, and a more engaging browsing experience.

A correctly implemented horizontal scroll not only increases session duration but also positively impacts conversion rates and engagement metrics. Whether showcasing portfolios, product galleries, or immersive storytelling experiences, smooth horizontal scrolling brings substantial value to a website.

If you're building interactive scrolling features in your JavaScript projects, consider exploring more JavaScript tutorials and guides for additional insights.

What's your experience been using Lenis Scroll in your projects—have you successfully managed horizontal scroll issues on MacBook trackpads? Share your thoughts or any unique challenges you tackled in the comments below.


Like it? Share with your friends!

Shivateja Keerthi
Hey there! I'm Shivateja Keerthi, a full-stack developer who loves diving deep into code, fixing tricky bugs, and figuring out why things break. I mainly work with JavaScript and Python, and I enjoy sharing everything I learn - especially about debugging, troubleshooting errors, and making development smoother. If you've ever struggled with weird bugs or just want to get better at coding, you're in the right place. Through my blog, I share tips, solutions, and insights to help you code smarter and debug faster. Let’s make coding less frustrating and more fun! My LinkedIn Follow Me on X

0 Comments

Your email address will not be published. Required fields are marked *