Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Henrique Zafniksi
Henrique Zafniksi
8,560 Points

Changing -webkit-grayscale with jQuery on any event

Considering -webkit- doesn't include all browsers, is it good practice for me to write the following code?

$('#on').find('div').on('mouseleave', function(){
     $(this).css('-webkit-filter', 'grayscale(100%)');
     $(this).css('-moz-filter', 'grayscale(100%)');
     $(this).css('filter', 'grayscale(100%)');
});

I was thinking about setting some conditional, like "if the browser is Firefox, then set... else if chrome, then", but I couldn't find conclusive answers on this subject.

1 Answer

It actually is a good practice to add all styles for different browsers. If a browser isnt webkit, it just ignores the -webkit prefix, so there is no overhead to having all the styles included in the statement. I do have on optimization that you might want to use: You can actually include all these styles in one statement like this:

$('#on div').on('mouseleave', function(){
     $(this).css({
         '-webkit-filter', 'grayscale(100%)',
         '-moz-filter', 'grayscale(100%)',
         'filter', 'grayscale(100%)'
    });
});

This makes the code a little cleaner and more concise.

Henrique Zafniksi
Henrique Zafniksi
8,560 Points

Awesome! Thank you for the thoughtful response!