CSS/jQuery: Create a faded effect on jQuery cycle color image slideshows

I'm looking for a way to visually highlight the currently selected slideshow by greying out all non-selected slideshows. I've tried using the canvas with CSS & jQuery, but it caused issues on my website. Is there another method to achieve this effect using grayscale in Safari, Chrome, Firefox, and IE>=8?

If only we could get -webkit-filter: grayscale(100%); to work across all browsers...

<script type="text/javascript">
$(window).load(function(){

    $('.item img').each(function(){
        var el = $(this);
        el.css({"position":"absolute"}).wrap("<div class='img_wrapper' style='display: inline-block'>").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","opacity":"0"}).insertBefore(el).queue(function(){
            var el = $(this);
            el.parent().css({"width":this.width,"height":this.height});
            el.dequeue();
        });
        this.src = grayscale(this.src);
    });

    $('.item img').mouseover(function(){
        $(this).parent().find('img:first').stop().animate({opacity:1}, 1000);
    })
    $('.img_grayscale').mouseout(function(){
        $(this).stop().animate({opacity:0}, 1000);
    });     
});

function grayscale(src){
    var canvas = document.createElement('canvas');
    var ctx = canvas.getContext('2d');
    var imgObj = new Image();
    imgObj.src = src;
    canvas.width = imgObj.width;
    canvas.height = imgObj.height; 
    ctx.drawImage(imgObj, 0, 0); 
    var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
    for(var y = 0; y < imgPixels.height; y++){
        for(var x = 0; x < imgPixels.width; x++){
            var i = (y * 4) * imgPixels.width + x * 4;
            var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
            imgPixels.data[i] = avg; 
            imgPixels.data[i + 1] = avg; 
            imgPixels.data[i + 2] = avg;
        }
    }
ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
return canvas.toDataURL();
}    
</script>

Answer №1

From what I understand, CSS filters are currently only supported in Webkit browsers. If you require cross-browser compatibility, utilizing canvas may be your best option. A helpful library that can assist with this task is available at:

You can also refer to the original source for more information:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Use jQuery's post method to send an input file to a PHP file whenever it changes

From my understanding, it seems that $.ajax() is similar to $.post(). I am attempting to use $.post to send an input file to a PHP script in order to upload the chosen image once the user has browsed for it. I'm trying to implement something straight ...

Utilize a PHP-AJAX request to retrieve JSON data and then transfer the data to JGauge.js in order to display the value

scenario where I need to transfer an ajax-called value to another script (specifically a widget script) within the same file, I have managed to successfully execute the ajax call and implement a justgage widget on a single page. While I can effectively dis ...

Transform the data attributes into a list without any specific order

Can someone help me with this code snippet? <a href="#" data-teste='["apresentacoes/1.jpg", "apresentacoes/2.jpg", "apresentacoes/3.jpg"]'> <img src="apresentacoes/thumbs/1.jpg" alt="img01"/> </a> I would like to modify th ...

Identify the row containing a value of null using jQuery (functionality not performing as anticipated)

Whenever the user clicks on the GetData button, I retrieve JSON data and display it in an HTML table similar to the demo below. Demo: https://plnkr.co/edit/I4XYY6CZohf7IS6wP8dR?p=preview There are instances where the value can be null, such as the loanNu ...

Preventing multiple event handlers from firing on an HTML element after a server response

I currently have a div block on my page: <div class='btn'>click here</div> <div class='dialogWindow'></div> along with some JavaScript containing a click handler: $('.btn').live('click', fu ...

What are some ways to expand the width and increase the spacing of bootstrap cards?

On my website, I have created Bootstrap cards that are displayed on two different pages. One set of cards is shown on the landing page, where I am looking to increase the spacing between them. The other set appears on the books page, and I want to make t ...

Filter posts on Blogger by unique terms

Currently, I have a code snippet designed to showcase blog posts based on tags. There is an option to modify the section that houses the loop: <b:loop values='data:post.labels' var='label'> Utilizing any of these variables: da ...

What is the best way to declare a minimum and maximum date in HTML as the current date?

I have a question regarding setting the min/max date for a date input in my Angular 6 project. How can I ensure that only dates up to the current date are enabled? So far, I have attempted to initialize a new Date object in the ngOnInit function and set t ...

Exploring the world of jQuery waypoints and the art of modifying

This is only the second question I'm asking here, so please be gentle! I've been experimenting with jQuery waypoints to dynamically show and hide a border under my navigation menu based on scroll position. For instance, when the sticky nav is ov ...

Dynamic resizing in NextJs does not trigger a re-render

My goal is to dynamically pass the width value to a component's styles. Everything works fine on initial load, but when I resize the window, the component fails to re-render even though the hook is functioning as intended. I came across some informat ...

Re-triggering jQuery email validation after making necessary corrections

After validating an email address with the IF statement and finding it to be invalid, everything works correctly. However, when a valid email is entered, the ELSE statement should trigger and clear the error div, but it is not doing so. <SCRIPT> $ ...

How come when I click on the edit button, the selected data's model doesn't appear in the dropdown menu as I would like it to?

this is the function in my controller public function getModel($make_id = null) { $make_id = request()->make_id; $carsmodel = CarModel::where('make_id', $make_id)->orderBy('model', 'ASC')->get(); return re ...

creating center-focused gradients in react native using LinearGradient

Hey there, I'm currently facing an issue with applying a box shadow to one of the buttons in my React Native application. Unfortunately, the box shadow property is not supported in Android. To work around this limitation, I am experimenting with creat ...

What could be causing the error in sending JSON data from JavaScript to Flask?

Check out this cool javascript code snippet I wrote: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script type=text/javascript> $(function() { $.ajax({ type: 'PO ...

showcase 7 content sections in a two-column layout

I have a large container with 7 smaller containers inside <div id="big_div"> <div>a</div> <div>a</div> <div>a</div> <div>b</div> <div>b</div> <div>b</div><div>b</d ...

Preventing Content Jumping When Concealing Elements: Tips and Tricks

I've been working on a project at , where you can click on a small map to expand a larger map below it. However, I noticed that when you scroll down to the large map and then try to close it using the button at the bottom, the content on the page jum ...

How can the results of an AJAX JSON request be effectively showcased?

When I make a call via ajax to an api, what is the simplest way to display the result? If I use alert on the result, I only see [object object]. If I try to alert a specific item that I know is in the returned JSON (results.title, for example), I get an &a ...

Stop flex child from shrinking in size

I have a form row with two inputs. There is a requirement to show ⚠️ (warning emoji) next to the second input in the row, based on the input number. However, because of how flexbox behaves, the input size gets reduced... How can I maintain the input s ...

Position the elements to the right side of the page by incorporating the row class in Bootstrap 4

I've written this code using Bootstrap 4 to place elements on the page: <div class="row justify-content-center container"> <div class=" col-md-8 col-xs-8 col-lg-8 col-xl-8 col-sm-12 p-0"> <div class="row justify-content-center col ...

The DataType.MultilineText field in my modal popup partial view is causing all validation to fail

I encountered a peculiar issue recently. After creating a new asp.net mvc5 application, I decided to upgrade it to asp.net mvc-5.2.2. The problem arose with the following field:- [Required] [StringLength(200)] public string Name { get; set; } When ren ...