• Stars
    star
    323
  • Rank 130,051 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Adds `long-press` event to the DOM in 1k of pure JavaScript

long-press-event

Shippable branch npm

A 1k script that adds a long-press event to the DOM using CustomEvent and pure JavaScript. Works in IE9+, Chrome, Firefox, Safari as well as popular mobile browsers including Cordova (Phone Gap) applications.

Defaults to 1.5 seconds but can be overridden by adding a data-long-press-delay attribute to an element.

Try the demo

Usage

Add the long-press-event.min.js file to your page and then listen for the event.

To listen for a long-press on any HTML element:

// the event bubbles, so you can listen at the root level
document.addEventListener('long-press', function(e) {
  console.log(e.target);
});

To listen for a long-press on a specific HTML element:

// grab the element
var el = document.getElementById('idOfElement');

// listen for the long-press event
el.addEventListener('long-press', function(e) {

  // stop the event from bubbling up
  e.preventDefault()

  console.log(e.target);
});

To set a custom delay and listen for a long-press:

<html>
    <head>
        <script src="long-press-event.js"></script>
        <script>
            document.addEventListener('long-press', function(e) {
                console.log(e.target);
            });
        </script>
    </head>
    <body>
        <div data-long-press-delay="500">Press and hold me for .5s</div>
    </body>
</html>

To set a default application wide data-long-press-delay, set attribute on a parent/topmost element:

<html data-long-press-delay="1500">
    <head>...</head>
    <body>...</body>
</html>

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -m 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request

Development

The project includes everything needed to tweak, including a node webserver. Run the following, then visit http://localhost:8080 in your browser.

You can test on a desktop using Device Mode in Google Chrome.

git clone https://github.com/john-doherty/long-press-event
cd long-press
npm install
npm start

Update .min files

Update long-press-event.min.js by tweaking the version number in package.json and running:

npm run build

Star the repo

If you find this useful, please star the repo. It helps me prioritise which open source issues to tackle first.

History

For change-log, check releases.

License

Licensed under MIT License © John Doherty

More Repositories

1

swiped-events

Adds `swiped` events to the DOM in 1k of pure JavaScript
JavaScript
580
star
2

selenium-cucumber-js

Browser automation framework written in pure JavaScript using official selenium-webdriver and cucumber-js
JavaScript
120
star
3

mixpanel-lite

3k alternative to mixpanel-js with offline support for PWAs
JavaScript
53
star
4

node-iframe-replacement

An alternative to sticking that lovely web app into an <iframe> on a corp website
JavaScript
50
star
5

raspberry-pi-mjpeg-server

Node.js module providing Motion JPEG access to the Raspberry PI camera module to enable video streaming via HTTP
JavaScript
44
star
6

notch-detected-event

A cross-browser script to detect the existence of a device notch
JavaScript
20
star
7

pure-dialog

<pure-dialog> is a 3k, self-contained, pure JavaScript dialog
JavaScript
20
star
8

cordova-file-storage

An easy way to read/write files in Cordova
12
star
9

jsdoc-to-json-schema

Generate JSON schemas from JavaScript code comments using jsDoc @schema tag.
JavaScript
11
star
10

offline-fetch

1.2k script that adds offline support to fetch by storing responses in localStorage - ideal for PWAs
JavaScript
11
star
11

pureGrid

Lightweight JavaScript Grid/SpreadSheet component written in pure JavaScript
JavaScript
11
star
12

offline-geo-from-ip

Get geo location information from an IP address
JavaScript
7
star
13

express-batch-requests

Express middleware to process batch HTTP requests
JavaScript
6
star
14

express-url-breadcrumb

Express middleware that generates a breadcrumb from the current URL and makes it available in all views via breadcrumb variable
JavaScript
4
star
15

fetch-reply-with

Unit test fetch requests with ease
JavaScript
3
star
16

seo-tabs

Simple SEO friendly tabs (1k of pure JS)
JavaScript
3
star
17

express-seo-noslash

Remove trailing slashes from incoming urls
JavaScript
2
star
18

express-set-domain

Force an express app to use a particular domain
JavaScript
2
star
19

express-request-transfer

Transfer requests without redirects (aka Server.Transfer in C#)
JavaScript
2
star
20

isomorphic-mongo-objectid

pure JavaScript implementation of mongo ObjectID for client and server in 0.7k
JavaScript
1
star
21

jsdom-add-files

An easy way to add files to HTML file inputs in jsdom
JavaScript
1
star
22

is-port-busy

A simple node module to check if a TCP port is busy on the local machine.
JavaScript
1
star
23

simple-storage

1k auto-casting localStorage wrapper
JavaScript
1
star
24

is-port-free

A simple node module to check if a TCP port is free on the local machine.
JavaScript
1
star