Skip to main content

Content & marketing

Parallax Horizontal Drift code

Copy the implementation or inspect every file that belongs to this component unit.

resources/views/components/ui/parallax-horizontal-drift.blade.php

@props([
    'image',
    'alt' => '',
    'eyebrow' => null,
    'title' => null,
    'description' => null,
    'distance' => 120,
    'overlayClass' => 'bg-gradient-to-r from-gray-950/85 via-gray-950/25 to-gray-950/70',
])

@php
    /*
    | Scroll-driven horizontal parallax. The oversized image pans sideways as
    | the section crosses the viewport.
    */
@endphp

<section
    x-data="{
        x: 0,
        reduced: window.matchMedia('(prefers-reduced-motion: reduce)').matches,
        update() {
            if (this.reduced) {
                this.x = 0;

                return;
            }

            const rect = $el.getBoundingClientRect();
            const progress = Math.max(-1, Math.min(1, (window.innerHeight / 2 - rect.top) / window.innerHeight));
            this.x = progress * {{ (int) $distance }};
        },
    }"
    x-init="update()"
    @scroll.window.passive="update()"
    {{ $attributes->merge(['class' => 'relative isolate flex min-h-screen overflow-hidden bg-gray-950']) }}
>
    <img
        src="{{ $image }}"
        alt="{{ $alt }}"
        @if ($alt === '') aria-hidden="true" @endif
        class="pointer-events-none absolute inset-y-0 left-1/2 -z-20 h-full w-[130%] max-w-none -translate-x-1/2 scale-105 object-cover will-change-transform motion-reduce:w-full motion-reduce:scale-100"
        style="transform: translate3d(-50%, 0, 0) scale(1.05);"
        :style="reduced ? 'transform: translate3d(-50%, 0, 0) scale(1);' : `transform: translate3d(calc(-50% + ${x}px), 0, 0) scale(1.05);`"
    >

    <div class="absolute inset-0 -z-10 {{ $overlayClass }}" aria-hidden="true"></div>
    <div class="absolute inset-x-0 bottom-0 -z-10 h-40 bg-gradient-to-t from-gray-950 to-transparent" aria-hidden="true"></div>

    <div class="mx-auto flex w-full max-w-7xl items-center justify-end px-6 py-16 sm:px-8 lg:px-10">
        <div class="max-w-2xl text-right">
            @if ($eyebrow)
                <x-ui.badge variant="brand" icon="arrows-left-right" class="bg-white/12 text-white ring-1 ring-white/20 backdrop-blur dark:bg-white/12 dark:text-white">
                    {{ $eyebrow }}
                </x-ui.badge>
            @endif

            @if ($title)
                <h2 class="mt-5 text-4xl font-semibold tracking-tight text-white text-balance sm:text-6xl">{{ $title }}</h2>
            @endif

            @if ($description)
                <p class="ml-auto mt-5 max-w-2xl text-base leading-7 text-gray-100/85 text-pretty sm:text-lg">{{ $description }}</p>
            @endif

            @isset($actions)
                <div class="mt-8 flex flex-col items-end gap-3 sm:flex-row sm:justify-end">{{ $actions }}</div>
            @endisset
        </div>
    </div>
</section>