├── .DS_Store ├── README.md ├── auto-suggest.php ├── css └── styles.css ├── images ├── bt-search.jpg └── try-demo.jpg ├── index.html ├── scripts ├── .DS_Store └── ajax-search-suggest.js └── test-data.php /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IanLunn/AJAX-Search-Suggest--WeAreHunted.com-Style-/29927636a4b6ce9514676c956310f42676548c1d/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #AJAX Search Suggest (WeAreHunted.com Style) 2 | 3 | Recreate a full viewport search feature similar to the one seen on [WeAreHunted.com](http://wearehunted.com/). 4 | 5 | Version 1.0 6 | Author: [Ian Lunn](http://www.ianlunn.co.uk/) 7 | 8 | Demo URL: [AJAX Search Suggest (WeAreHunted.com Style)](http://www.ianlunn.co.uk/demos/ajax-search-suggest-wearehunted/) 9 | Tutorial URL: [AJAX Search Suggest (WeAreHunted.com Style)](http://www.ianlunn.co.uk/blog/code-tutorials/ajax-search-suggest-wearehunted/) 10 | GitHub: [AJAX Search Suggest (WeAreHunted.com Style)](https://github.com/IanLunn/AJAX-Search-Suggest--WeAreHunted.com-Style-/) 11 | 12 | Dual licensed under the MIT and GPL licenses: 13 | http://www.opensource.org/licenses/mit-license.php 14 | http://www.gnu.org/licenses/gpl.html 15 | -------------------------------------------------------------------------------- /auto-suggest.php: -------------------------------------------------------------------------------- 1 | $url){ //for each value in the data array... 25 | if (substr(strtolower($name),0,$latestQueryLength) == strtolower($latestQuery)){ //check to see if the query matches any search terms, if it does... 26 | $result[$name] = $url; //add the term as a key and the url as it's value. example: ACDC : "?search=ACDC" 27 | } 28 | } 29 | 30 | echo json_encode($result); //encode the results list as a JavaScript object, and send it back to the JavaScript 31 | } 32 | ?> -------------------------------------------------------------------------------- /css/styles.css: -------------------------------------------------------------------------------- 1 | /* 2 | Demo: AJAX Search Suggest (WeAreHunted.com Style) 3 | Version 1.0 4 | Author: Ian Lunn 5 | Author URL: http://www.ianlunn.co.uk/ 6 | Demo URL: http://www.ianlunn.co.uk/demos/ajax-search-suggest-wearehunted/ 7 | Tutorial URL: http://www.ianlunn.co.uk/blog/code-tutorials/ajax-search-suggest-wearehunted/ 8 | GitHub: https://github.com/IanLunn/AJAX-Search-Suggest--WeAreHunted.com-Style-/ 9 | 10 | Dual licensed under the MIT and GPL licenses: 11 | http://www.opensource.org/licenses/mit-license.php 12 | http://www.gnu.org/licenses/gpl.html 13 | */ 14 | 15 | /* http://meyerweb.com/eric/tools/css/reset/ 16 | v2.0 | 20110126 17 | License: none (public domain) 18 | */ 19 | 20 | html, body, div, span, applet, object, iframe, 21 | h1, h2, h3, h4, h5, h6, p, blockquote, pre, 22 | a, abbr, acronym, address, big, cite, code, 23 | del, dfn, em, img, ins, kbd, q, s, samp, 24 | small, strike, strong, sub, sup, tt, var, 25 | b, u, i, center, 26 | dl, dt, dd, ol, ul, li, 27 | fieldset, form, label, legend, 28 | table, caption, tbody, tfoot, thead, tr, th, td, 29 | article, aside, canvas, details, embed, 30 | figure, figcaption, footer, header, hgroup, 31 | menu, nav, output, ruby, section, summary, 32 | time, mark, audio, video { 33 | margin: 0; 34 | padding: 0; 35 | border: 0; 36 | font-size: 100%; 37 | font: inherit; 38 | vertical-align: baseline; 39 | } 40 | /* HTML5 display-role reset for older browsers */ 41 | article, aside, details, figcaption, figure, 42 | footer, header, hgroup, menu, nav, section { 43 | display: block; 44 | } 45 | body { 46 | line-height: 1; 47 | } 48 | ol, ul { 49 | list-style: none; 50 | } 51 | blockquote, q { 52 | quotes: none; 53 | } 54 | blockquote:before, blockquote:after, 55 | q:before, q:after { 56 | content: ''; 57 | content: none; 58 | } 59 | table { 60 | border-collapse: collapse; 61 | border-spacing: 0; 62 | } 63 | 64 | *:focus {outline: none;} 65 | 66 | body{ 67 | font-family: 'Play', sans-serif; 68 | } 69 | 70 | a{ 71 | color: white; 72 | } 73 | 74 | a:hover{ 75 | text-decoration: none; 76 | } 77 | 78 | #search{ 79 | background: #d3d3d3; 80 | cursor: pointer; 81 | font-size: 24px; 82 | font-weight: bold; 83 | text-transform: uppercase; 84 | padding: 20px 2%; 85 | width: 96%; 86 | } 87 | 88 | #search-overlay{ 89 | background: black; 90 | background: rgba(0,0,0,0.85); 91 | color: white; 92 | display: none; 93 | font-size: 24px; 94 | height: 100%; 95 | padding: 20px; 96 | position: fixed; 97 | width: 100%; 98 | } 99 | 100 | #display-search{ 101 | background: none; 102 | border: none; 103 | color: white; 104 | font-size: 60px; 105 | margin: 25px 0 0 0; 106 | width: 960px; 107 | } 108 | 109 | #hidden-search{ 110 | left: -10000px; 111 | position: absolute; 112 | } 113 | 114 | #results{ 115 | display: none; 116 | width: 300px; 117 | } 118 | 119 | #artists{ 120 | font-size: 18px; 121 | line-height: 125%; 122 | padding: 0 0 0 10px; 123 | } 124 | 125 | h2.artists{ 126 | margin: 30px 0 20px 0; 127 | } 128 | 129 | h2{ 130 | margin: 0 0 20px 0; 131 | } 132 | 133 | #close{ 134 | cursor: pointer; 135 | position: fixed; 136 | right: 20px; 137 | top: 20px; 138 | } 139 | 140 | .try{ 141 | margin: 20px 0 0 60px; 142 | } -------------------------------------------------------------------------------- /images/bt-search.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IanLunn/AJAX-Search-Suggest--WeAreHunted.com-Style-/29927636a4b6ce9514676c956310f42676548c1d/images/bt-search.jpg -------------------------------------------------------------------------------- /images/try-demo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IanLunn/AJAX-Search-Suggest--WeAreHunted.com-Style-/29927636a4b6ce9514676c956310f42676548c1d/images/try-demo.jpg -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 14 | 15 | 16 | 17 | 18 | 19 | AJAX Search Suggest (WeAreHunted.com Style) 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 | 36 | 42 |
43 | 44 | 45 |
46 |

Begin typing to search

47 |
X
48 |
49 | 50 | 51 |
52 | 53 |
54 |

Artists

55 | 56 |
57 |
58 | 59 | 63 | Click search to try the demo 64 | 65 | 68 | 69 | -------------------------------------------------------------------------------- /scripts/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/IanLunn/AJAX-Search-Suggest--WeAreHunted.com-Style-/29927636a4b6ce9514676c956310f42676548c1d/scripts/.DS_Store -------------------------------------------------------------------------------- /scripts/ajax-search-suggest.js: -------------------------------------------------------------------------------- 1 | /* 2 | Demo: AJAX Search Suggest (WeAreHunted.com Style) 3 | Version 1.0 4 | Author: Ian Lunn 5 | Author URL: http://www.ianlunn.co.uk/ 6 | Demo URL: http://www.ianlunn.co.uk/demos/ajax-search-suggest-wearehunted/ 7 | Tutorial URL: http://www.ianlunn.co.uk/blog/code-tutorials/ajax-search-suggest-wearehunted/ 8 | GitHub: https://github.com/IanLunn/AJAX-Search-Suggest--WeAreHunted.com-Style-/ 9 | 10 | Dual licensed under the MIT and GPL licenses: 11 | http://www.opensource.org/licenses/mit-license.php 12 | http://www.gnu.org/licenses/gpl.html 13 | */ 14 | 15 | $(document).ready(function(){ //when the browser has rendered the page... 16 | 17 | //save selectors we'll be using more than once into variables for better performance 18 | var $hiddenSearch = $("#hidden-search"), 19 | $displaySearch = $("#display-search"), 20 | $searchOverlay = $("#search-overlay"), 21 | $artistsList = $("#artists"); 22 | 23 | $("#search").click(function(){ //when the search link is clicked... 24 | $searchOverlay.show(); //show the search overlay 25 | $hiddenSearch.focus(); //give the hidden input box focus 26 | }); 27 | 28 | $searchOverlay.click(function(event){ //when the search overlay is clicked... 29 | $hiddenSearch.focus(); //give the hidden input box focus 30 | if(event.target.id == "search-overlay" || event.target.id == "close"){ //...only if the user is clicking the empty areas of the overlay (and not child elements)... 31 | $hiddenSearch.blur(); //remove focus from the hidden input 32 | $(this).animate({"opacity": 0}, 500, function(){ //...animate it's opacity to 0... 33 | $(this).hide().css("opacity", 1); //...hide it (so the user can click the elements behind it again) and reset its opacity 34 | }); 35 | } 36 | }); 37 | 38 | //when the user pushes a key whilst the input box has focus... 39 | $hiddenSearch.keydown(function(e){ 40 | currentQuery = $displaySearch.val(); //get the current value of the search input 41 | if(e.keyCode == 8){ //if the user hits backspace... 42 | 43 | latestQuery = currentQuery.substring(0, currentQuery.length - 1); //...remove the last character 44 | $displaySearch.val(latestQuery); //...update the search input box 45 | updateResults(latestQuery); //...update the results 46 | 47 | }else if(e.keyCode == 32 || (e.keyCode >= 65 && e.keyCode <= 90) || (e.keyCode >= 48 && e.keyCode <= 57)){ //if the user hits spacebar (32), characters a - z (65 - 90) or numeric keys(0 - 9)... 48 | latestQuery = currentQuery + String.fromCharCode(e.keyCode); //...add the newly entered key onto the current query 49 | $displaySearch.val(latestQuery); //...update the search input value with the latest query 50 | updateResults(latestQuery); //...update the results 51 | } 52 | }); 53 | 54 | function updateResults(latestQuery){ 55 | if(latestQuery.length > 0){ //if there is a query to process... 56 | $.post("auto-suggest.php", {latestQuery: latestQuery}, function(data){ //..send that query to the php script "auto-suggest.php" via ajax 57 | 58 | if(data.length > 0){ //if the php script returns a result... 59 | data = $.parseJSON(data); //turn the string from the PHP script into a JavaScript object 60 | $("#artists li").remove(":contains('No results')"); //remove the "No results" text if it's being displayed 61 | $("#results").show(); //show the results container 62 | 63 | previousTerms = new Array(); //set up an array that will hold the terms currently being displayed 64 | $("#artists li").each(function(){ //for each result currently being displayed... 65 | previousTerms.push($(this).text()); //add it to the previousTerms array 66 | }); 67 | 68 | keepTerms = new Array(); 69 | 70 | for(term in data){ //for each matched term in the returned data... 71 | url = data[term]; //get the url for the matched term; 72 | if($.inArray(term, previousTerms) === -1){ //if this term isn't in the previous list of terms (and isn't already being displayed)... 73 | $artistsList.prepend('
  • '+term+'
  • '); 74 | }else{ //if it is in the previous list... 75 | keepTerms.push(term); //add the term we want to keep to an array 76 | } 77 | } 78 | 79 | if(data == "" || (keepTerms.length == 0 && (previousTerms.length != 0 || $displaySearch.val() == ""))){ 80 | $artistsList.html("
  • No results
  • "); 81 | }else{ 82 | for(term in previousTerms){ //for each term in the displayed list... 83 | if($.inArray(previousTerms[term], keepTerms) === -1){ 84 | $("#artists li").filter(function(){ 85 | return $(this).text() == previousTerms[term] 86 | }).remove(); 87 | } 88 | } 89 | } 90 | } 91 | }); 92 | }else{ 93 | $artistsList.html("
  • No results
  • "); 94 | } 95 | } 96 | }); -------------------------------------------------------------------------------- /test-data.php: -------------------------------------------------------------------------------- 1 | "?search=ABBA", 19 | "ACDC" => "?search=ACDC", 20 | "Ace of Spades" => "?search=AceOfSpades", 21 | "Beyonce" => "?search=Beyonce", 22 | "Black Eyed Peas" => "?search=BlackEyedPeas", 23 | "Coldplay" => "?search=Coldplay", 24 | "Chaz and Dave" => "?search=ChazAndDave", 25 | "Dave Matthews Band" => "?search=DaveMatthewsBand", 26 | "Death Cab for Cutie" => "?search=DeathCabforCutie", 27 | "Eminem" => "?search=Eminem", 28 | "Elliott Smith" => "?search=ElliottSmith", 29 | "Florence and the Machine" => "?search=FlorenceandtheMachine", 30 | "Fall Out Boy" => "?search=FallOutBoy", 31 | "Green Day" => "?search=GreenDay", 32 | "Gorillaz" => "?search=Gorillaz", 33 | "Hollywood Undead" => "?search=HollywoodUndead", 34 | "He is Legend" => "?search=HeIsLegend", 35 | "Iron & Wine" => "?search=Iron&Wine", 36 | "Incubus" => "?search=Incubus", 37 | "John Mayer" => "?search=JohnMayer", 38 | "Jason Mraz" => "?search=JasonMraz", 39 | "Kings of Leon" => "?search=KingsofLeon", 40 | "Kanye West" => "?search=KanyeWest", 41 | "Lady GaGa" => "?search=LadyGaGa", 42 | "Led Zeppelin" => "?search=LedZeppelin", 43 | "Modest Mouse" => "?search=ModestMouse", 44 | "Mumford & Sons" => "?search=Mumford&Sons", 45 | "Nine Inch Nails" => "?search=NineInchNails", 46 | "Nirvana" => "?search=Nirvana", 47 | "Owl City" => "?search=OwlCity", 48 | "Oasis" => "?search=Oasis", 49 | "Paramore" => "?search=Paramore", 50 | "Pearl Jam" => "?search=PearlJam", 51 | "Queen" => "?search=Queen", 52 | "Queens of the Stone Age" => "?search=QueensoftheStoneAge", 53 | "Radiohead" => "?search=Radiohead", 54 | "Regina Spektor" => "?search=ReginaSpektor", 55 | "Sufjan Stevens" => "?search=SufjanStevens", 56 | "Stereophonics" => "?search=Stereophonics", 57 | "Taylor Swift" => "?search=TaylorSwift", 58 | "Tool" => "?search=Tool", 59 | "U2" => "?search=U2", 60 | "Underoath" => "?search=Underoath", 61 | "Vampire Weekend" => "?search=VampireWeekend", 62 | "Vader" => "?search=Vader", 63 | "Weezer" => "?search=Weezer", 64 | "Wilco" => "?search=Wilco", 65 | "Xzibit" => "?search=Xzibit", 66 | "Xiu Xiu" => "?search=XiuXiu", 67 | "Yeah Yeah Yeahs" => "?search=YeahYeahYeahs", 68 | "You Me At Six" => "?search=YouMeAtSix", 69 | "Zero 7" => "?search=Zero7", 70 | "ZZ Top" => "?search=ZZTop" 71 | ); 72 | ?> --------------------------------------------------------------------------------