Convert Figma logo to code with AI

protonet logojquery.inview

A jQuery plugin that adds a bindable 'inview' event for detecting when an element is scrolled into view.

1,674
489
1,674
41

Top Related Projects

10,382

Waypoints is a library that makes it easy to execute a function whenever you scroll to an element.

4,649

Get notified when a DOM element enters or exits the viewport. :eyes:

A simple and fast API to monitor elements as you scroll

Quick Overview

jQuery.inview is a jQuery plugin that adds an event which is triggered when an element enters or leaves the viewport. It's particularly useful for implementing lazy loading of images or infinite scrolling features in web applications.

Pros

  • Easy to implement and integrate with existing jQuery projects
  • Lightweight and performant
  • Customizable with options for offset and partial visibility
  • Supports both entering and leaving viewport events

Cons

  • Depends on jQuery, which may not be ideal for modern web development practices
  • Limited maintenance and updates in recent years
  • May have performance issues with a large number of elements
  • Lacks advanced features like intersection ratio or root margin found in newer APIs

Code Examples

  1. Basic usage:
$('.my-element').on('inview', function(event, isInView) {
  if (isInView) {
    // Element is now visible in the viewport
    $(this).addClass('visible');
  } else {
    // Element has left the viewport
    $(this).removeClass('visible');
  }
});
  1. Using with image lazy loading:
$('img.lazy').on('inview', function(event, isInView) {
  if (isInView) {
    var $img = $(this);
    $img.attr('src', $img.data('src')).removeClass('lazy');
  }
});
  1. Implementing infinite scroll:
$('#content-end').on('inview', function(event, isInView) {
  if (isInView) {
    // Load more content when the end of the content comes into view
    loadMoreContent();
  }
});

Getting Started

  1. Include jQuery and the jquery.inview.js script in your HTML:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="path/to/jquery.inview.js"></script>
  1. Add the inview event listener to your desired elements:
$(document).ready(function() {
  $('.my-element').on('inview', function(event, isInView) {
    if (isInView) {
      console.log('Element is now in view');
    } else {
      console.log('Element is out of view');
    }
  });
});
  1. Customize the behavior as needed for your specific use case.

Competitor Comparisons

10,382

Waypoints is a library that makes it easy to execute a function whenever you scroll to an element.

Pros of Waypoints

  • More feature-rich with support for horizontal scrolling and custom offsets
  • Better documentation and examples
  • Actively maintained with regular updates

Cons of Waypoints

  • Larger file size and potentially higher performance overhead
  • Steeper learning curve due to more complex API
  • Requires more setup for basic functionality

Code Comparison

jquery.inview:

$(element).on('inview', function(event, isInView) {
  if (isInView) {
    // Element is now visible in the viewport
  }
});

Waypoints:

var waypoint = new Waypoint({
  element: document.getElementById('element'),
  handler: function(direction) {
    // Element has entered the viewport
  }
});

Key Differences

  • jquery.inview is a lightweight plugin focused solely on detecting when elements enter or leave the viewport
  • Waypoints offers more advanced features like sticky elements and infinite scrolling
  • jquery.inview has a simpler API, making it easier to use for basic viewport detection
  • Waypoints provides more control over trigger points and callback execution

Use Cases

  • Choose jquery.inview for simple viewport detection with minimal setup
  • Opt for Waypoints when you need advanced scrolling features or more precise control over trigger points

Community and Support

  • jquery.inview has fewer stars and contributors on GitHub
  • Waypoints has a larger community and more third-party resources available
4,649

Get notified when a DOM element enters or exits the viewport. :eyes:

Pros of in-view

  • Lightweight and dependency-free, unlike jquery.inview which requires jQuery
  • Supports modern browsers with better performance
  • More actively maintained with recent updates

Cons of in-view

  • Less extensive browser support compared to jquery.inview
  • Fewer configuration options and customization features
  • May require more setup for complex use cases

Code Comparison

in-view:

inView('.selector')
  .on('enter', el => {
    el.classList.add('in-view');
  })
  .on('exit', el => {
    el.classList.remove('in-view');
  });

jquery.inview:

$('.selector').on('inview', function(event, isInView) {
  if (isInView) {
    $(this).addClass('in-view');
  } else {
    $(this).removeClass('in-view');
  }
});

Both libraries provide similar functionality for detecting when elements enter or exit the viewport. in-view uses a more modern, chainable API, while jquery.inview relies on jQuery's event system. in-view's approach is more flexible and allows for easier separation of concerns, but jquery.inview's implementation might be more familiar to developers accustomed to jQuery.

A simple and fast API to monitor elements as you scroll

Pros of scrollmonitor

  • More feature-rich, offering advanced functionality like custom offsets and locking
  • Better performance, especially for large numbers of elements
  • No jQuery dependency, making it more lightweight and versatile

Cons of scrollmonitor

  • Steeper learning curve due to more complex API
  • Less straightforward implementation for simple use cases
  • Requires more setup code compared to jquery.inview

Code Comparison

scrollmonitor:

var elementWatcher = scrollMonitor.create(element);
elementWatcher.enterViewport(function() {
    console.log('Element is in view');
});

jquery.inview:

$(element).on('inview', function(event, isInView) {
    if (isInView) {
        console.log('Element is in view');
    }
});

Key Differences

  • scrollmonitor provides more granular control over viewport events
  • jquery.inview has a simpler API, making it easier to use for basic scenarios
  • scrollmonitor offers better performance for tracking multiple elements
  • jquery.inview relies on jQuery, while scrollmonitor is standalone

Use Cases

  • Choose scrollmonitor for complex projects with many elements to track
  • Opt for jquery.inview for simpler projects or when already using jQuery
  • scrollmonitor is better suited for performance-critical applications
  • jquery.inview is ideal for quick implementations with minimal setup

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

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');

If you would like the event only to trigger once per element while the page is loaded, you can use the .one() method instead of .on():

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

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

NPM DownloadsLast 30 Days