Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Adaptive Backgrounds jQuery Plugin

DZone's Guide to

Adaptive Backgrounds jQuery Plugin

· Java Zone ·
Free Resource

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}