All Projects β†’ patik β†’ Within Viewport

patik / Within Viewport

Licence: isc
JavaScript utility that determines whether an element is completely within the browser viewport

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Within Viewport

Viewport Checker
Little utility to detect if elements are currently within the viewport πŸ”§
Stars: ✭ 596 (+144.26%)
Mutual labels:  viewport, browser
Peertransfer
πŸ“¦ β€’ Send a file p2p and e2e encrypted in your browser using WebRTC.
Stars: ✭ 238 (-2.46%)
Mutual labels:  browser
Instascan
HTML5 QR code scanner using your webcam
Stars: ✭ 2,657 (+988.93%)
Mutual labels:  browser
Endless
iOS web browser with a focus on security and privacy
Stars: ✭ 237 (-2.87%)
Mutual labels:  browser
Use Position
🌍 React hook usePosition() for fetching and following a browser geolocation
Stars: ✭ 230 (-5.74%)
Mutual labels:  browser
Popsicle
Simple HTTP requests for node and the browser
Stars: ✭ 238 (-2.46%)
Mutual labels:  browser
Run Series
Run an array of functions in series
Stars: ✭ 223 (-8.61%)
Mutual labels:  browser
Pax
The fastest JavaScript bundler in the galaxy.
Stars: ✭ 2,626 (+976.23%)
Mutual labels:  browser
Vue Firestore
☁️ Cloud Firestore binding in realtime with Vuejs
Stars: ✭ 239 (-2.05%)
Mutual labels:  browser
Getsy
A simple browser/client-side web scraper.
Stars: ✭ 238 (-2.46%)
Mutual labels:  browser
Supercookie
πŸ’­ Inspiration
Stars: ✭ 3,630 (+1387.7%)
Mutual labels:  browser
Jshistory Cn
πŸ‡¨πŸ‡³ γ€ŠJavaScript δΊŒεεΉ΄γ€‹δΈ­ζ–‡η‰ˆ
Stars: ✭ 3,686 (+1410.66%)
Mutual labels:  browser
Extension Create
Create modern cross-browser extensions with no build configuration.
Stars: ✭ 167 (-31.56%)
Mutual labels:  browser
Chromely
Build HTML Desktop Apps on .NET/.NET Core/.NET 5 using native GUI, HTML5, JavaScript, CSS
Stars: ✭ 2,728 (+1018.03%)
Mutual labels:  browser
Browserhtml
Experimental Servo browser built in HTML
Stars: ✭ 2,609 (+969.26%)
Mutual labels:  browser
Oasis
Free, open-source, peer-to-peer social application that helps you follow friends and discover new ones on Secure Scuttlebutt (SSB).
Stars: ✭ 225 (-7.79%)
Mutual labels:  browser
Svg2pdf.js
A javascript-only SVG to PDF conversion utility that runs in the browser. Brought to you by yWorks - the diagramming experts
Stars: ✭ 231 (-5.33%)
Mutual labels:  browser
Gngr
a cross-platform browser focussed on privacy.
Stars: ✭ 238 (-2.46%)
Mutual labels:  browser
Nightwatch Cucumber
[DEPRECATED] Cucumber.js plugin for Nightwatch.js.
Stars: ✭ 243 (-0.41%)
Mutual labels:  browser
Caesar
An HTTP based RAT (Remote Administration Tool) that allows you to remotely control devices from your browser
Stars: ✭ 240 (-1.64%)
Mutual labels:  browser

Within Viewport

Determine whether elements are within the viewport

Includes:

  • A standalone, plain JavaScript function, withinviewport()
  • AMD and Node/CommonJS support
  • Optional jQuery plugin with handy selectors and shorthand methods

All of the above offer the same features.

Note

Although this plugin is still actively maintained, it will eventually be made obsolete by the Intersection Observer API. You can check the current state of browser compatibility at caniuse.com. Meanwhile, withinviewport will continue to work on current and legacy browsers.

Installation

AMD, Node.js, CommonJS

NPM

npm install withinviewport

And then:

var withinviewport = require('withinviewport');

Bower:

bower install within-viewport

Traditional include

Standalone (no jQuery):

<script src="withinviewport.js"></script>

jQuery plugin:

<script src="withinviewport.js"></script>
<script src="jquery.js"></script>
<script src="jquery.withinviewport.js"></script>

Usage

Basic

// Returns true if the element is entirely within view of the window
var elem = document.getElementById('#myElement');
withinviewport(elem);

Advanced

// Test against only some sides of the window for faster performance
withinviewport(elem, {sides: 'left'});
// Pick another element to act as the viewport (instead of `window`)
withinviewport(elem, {container: document.getElementById('myElem')});
// Define your own viewport crop by specifying thresholds for each side
// Example: element is at least 12px inside the top and right of the viewport
withinviewport(elem, {top: 12, right: 12});

For more options, see Settings section below.

Shorthand notation

// These will use the default thresholds; see 'Settings' section below
withinviewport(elem, 'bottom right');
withinviewport.left(elem);

jQuery plugin

Usage

Basic

// Returns true if the element is entirely within the viewport
$('#myElement').is(':within-viewport');
// Returns a jQuery object of all <div>s that are within the viewport
$('div').withinviewport();

Advanced

There are shorthand selectors and methods for testing against only one edge of the viewport.

// Returns true if the element is within the left edge of the viewport
// Also works with 'top', 'right', and 'bottom'
$('#myElement').is(':within-viewport-left');
// Returns a jQuery collection of all <div>s within the left edge of the viewport
$('div').withinviewportleft();
// Same as above, but only elements that are at least 12px inside the left edge
$('div').withinviewportleft({left: 12});

These shortcuts will result in slightly better performance if you're testing hundreds or thousands of elements.

Live updating

If you're looking to keep tabs on elements' whereabouts at all times, you can bind to the window's resize and scroll events. Instead of scroll, I recommend using James Padolsey's scrollStop event since firing on every window.scroll event will bring your UI to its knees.

$(window).on('resize scrollStop', function() {
    // Your code here...

    // Example:
    $('div')
        // Momentarily declare all divs out of the viewport...
        .removeClass('within-viewport');
        // Then filter them to reveal which ones are still within it
        .filter(':within-viewport')
            .addClass('within-viewport');
});

A future version will allow you to fire custom events when elements pass in and out of the viewport.

Settings

This applies to both the jQuery plugin and standalone function.

Use the object withinviewport.defaults to define your page's practical viewport compared to the actual browser window.

Custom viewport element

If you want to test whether an element is within a scrollable parent element (e.g. which has overflow: auto), assign the parent element to the container property:

$('.child-element').withinviewport({
    container: $('.parent-element')
});

Custom boundaries

For example, a fixed header with a height of 100px that spans the entire width of the page effectively lowers the viewport by 100px from the top edge of the browser window:

withinviewport.defaults.top = 100;

If you only care about some edges of the viewport, you can specify them to improve performance:

withinviewport.defaults.sides = 'left bottom';

You can also pass settings on the fly to temporarily override the defaults:

withinviewport(elem, {sides:'left bottom', left: 40});
$('div').withinviewport({sides:'left bottom', left: 40});

Individual elements may have their own settings embedded in a data attribute using object notation. These will override both the defaults any any settings passed to the function on the fly (like the example above).

<div data-withinviewport-settings="{sides: 'left', top: 40}">

You can specify negative threshold values to allow elements to reside outside the viewport.

Browser Support

  • IE 7(?) and higher
  • All the others except Opera Mini
    • Tested in the latest stable Chrome, Firefox, Safari, and IE
    • No "new" JavaScript or quirky techniques are employed so it should work in all other modern browsers not specifically mentioned above

What's Next

Please note that the camel case withinViewport method name is deprecated and is no longer supported as of version 2.0.0.

  • Option to fire events when elements pass in and out of the viewport
  • Test against Firefox 3.6, Safari 5.0.1

Credit

Within Viewport is inspired by these similar utilities which only reflect whether an element is at least partially in view:

License

Have fun with it β€” ISC. See included LICENSE file.

Author

Craig Patik, patik.com & @craigpatik

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].