Giter Site home page Giter Site logo

phucbm / cuajs Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 195 KB

A holy cua library to create a horizontal scrolling page.

Home Page: https://cuajs.netlify.app

License: MIT License

JavaScript 79.65% HTML 14.10% SCSS 6.25%
horizontal-scrolling lenis smooth-scrolling

cuajs's Introduction

banner

npm minified jsdelivr Netlify Status

A holy cua library to create a horizontal scrolling page.

(Cua is a Vietnamese word for crab)

Introduction

See Demo.

  • Respect the native behavior of the browser's scrollbar, which means accessibility is ensured.
  • Default scrolling events are remained. No DOM manipulation.
  • Keyboard, trackpad, mouse wheel, and touch screens are tested.
  • Able to switch to vertical scrolling on mobile devices.
  • Smooth scrolling experience with Lenis.

Installation

CuaJs has no dependency. However, it is recommended to install Lenis to enable smooth scrolling and gain a better experience.

Download

Using a package manager:

npm i @studio-freight/lenis
npm i cuajs
import Lenis from '@studio-freight/lenis'
import "cuajs";

Using CDN:

<!-- Lenis smooth scroll -->
<script src="https://cdn.jsdelivr.net/gh/studio-freight/lenis@1/bundled/lenis.min.js"></script>

<!-- CuaJs -->
<script src="https://cdn.jsdelivr.net/gh/phucbm/[email protected]/dist/cua.min.js"></script>

Init

HTML setup:

<!-- [data-cua] must be defined -->
<div data-cua>
    <!-- Each child of wrapper must have [data-cua-section] -->
    <section data-cua-section>
        Your content

        <!-- Scroll to specific positions with [data-cua-to] -->
        <button data-cua-to="start">To the start</button>
        <button data-cua-to="end">To the end</button>
        <button data-cua-to="#second-section">To the long-section</button>
    </section>

    <section data-cua-section id="second-section">
        <!-- Vertical scrollable div must have [data-cua-vertical-scroll] -->
        <div data-cua-vertical-scroll>
            Content with vertical scroll
        </div>
    </section>
</div>

Note The value of [data-cua-to] could be number for pixels, string for CSS selector or keyword (start, end,...). See detail at scrollTo()

Options

Attribute Type Default Description
wrapper DOM element undefined Required. Wrapper element.
smoothScroll boolean true Enable smooth scroll
verticalBreakpoint number 1024 Switch to vertical layout mode when window.innerWidth <= 1024
smoothVerticalScroll boolean true Enable smooth scroll for vertical layout mode
keyScrollDistance number 200 Distance to scroll on each key press (px)
keyScroll boolean true Enable navigate by a arrow key
onScrollableContent function undefined Callback on each scrollable content
// init with options
const instance = CuaJs.init({
    wrapper: document.querySelector('.wrapper')
});

Add options via HTML

<div data-cua='{"verticalBreakpoint":"1024"}'>
</div>

Events

Assign onScroll event:

CuaInstance.on('onScroll', (data) => {
    console.log(data.axis, data.progress);

    // more info
    console.log(data);
});

Deployment

# Install
npm i

# Run dev server
npm run dev

# Build dev site
npm run build

# Generate production files
npm run prod

License

MIT License

Copyright (c) 2023 PHUCBM

cuajs's People

Contributors

phucbm avatar vandangnhathung avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

cuajs's Issues

Active section when scrolling horizontal

When scrolling horizontally, then scrolling to the end of the section, we must scroll for a while to active the next section. But this distance is not the same between sections. Can we check these distances or make it an option? Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.