Over a million developers have joined DZone.

Adaptive Backgrounds jQuery Plugin

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

I 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.

adaptive-background

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.

Adaptive Background Plugin

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.

Demo

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

Published at DZone with permission of Paul Underwood, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}