• Stars
    star
    561
  • Rank 76,706 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Make an image grayscale in all browsers

gray

CSS Styles and jQuery plugin to display images in grayscale.npm version

Demo

Support:

  • IE 6-11 (10 and 11 use the JavaScript polyfill)
  • Edge 12+ (12 uses the JavaScript polyfill)
  • Firefox 10+, Firefox on Android
  • Chrome 19+, Chrome for Android 25+
  • Android Browser 4.4+
  • Safari 6+, Safari iOS 6+
  • Opera 15+, Opera Mobile 16+
  • BlackBerry Browser 10+

Installation

You can install with npm:

npm install --save jquery-gray

Or Bower:

bower install --save gray

Or use the CDN links:

https://npmcdn.com/[email protected]/css/gray.min.css
https://npmcdn.com/[email protected]/js/jquery.gray.min.js

Or you can just download it.

How to Use

  1. Add the CSS from gray.css.

    If you want to use your own CSS selector instead of .grayscale, edit or copy the CSS.

  2. Add the plugin after jQuery at the bottom of the body.

    <script src="/js/jquery.gray.min.js"></script>

    The plugin automatically initializes for all elements with a class of grayscale.

    The plugin can also be called manually with:

    $('.my-grayscale-class').gray();

    The effect can be toggled with the grayscale-off class:

    $('.grayscale').toggleClass('grayscale-off');
  3. Add your image with a class of grayscale.

    <img src="/img/color.jpg" class="grayscale" />

    Images with CSS background-image, background-size and (numeric) background-position are also supported:

    <div
      style="
      background-image   : url(/img/color-sprite-lg.jpg);
      background-size    : auto 72px;
      background-position: -180px 0;
      display            : inline-block;
      width              : 180px;
      height             : 72px;
    "
      class="grayscale"
    ></div>

    Non-numeric background-size (cover, contain, ...) and background-position (center, ...) are not currently supported (pull requests welcome!).

  4. (Optional) Add class of grayscale-fade if you want transitioning from grayscale back to color on hover

    <img src="/img/color.jpg" class="grayscale grayscale-fade" />
  5. If you are using Modernizr already, make sure that the required feature detects are included in your build.

  6. Revel in your absolute and utter awesomeness.

Caveats

Non-numeric background-size and background-position

Non-numeric background-size (cover, contain, ...) and background-position (center, ...) are not currently supported.

Percentage-based background-position

Percentage-based background-position (ex. 50% 50%, ...) is not currently supported.

Non-visible elements will not be correctly initialized

Calling the plugin on hidden images (or other images that cannot have their size calculated) will fail in polyfill browsers. Workarounds can be found in the discussions at #57 and #40.

Child elements removed in browsers without support for CSS filters

Target elements with child elements will have those child elements removed in browsers without support for CSS filters. #61

Event listeners removed in browsers without support for CSS filters

Target elements with event listeners removed in browsers without support for CSS filters. #65

Colored borders

Colored borders will be also converted to grayscale in non-polyfill browsers. To also convert to grayscale in polyfill browsers, the class grayscale-replaced can be used:

.grayscale-replaced {
  border-color: #494949 !important;
}

How it Works

In browsers that support css filters, the styles in gray.css will use CSS filters to turn the image gray.

Modernizr

The jquery.gray plugin uses the Modernizr._prefixes, css-filters, Inline SVG and svg-filters feature detects from Modernizr to determine browser support. If a browser supports inline SVG and SVG filters but not CSS filters, the plugin replaces the elements with SVG elements with filters.

Changelog

  • v1.7.0: Upgrade Modernizr #80
  • v1.6.0: Throw error when modernizr feature detects not in build #71 #72
  • v1.5.0: Add SVG <title> to polyfill for accessibility (demo)
  • v1.4.5: Fix sizing of img tags with padding
  • v1.4.4: Fix bug with missing padding properties with background images
  • v1.4.3: Fix bug with padding on img tag
  • v1.4.2: Add Firefox 35+ support, normalize cross-browser rendering
  • v1.4.1: Fix bug with display: none on parents
  • v1.4.0: Add support for programmatic toggling of grayscale
  • v1.3.6: Fix image size with resized img tag and grayscale-fade in polyfill
  • v1.3.5: Declare variables to fix error in strict mode
  • v1.3.4: Don't override existing modernizr
  • v1.3.3: Update minified js to match source
  • v1.3.2: Remove grayscale on hover in ie6-9 (fade option), use same svg filter in polyfill as in css
  • v1.3.1: Bugfix for empty gray class name
  • v1.3.0: Rename fade class to grayscale-fade to resolve conflict with bootstrap
  • v1.2.0: IE shim: Copy styles from element to replacement element
  • v1.1.1: Improve documentation and demo
  • v1.1.0: Support for background images with basic background-size and background-position
  • v1.0.0: First basic version with support for <img> tags

License

MIT © Karl Horky

More Repositories

1

learn-to-program

Educational resources to learn to program (Foundation in Web Development)
3,473
star
2

awesome-speakers

Awesome speakers in the programming and design communities
759
star
3

pwa-tricks

A collection of helpful tricks for PWAs (Progressive Web Apps)
122
star
4

BlockCursorEverywhere

Sublime Text plugin to display a block cursor
Python
41
star
5

typescript-tricks

A collection of useful TypeScript tricks
37
star
6

talk-notes

notes from talks and conferences
30
star
7

unofficial-guide-to-google-analytics-cookie-banners

Unofficial Guide to Google Analytics Cookie Banners
28
star
8

dotfiles

macOS dotfiles
Shell
20
star
9

github-tricks

A collection of useful GitHub tricks
11
star
10

react-malarkey

JavaScript
6
star
11

talks

Talks and presentations
JavaScript
6
star
12

react-tricks

A collection of useful React tricks
JavaScript
6
star
13

npm-tricks

A collection of useful npm scripts tricks
5
star
14

electron-app-patcher

Patch an Electron app to apply your own changes
TypeScript
5
star
15

frontend-tricks

Helpful web frontend tricks
HTML
5
star
16

vercel-pkg-github-actions-release

Automatic GitHub Releases of packages built with Vercel pkg using GitHub Actions
5
star
17

chartist-plugin-line-tooltip

Plugin for Chartist.js Line Charts
JavaScript
4
star
18

chrome-tricks

A collection of tricks for Google Chrome
4
star
19

archive-webpage-browser-extension

Chrome extension to archive webpages on archive.ph and the Internet Archive Wayback Machine
TypeScript
4
star
20

hyper-no-close

Remove close buttons on tabs in Hyper.app
JavaScript
4
star
21

next-js-tricks

A collection of useful Next.js tricks
4
star
22

react-sc-braces

Braces to indicate elements in diagrams
JavaScript
4
star
23

github-actions-database-persistence

GitHub Actions CRON workflow experiments with persisting CSV, SQLite and PostgreSQL databases to GitHub Actions cache and Git commits
TypeScript
4
star
24

karlhorky.github.io

Personal portfolio site for Karl Horky, front-end engineer.
HTML
3
star
25

renovate-config

3
star
26

recommendations

3
star
27

email-tricks

HTML
3
star
28

git-commit-push-workflow-test

HTML
3
star
29

command-line-tricks

Shell
2
star
30

ts-language-service-plugin-formatting-cli

TypeScript
2
star
31

macos-tricks

A collection of tricks for macOS
2
star
32

userscripts

Violentmonkey / Greasemonkey userscripts for improving the behavior of various websites
JavaScript
2
star
33

flow-tricks

A collection of helpful Flow tricks
JavaScript
2
star
34

refined-gmail-userscript

Userscript to improve Gmail: 1) show badge with unread count 2) always compose in new windows in Gmail
JavaScript
2
star
35

refined-google-search-userscript

Improve design and UX of Google Search
JavaScript
1
star
36

uses

Hardware and Software Setup
1
star
37

design-assets

Design assets collected over the years
1
star
38

test-renovate-config-ban-types-versions

1
star
39

mailgun-js-suppression-notifications-poc

Send a notification if any bounces occur (proof of concept)
JavaScript
1
star
40

google-plus-collections

HTML
1
star
41

postgresql-tricks

A collection of tricks for PostgreSQL
1
star
42

prismjs-vs-code-dark-theme-fix

Illustrates fixes for the VS Code Dark Plus Prisma.js theme
CSS
1
star
43

react-native-empty-string-outside-text-component

Demo for https://github.com/facebook/react-native/issues/35002
Java
1
star
44

karlhorky

GitHub Profile
1
star
45

yarn-1-resolutions-bug

1
star
46

eslint-tricks

A collection of useful ESLint tricks
1
star
47

renovate-repro-npm-overrides

Reproduction for https://github.com/renovatebot/renovate/issues/15278
1
star
48

windows-configfiles

PowerShell
1
star
49

next-app-cypress-es2019-es2022

Reproduction for Cypress + tsconfig.json compilerOptions.target problem https://github.com/cypress-io/cypress/issues/16914#issuecomment-1172877775
TypeScript
1
star
50

react-scripts-demo

⚠️ Abandoned and unmaintained with insecure dependencies
HTML
1
star
51

refined-notion-userscript

Userscript to improve Notion.so: 1) Fix bug where cmd-N shortcut overrides browser "New Window" shortcut
JavaScript
1
star
52

typescript-esm-fully-specified-extensions

Demo of TypeScript errors with missing ESM extensions https://github.com/microsoft/TypeScript/issues/37582#issuecomment-1352776684
TypeScript
1
star
53

codesandbox-tricks

A collection of useful CodeSandbox tricks
1
star
54

jscodeshift-tricks

A collection of helpful tricks for jscodeshift codemods
1
star