├── .gitignore ├── README.md └── jquery.longpress.js /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *~ 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # longpress: a jQuery plugin 2 | 3 | Longpress is a jQuery plugin that makes it easy to support long press 4 | events on mobile devices and desktop borwsers. 5 | 6 | ## Native Library 7 | 8 | If you want something native and more low level than the jQuery implementation, 9 | take a look at [longpress.js][lp] that is also written by me. 10 | 11 | [lp]: https://github.com/vaidik/longpress.js 12 | 13 | ## Quick Usage 14 | 15 | ```html 16 | 17 | ``` 18 | 19 | ```js 20 | 21 | $('#button').longpress(function() { 22 | // longpress callback 23 | alert('You just longpress-ed a button.'); 24 | }); 25 | 26 | ``` 27 | 28 | ### Detailed Usage 29 | 30 | **.longpress(longpressHandler(event)[, shortpressHandler(event), duration])** 31 | 32 | 33 | 34 | 35 | 39 | 40 | 41 | 42 | 43 | 47 | 48 | 49 | 50 | 51 | 55 | 56 |
longpressHandler(event)Required 36 | Type: Function() 37 | A function to execute each time someone longpresses something. 38 |
shortpressHandler(event)Optional 44 | Type: Function() 45 | A function to execute each time someone releases the mouse or touch before the longpress duration elapses. 46 |
durationOptional 52 | Type: Integer 53 | longpress duration in milliseconds. 54 |
57 | 58 | **Example:** 59 | 60 | ```js 61 | $('#button').longpress(function(e) { 62 | alert('You just longpressed something.'); 63 | }, function(e) { 64 | alert('You released before longpress duration and that\'s why its a shortpress now.'); 65 | }); 66 | ``` 67 | 68 | ## Author 69 | 70 | [Vaidik Kapoor](http://vaidikkapoor.info) ([@vaidikkapoor](http://twitter.com/vaidikkapoor)) 71 | 72 | ## Other 73 | 74 | [MIT License](http://www.opensource.org/licenses/mit-license.php) 75 | 76 | Copyright (c) 2008-2013, Vaidik Kapoor (kapoor [*dot*] vaidik -[at]- gmail [*dot*] com) 77 | -------------------------------------------------------------------------------- /jquery.longpress.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Longpress is a jQuery plugin that makes it easy to support long press 3 | * events on mobile devices and desktop borwsers. 4 | * 5 | * @name longpress 6 | * @version 0.1.2 7 | * @requires jQuery v1.2.3+ 8 | * @author Vaidik Kapoor 9 | * @license MIT License - http://www.opensource.org/licenses/mit-license.php 10 | * 11 | * For usage and examples, check out the README at: 12 | * http://github.com/vaidik/jquery-longpress/ 13 | * 14 | * Copyright (c) 2008-2013, Vaidik Kapoor (kapoor [*dot*] vaidik -[at]- gmail [*dot*] com) 15 | */ 16 | 17 | (function($) { 18 | $.fn.longpress = function(longCallback, shortCallback, duration) { 19 | if (typeof duration === "undefined") { 20 | duration = 500; 21 | } 22 | 23 | return this.each(function() { 24 | var $this = $(this); 25 | 26 | // to keep track of how long something was pressed 27 | var mouse_down_time; 28 | var timeout; 29 | 30 | // mousedown or touchstart callback 31 | function mousedown_callback(e) { 32 | mouse_down_time = new Date().getTime(); 33 | var context = $(this); 34 | 35 | // set a timeout to call the longpress callback when time elapses 36 | timeout = setTimeout(function() { 37 | if (typeof longCallback === "function") { 38 | longCallback.call(context, e); 39 | } else { 40 | $.error('Callback required for long press. You provided: ' + typeof longCallback); 41 | } 42 | }, duration); 43 | } 44 | 45 | // mouseup or touchend callback 46 | function mouseup_callback(e) { 47 | var press_time = new Date().getTime() - mouse_down_time; 48 | if (press_time < duration) { 49 | // cancel the timeout 50 | clearTimeout(timeout); 51 | 52 | // call the shortCallback if provided 53 | if (typeof shortCallback === "function") { 54 | shortCallback.call($(this), e); 55 | } else if (typeof shortCallback === "undefined") { 56 | ; 57 | } else { 58 | $.error('Optional callback for short press should be a function.'); 59 | } 60 | } 61 | } 62 | 63 | // cancel long press event if the finger or mouse was moved 64 | function move_callback(e) { 65 | clearTimeout(timeout); 66 | } 67 | 68 | // Browser Support 69 | $this.on('mousedown', mousedown_callback); 70 | $this.on('mouseup', mouseup_callback); 71 | $this.on('mousemove', move_callback); 72 | 73 | // Mobile Support 74 | $this.on('touchstart', mousedown_callback); 75 | $this.on('touchend', mouseup_callback); 76 | $this.on('touchmove', move_callback); 77 | }); 78 | }; 79 | }(jQuery)); 80 | --------------------------------------------------------------------------------