Last active
December 12, 2015 09:09
-
-
Save ynonp/4749795 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .autocomplete { | |
| list-style: none; | |
| position: absolute; | |
| top: 0; | |
| left: 0; | |
| padding: 0; | |
| margin: 0; | |
| outline: 1px solid gray; | |
| display: none; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Hello jQPlugin</title> | |
| <link rel="stylesheet" href="autocomplete.css" /> | |
| <style> | |
| div { | |
| width: 200px; | |
| height: 200px; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <input type="text" class="same" /> | |
| <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> | |
| <script src="plugin.js"></script> | |
| <script src="sametext.js"></script> | |
| <script src="autocomplete.js"></script> | |
| <script> | |
| $('input').autocomplete({ | |
| data: ['apple', 'banana', 'grapefruit', 'ananas', 'lemon'] | |
| }); | |
| $('input').remove(); | |
| </script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function($) { | |
| $.fn.autocomplete = function(options) { | |
| var self = this; | |
| options = $.extend({}, $.fn.autocomplete.defaults, options ); | |
| var list_el = $('<ul class="autocomplete"></ul>'); | |
| var fill_list = function( partial ) { | |
| var data = options.data.filter( function(el) { | |
| return el.indexOf(partial) >= 0; | |
| }); | |
| list_el.html ( '<li>' + data.join('</li><li>') + '</li>' ); | |
| }; | |
| list_el.on('click', 'li', function(ev) { | |
| self.val( $(ev.target).html() ); | |
| list_el.hide(); | |
| }); | |
| self.on('input', function(ev) { | |
| var top = self.offset().top; | |
| var left = self.offset().left; | |
| var width = self.width(); | |
| var height = self.height(); | |
| list_el.css({ | |
| top: (top + height + 5) + 'px', | |
| left: left + 'px', | |
| width: width + 'px' | |
| }); | |
| if ( ev.target.value.length > 0 ) { | |
| fill_list(ev.target.value); | |
| list_el.show(); | |
| } else { | |
| list_el.hide(); | |
| } | |
| }); | |
| $(document.body).append(list_el); | |
| }; | |
| $.fn.autocomplete.defaults = { | |
| data: [ | |
| 'windows', | |
| 'mac', | |
| 'linux', | |
| 'bsd', | |
| 'unix', | |
| 'ios', | |
| 'android', | |
| 'winphone', | |
| 'maemo', | |
| 'meego', | |
| 'zos', | |
| 'mvs' | |
| ] | |
| }; | |
| }(jQuery)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment