Giter Site home page Giter Site logo

pierrevdk / probaclick Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alexmacarthur/probaclick

0.0 1.0 0.0 857 KB

Do something when someone is probably going to click something.

Home Page: https://www.npmjs.com/package/probaclick

License: MIT License

JavaScript 87.52% HTML 12.48%

probaclick's Introduction

ProbaClick

Do something when someone's probably going to click something.

ProbaClick Example

Overview

ProbaClick fires a callback when a user hovers over an element for a specified length of time or, if specified, after a user hovers over a link a certain number of times. It's designed under the assumption that when a user spends a certain amount of time hovering over an item, it's likely that they're about click or interact with it in some way. By anticipating that click, you're able to perform an action before the user actually does it.

Use Cases

The use cases are wide, but perhaps the most common is to dynamically fetch resources when it's likely that a user will click on a link. For example, you might dynamically prefetch a page that a user is probably going to navigate to next.

Usage

To use, pass a NodeList, Node, or string selector, and define your options.

//-- Turn each button red when it's hovered over for a second.
ProbaClick(document.querySelectorAll("button"), {
  callback: function (element) {
    element.style.background = "red";
  },
  delay: 1000,
});

Trigger by Hover Duration or Hover Count

By default, ProbaClick will fire its callback method after a certain amount of total time hovered. For example, if delay is set to 1000, the callback will fire after a user hovers twice at 500ms each, once at 1000ms, or any other number of hovers whose durations add up to at least 1000ms.

However, it's also possible to trigger the callback based on the total number of hovers, regardless of the amount of time spent hovering. The following configuration will fire the callback after a total hover time of 1000ms OR when the user has hovered over the element 3 times.

ProbaClick(document.querySelectorAll("button"), {
  callback: function (element) {},
  delay: 1000,
  count: 3,
});

After the callback has fired, the total hover time and hover count will be reset to zero.

Arguments

Elements

Any of the following are valid:

ProbaClick(document.querySelectorAll("a.class")); // ...
ProbaClick(document.querySelector("a.class")); // ...
ProbaClick("a.class"); // ...

Options

Option Description Default
callback The function that will fire after the delay is met. function(){}
delay The amount of milliseconds to wait before firing the callback. 500
count The number of individual hovers to allow before fireing the callback. null
max The maximum number of times the callback will fire after repeatedly hovering over it. Not specifying this value will allow the callback to fire over and over with no limit as a user meets the hover threshold. After the max has been met, event listeners will be removed. false

probaclick's People

Contributors

alexmacarthur avatar dependabot[bot] avatar

Watchers

 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.