GithubHelp home page GithubHelp logo

jae04099 / long-press-event Goto Github PK

View Code? Open in Web Editor NEW

This project forked from john-doherty/long-press-event

0.0 0.0 0.0 365 KB

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

Home Page: https://jsfiddle.net/kdfhz12y/

License: MIT License

JavaScript 85.29% HTML 14.71%

long-press-event's Introduction

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

long-press-event's People

Contributors

avrahamcool avatar btjones avatar cablegunmaster avatar greew avatar john-doherty avatar mrjestice 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.