A ScrollSpy library for detecting enter/exit of elements in the viewport when the user scrolls

Overview

jquery-scrollspy

A jQuery plugin for detecting enter/exit of elements in the viewport when the user scrolls.

New Features

Added a couple new features:

  • May 2014 - optional window offset, allows for things like floating navs
  • Feb 2014 - supports window resize
  • Nov 2013 - throttles scrollspy events so that event handles only fire every 100 milliseconds

Usage

$('.tile').on('scrollSpy:enter', function() {
	console.log('enter:', $(this).attr('id'));
});

$('.tile').on('scrollSpy:exit', function() {
	console.log('exit:', $(this).attr('id'));
});

$('.tile').scrollSpy();

// or you could do this:
// $.scrollSpy($('.tile'));
// or this
// $('.tile').each(function(i, element) {
// 		$.scrollSpy(element);
// });

Window Resize

Use the scrollSpy:winSize event for watching window resize. This fires any time the window is resized by the user.

$.winSizeSpy().on('scrollSpy:winSize', funcy)

Contributions

Please provide pull requests for additional features. Test cases would be most weclome!

Thank you contributors:

Comments
  • Solved - SpyScroll not updated when i loaded new element from AJAX

    Solved - SpyScroll not updated when i loaded new element from AJAX

    Great and simple plugin :D

    here is my case, i have one class at first, then i loaded new element, but the scrollspy not updated. My new element didnt counted as scrollspy element. Any idea how to get this working?

    Thanks in advance

    opened by farizsofyan-inmagine 7
  • onScroll: throttled only once, always perform after the DOM is ready

    onScroll: throttled only once, always perform after the DOM is ready

    The following changes are made:

    • onScroll is throttled only once (otherwise two throttles happen independently of each other)
    • not only one, but every onScroll is delayed until the DOM is ready (and throttled to ensure that onScroll is not called several times when the DOM becomes ready)
    • each $.scrollSpy(...) call causes one onScroll call (throttled and DOM-delayed) regardless of the isSpying state (otherwise the second call and later $.scrollSpy(...) calls do not immediately trigger scrollSpy:enter on elements that are already “above the fold”)

    The latter is actually a bugfix.

    opened by Mithgol 6
  • Option for custom sized viewport rectangle for detecting elements

    Option for custom sized viewport rectangle for detecting elements

    The entire viewport of the browser is currently used as the field; elements that enter this area have "entered" to use the terminology of this library.

    I'm in need of offsets here - I would like to say that elements only enter when they've been scrolled half way up the viewport. This is not possible with the current design of code, and would probably need a bit of refactoring.

    Preferably this would be something you could define in the options when calling the scrollSpy() method.

    enhancement 
    opened by ericandrewlewis 2
  • Support for div  `overflow-y: scroll;`

    Support for div `overflow-y: scroll;`

    thanks for this great plugin. I just tested it, but unfortunately it doesn't work on content which is within a div with style overflow-y: scroll; maybe because the viewport sit's on window? any idea how to get this working??

    enhancement 
    opened by rngtng 2
  • Adjusting offset

    Adjusting offset

    Is there a way to set an offset of the element before/after entering the screen?

    I am having trouble with spying on elements which are shorter than the window height and the next element enters - then the next element takes precendence over the element that was already in screen and didn't exit

    opened by guyisra 1
  • Example file inclusion fix

    Example file inclusion fix

    You have a boo-boo in the example file.

    You need to change:

        <script src="../scroll-spy.js"></script>
    

    to:

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

    Not a big deal. Any good developer can detect and fix this on his own.

    opened by campuscodi 1
  • Add event for centre of view

    Add event for centre of view

    Hi - great lib nice that materialize have started to use it. So I found you as I wanted an event for when the item is in the middle of the viewport. It almost feels faster to do it myself but would prefer to levarage and already in use lib ..thanks

    opened by landed1 0
  • Add offsets support for altering viewport

    Add offsets support for altering viewport

    Adding an option to provide offsets to how the viewport frame is calculated. As an example, this is useful when using a navbar that hides a part of the viewport.

    opened by eithanshavit 0
  • Error with installing the package as a repository using npm

    Error with installing the package as a repository using npm

    @thesmart I have added this package to my package.json file

    "dependencies": {
      ...
      "jquery-scrollspy": "https://github.com/thesmart/jquery-scrollspy.git"
      ...
    }
    

    when I run npm install there is an error npm ERR! premature close and this error because the package doesn't have a package.json file name, would you please add it ?

    opened by heshamabdallah 0
  • scrollSpy triggers even when the element is not within the viewport

    scrollSpy triggers even when the element is not within the viewport

    Hi I am using the following html:

    `

    ` and css: ` #section1, #section2 { height: 100%; width: 100%; } `

    and using the scrollSpy plugin as follows: ` $('#section2').on('scrollSpy:enter', function() { alert("scrolled in"); });

    $('#section2').on('scrollSpy:exit', function() {
                  alert("scrolled out");
    });
    
    $('#section2').scrollSpy();
    

    `

    When the page loads, even though #section2 is not visible the alert is shown by the browser. How can I avoid this? Also, if increase the height to 101% this issue is not occuring.

    opened by shad-k 0
  • Version inconsistent problem

    Version inconsistent problem

    Hello @thesmart , I am the member of cdnjs project. We want to host this library. Please help me confirm the version on github. I saw the version of scrollspy.js is 0.1.2 in the release of 0.1.3. Thanks!

    https://github.com/cdnjs/cdnjs/issues/8520

    opened by kennynaoh 1
  • Fix sizzle selector error when using jQuery 2.2.0

    Fix sizzle selector error when using jQuery 2.2.0

    The lines of code using attribute selectors to match the id do not work with the latest jQuery:

    $('a[href=#' + visible[0].attr('id') + ']')
    

    In my case, the error thrown is: Error: Syntax error, unrecognized expression: a[href=#objective]

    Simply changing the code to match the following correctly using " fixes this:

    $('a[href="#' + visible[0].attr('id') + '"]')
    

    I am happy to raise a PR to fix this as I feel it is quite important to address since Materialise CSS uses your plugin.

    opened by clocked0ne 0
Owner
John Smart
Formerly a principal engineer at Figma
John Smart
A tiny JavaScript library to enable CSS animations when user scrolls.

A tiny JavaScript library to enable CSS animations when user scrolls. This library allows you to toggle one or more classes on an element when it appe

Matthieu Bué 35 Nov 24, 2022
🦉The jQuery plugin "Stick to me" allows you to create exit-popups on your web page, so you can capture visitors just as they are about to leave.

?? The jQuery plugin "Stick to me" allows you to create exit-popups on your web page, so you can capture visitors just as they are about to leave. How

Guilherme Assemany 18 Feb 10, 2022
Create a deep copy of a set of matched elements with the dynamic state of all form elements copied to the cloned elements.

jq-deepest-copy FUNCTION: Create a deep copy of a set of matched elements while preserving the dynamic state of any matched form elements. Example Use

Michael Coughlin 5 Oct 28, 2022
Grupprojekt för kurserna 'Javascript med Ramverk' och 'Agil Utveckling'

JavaScript-med-Ramverk-Laboration-3 Grupprojektet för kurserna Javascript med Ramverk och Agil Utveckling. Utvecklingsguide För information om hur utv

Svante Jonsson IT-Högskolan 3 May 18, 2022
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
A Jquery plugin that allows user to enter multiple emails using one input field

multi-emails A Jquery plugin that allows user to enter multiple emails using one input field Usage $("...").multiEmails() Options color textColor font

null 1 Aug 26, 2022
A JQuery Implementation of David Walsh's MooTools scrollspy

jquery-scrollspy This library is no longer actively supported or maintained. For similar functionality checkout these other libraries: https://github.

Sam Alexander 443 Dec 13, 2022
ScrollSpy in pure JavaScript

VanillaJS ScrollSpy ScrollSpy in pure JavaScript. Browser Support IE 10+ ✔ Latest ✔ Latest ✔ Latest ✔ Latest ✔ Installation $ npm install vanillajs-sc

Eder Sampaio 46 Dec 13, 2022
jQuery easy ticker is a news ticker like plugin, which scrolls the list infinitely. It is highly customizable, flexible with lot of features and works in all browsers.

jQuery Easy Ticker plugin jQuery easy ticker is a news ticker like plugin which scrolls a list infinitely. It is highly customizable, flexible with lo

Aakash Chakravarthy 208 Dec 20, 2022
Simple scrollspy without jQuery, no dependencies

Simple Scrollspy Simple scrollspy is a lightweight javascript library without jQuery, no dependencies. It is used to make scrollspy effect for your me

Nguyen Huu Kim 57 Dec 13, 2022
Library to download binary files from GitHub releases detecting the correct platform.

Dbin TypeScript library to download binary files from GitHub releases detecting the correct platform. Example: import dbin from "https://deno.land/x/d

Óscar Otero 7 Oct 4, 2022
Device.js is a JavaScript library to detect device, viewport, and browser information using plain JavaScript.

Device.js Device.js is a JavaScript library to detect device, viewport, and browser information using plain JavaScript. Compatibility Works with all m

Emanuel R. Vásquez 5 Dec 16, 2022
A website for detecting name of bank from card number, supported all Iranian banks

Detect Iranian Bank Web A website for detecting name of bank from card number, supported all Iranian banks. This package contains SVG logo and brand c

Max Base 12 Oct 2, 2022
Copy/paste detecting GitHub Action for programming source code (jscpd)

dry-code Copy/paste detecting GitHub Action for programming source code with jscpd Action inputs Action input Description Default Value Required optio

null 5 Dec 14, 2022
Improve the security of your API by detecting common vulnerabilities as defined by OWASP and enforced with Spectral.

Spectral OWASP API Security Scan an OpenAPI document to detect security issues. As OpenAPI is only describing the surface level of the API it cannot s

Stoplight 23 Dec 8, 2022
Lazyload images, iframes or any src* element until they are visible in the viewport.

Lazyload images, iframes or any src* element until they are visible in the viewport.

Vincent Voyer 938 Nov 15, 2022
Demodal is a browser extension that automatically removes content blocking modals including paywalls, discount offers, promts to sign up or enter your email address and more.

Demodal Demodal is a browser extension that automatically removes content blocking modals including paywalls, discount offers, promts to sign up or en

Elbert Alias 225 Jan 4, 2023
linuxbu - a chrome extension that helps you to enter SBU classes via html-view in linux

linuxbu is a chrome extension that helps you to enter SBU classes via html-view in linux

Hasan Zonuzi 8 Dec 19, 2022