Giter VIP home page Giter VIP logo

bodyscrollkiller.js's Introduction

bodyscrollkiller.js

Small utility to prevent webpage body to scroll.

Intro

Sometimes you need to prevent your webpage to scroll, mostly when you want to use a modal dialog box, lightbox or similar. On all inside tags, prevent scrollig is simple - just add scroll eventhandler and prevent default avtion.

But page scroll is different. Browsers themselves scroll the whole page and do it differently. Some scroll BODY, some scroll HTML tag. But more importantly, page scroll cannot be prevented in no other way than setting that scrolling tag overflow settings to 'hidden'. Unfortunately there's a ugly side effect - when scrollbar(s) disappear and you have centered page contents (as common approach is), the page shifts right.

To compensate that right shift bodyscrollkiller.js measures your browser's scrollbar width and adds that width temporarily to the right margin of the scrollig tag. No shift anymore.

As a bonus, viewport scrolling on touch webkit can be turned off too.

bodyscrollkiller.js is written in vanilla Javascript and has no dependancies.

Usage

Include bodyscrollkiller.js:

<script src="path/to/bodyscrollkiller.js" type="text/javascript"></script>

To disable page scroll:

atirip.bodyScrollOff()

To enable page scroll:

atirip.bodyScrollOn()

To check status:

atirip.bodyScroll()

returns true if scrollbars are on, false if off

Internet Explorer

IE is a special beast (as always). IE rounds rectangles (tags) differenlty from others, if you have content wrapper in body centered (margin: 0 auto) then small adjustment is needed sometimes.

To disable page scroll in IE :

atirip.bodyScrollOff(wrapper)

where wrapper is vanilla DOM Node of your wrapper tag, if you use jQuery, then its likely something like this: $('#wrapper')[0]

Demo

Simple demo https://raw.github.com/atirip/bodyscrollkiller.js/master/scroll.html
Click once to turn scrolling off, click again to turn it back on.

Compatible

Tested on Firefox 16, Chrome 23, Opera 12 on Mountain Lion and Windows, IE 8, IE 7 on Windows (Windows XP under Parallels 7).

TODO

  • Windows Phone support, I have no idea if it works or not

Contact me

For support, remarks and requests, please mail me at [email protected].

License

Copyright (c) 2012 Priit Pirita, released under the MIT license.

bodyscrollkiller.js's People

Contributors

atirip avatar

Stargazers

Hadj H. 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.