Adaptive Backgrounds jQuery Plugin
Join the DZone community and get the full member experience.
Join For FreeI recently found a good resource for changing the background colour of a containing div. This is a jQuery plugin that will search for the most dominating colour in an image and will change it's containing element background to this colour.
Allowing you to create some nice looking showcase pages with just a few lines of Javascript.
This plugin is called the jQuery Adaptive Background Plugin.
To use this jQuery plugin all you have to do is have a page with a number of images on it wrapped around a div like this.
<div class="page-slide"> <img src="./images/water.jpg" alt="Water" data-adaptive-background='1'/> </div> <div class="page-slide"> <img src="./images/water2.jpg" alt="Water 2" data-adaptive-background='1'/> </div> <div class="page-slide"> <img src="./images/water3.jpg" alt="Water 3" data-adaptive-background='1'/> </div> <div class="page-slide"> <img src="./images/water4.jpg" alt="Water 4" data-adaptive-background='1'/> </div> <div class="page-slide"> <img src="./images/water5.jpg" alt="Water 5" data-adaptive-background='1'/> </div>
When the plugin is ran it will search for images which have an attribute of data-adaptive-background and will find the dominate colour in these images.
To setup the Javascript to use the following you first need to download the plugin from the Github project page.
Because this is a JQuery plugin it relies on jQuery being loaded on the page before you can use it.
<head> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script> <script src="./js/jquery.adaptive-backgrounds.js"></script> </head>
With the adaptive background plugin loaded on the page all we have to do is run the adaptiveBackground method to start the plugin.
<script> $(document).ready(function(){ $.adaptiveBackground.run() }); </script>
Now you will see all the wrapper div will have a new background colour of the dominate colour in the image.
View the demo to see how this works.
Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
A React Frontend With Go/Gin/Gorm Backend in One Project
-
What Is Envoy Proxy?
-
Cucumber Selenium Tutorial: A Comprehensive Guide With Examples and Best Practices
-
From On-Prem to SaaS
Comments