This article demonstrates building a live image search filter using jQuery, powered by Flickr image data. The search dynamically updates displayed images as you type. This is achieved using the QuickSilver Style jQuery plugin, which implements a JavaScript string ranking algorithm for efficient searching.
Core Functionality:
The primary jQuery code for the live search is concise:
$("#filter").keyup(function () { var filter = $(this).val(), count = 0; $(".filtered:first li").each(function () { if ($(this).text().search(new RegExp(filter, "i")) < 0) { $(this).addClass("hidden"); } else { $(this).removeClass("hidden"); count++; } }); $("#filter-count").text(count); });
This snippet listens for keyup events on an element with the ID "filter". It then filters list items within the ".filtered" class, hiding those that don't match the input text (case-insensitive). A count of matching items is also displayed.
QuickSilver Live Search Plugin:
The article also details the QuickSilver plugin, crucial for the live search's performance:
(function($) { // ... (Plugin code as in original input) ... })(jQuery);
This plugin enhances the search functionality by providing a more sophisticated string-matching algorithm, improving speed and relevance.
Complete Image Search Code:
The complete code integrates Flickr data fetching, image display, and the live search functionality:
/* ... (Full code as in original input) ... */
This code fetches Flickr photos via a JSON API call, dynamically creates image elements, and applies the live search functionality.
Frequently Asked Questions (FAQs):
The article concludes with a comprehensive FAQ section addressing various jQuery image filtering techniques, including:
.filter()
and .attr()
to select images based on their attributes (e.g., alt
, src
)..filter()
, .width()
, and .height()
to select images based on size.:visible
and :hidden
selectors.This enhanced summary retains the key information while improving clarity and readability. The image is included and its format is preserved.
The above is the detailed content of jQuery Filter Images (Search Filter). For more information, please follow other related articles on the PHP Chinese website!