Home > Web Front-end > JS Tutorial > jQuery Filter Images (Search Filter)

jQuery Filter Images (Search Filter)

Jennifer Aniston
Release: 2025-03-05 00:16:09
Original
131 people have browsed it

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.

jQuery Filter Images (Search Filter)

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);
});
Copy after login

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);
Copy after login

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) ... */
Copy after login

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:

  • Filtering by attributes: Using .filter() and .attr() to select images based on their attributes (e.g., alt, src).
  • Filtering by dimensions: Using .filter(), .width(), and .height() to select images based on size.
  • Filtering by visibility: Using :visible and :hidden selectors.
  • Filtering by source URL, DOM position, CSS properties, data attributes, parent element, index, and alt text: Detailed explanations and code examples for each scenario are provided.

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template