Coder Social home page Coder Social logo

lc-body-scroll-locker's Introduction

lc-body-scroll-locker

Lock or unlock your body scroll.

Why I make another wheel?

I have already try a lot of way to lock body scroll. Includes belows:

  1. Set document and body height 100%.
  2. Prevent scroll and touch event. ...

But none of these works for me.
Finally, I find a best way to achieve and I implement in this package.
If you want to know the detail, see below Theory section.

Install

Use NPM or Yarn:

npm install lc-body-scroll-locker --save
yarn add lc-body-scroll-locker

Usage

Support TypeScript.

import bodyScrollLocker from 'lc-body-scroll-locker';
// lock body scroll
bodyScrollLocker.lock();
// unlock body scroll
bodyScrollLocker.unlock();
// see current lock status
console.log(bodyScrollLocker.isLocked);

Compatible

IE9+

Theory

The theory in this package is quite simple.
Lock:

  1. Remember the scroll top position.
  2. Set body position fixed and transform: translate3d(0, -top.px, 0)).

Unlock:

  1. Clean body position and transform value.
  2. Set scroll top position.

Issue

  1. Document.body should not have margin. Use padding instead as possible.

Test

  1. Run below command to start test server:
yarn serve
  1. Open http://localhost:8000/test/index.html

  2. Manual test: Scroll page and click page will toggle lock status.
    See console panel.

Author

JamesYin, https://www.lazycoffee.com

License

MIT

lc-body-scroll-locker's People

Contributors

elantion avatar

Watchers

James Cloos avatar

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.