Giter Club home page Giter Club logo

jquery.inview's Introduction

Element 'inview' Event Plugin

Event that is fired as soon as an element appears in the user's viewport.

Usage

The script makes use of the new $.contains method - so it will only work with jQuery 1.8 upwards. If you need to use it with older versions of jQuery, drop a comment, and I'll post an alternative.

The event will only fire when the element comes in to view of the viewport, and out of view. It won't keep firing if the user scrolls and the element remains in view.

The variable after the event argument indicates the visible state in the viewport.

$('div').on('inview', function(event, isInView) {
  if (isInView) {
    // element is now visible in the viewport
  } else {
    // element has gone out of viewport
  }
});

To stop listening for the event - simply unbind:

$('div').off('inview');

Remember you can also bind once:

$('div').one('inview', fn);

Live events

Yep, inview events can also be used with .on/.delegate methods. Please note that this could slow down your app when the selector is too complex and/or matches a huge set of elements. The following code snippet only loads images when they appear in the browser's viewport.

// Assuming that all images have set the 'data-src' attribute instead of the 'src'attribute
$("body").on("inview", "img[data-src]", function() {
  var $this = $(this);
  $this.attr("src", $this.attr("data-src"));
  // Remove it from the set of matching elements in order to avoid that the handler gets re-executed
  $this.removeAttr("data-src");
});

Use cases

  • Reduce http requests and traffic on server by loading assets (images, javascript, html, ...) only when they are visible to the user
  • Endless scrolling (twitter-like)
  • Tracking (eg. to see whether a user has read an entire article)
  • ...

Browser Compatibility

The Test Suite succeeds in the following browsers that were tested:

  • Firefox 3+
  • Safari 3+
  • Chrome 7+
  • Opera 10+
  • IE 6+
  • Mobile Safari on iPad 4.2.2

jquery.inview's People

Contributors

tiff avatar nugged avatar jrrudolph avatar naltatis avatar

Watchers

Thomas GILLES 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.