March 12, 2010
Developer In-Depth:
prev
next
- podcast
- research
- search
- security
- technology
- video
- AIM
- Alfresco
- Collaboration
- ECM
- ESX
- Hyper-V
- IE8
- Internet Explorer
- Iomega
- Linux
- MIX08
- Microsoft
- NAS
- Nokia
- REV
- S60
- SaaS
- Sharepoint
- Silverlight
- Sony Ericsson
- VMware
- Windows Live
- YouTube
- advertising
- backup
- beta test
- blogs
- convergence
- display
- enterprise
- humans
- instant messaging
- multimedia
- networking
- open source
- phishing
Create an Image Rotator in SharePoint Using jQuery
Forget about workflow, data view web parts, records management, SP 2010, and all that good stuff. These days, the only thing anyone wants to do with SharePoint is to create an image rotator. This article explains how create one in SharePoint.Getting Started
To create an image rotator, you need the following:-
* Some images
* Access to a SharePoint environment
* An image library in your environment
* Enough privileges to add a content editor web part to a page
Build the Cat Rotator
We'll use jQuery in a content editor web part to create the rotator. We do this by adding a content editor web part (CEWP) to our web site and inserting some JavaScript to do the heavy lifting. The basic idea is as follows:-
* Add JavaScript to the CEWP via the "Modify Source" option on the web part.
* Insert the JavaScript necessary to reference the jQuery library from Microsoft's content delivery network (CDN).
* Insert the JavaScript that uses jQuery to swap images every few seconds.
* Insert the original image, taking care to name it via the ID attribute.
<!-- SECTION 1.0: Initial cat image and image placeholder -->
<div id='catPicturesID'>
<img id="CatImage" src="/jQuery/Cat%20Pictures/cat01.jpg">
</div>
<!-- SECTION 2.0: Content Delivery Network (CDN) -->
<script src="https://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
<!-- SECTION 3.0: Core Rotator Functionality -->
<script type="text/javascript">
var i = 2; // Start with the 2nd image since the first one is already displayed
<!-- SECTION 3.1: Heavy lifting -->
function RotateCat ()
{
if (i == 4) i = 1; // Loop back to the first image.
$("#CatImage").attr("src","/jQuery/Cat%20Pictures/cat0" + i + ".jpg");
i++;
} // function RotateCat()
<!-- SECTION 3.2: Timer -->
$(function() {
setInterval('RotateCat()',3000);
});
</script>
Let's examine this section by section.
SECTION 1.0: Initial Image
This sets up an img tag with an ID, "CatImage" as well as our intial cat image, "cat01.jpg". Replace "/jQuery/Cat%20Pictures" with the path to cat01 in your environment.
SECTION 2.0: Content Delivery Network
Consider the first line of code:
<script src="https://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
This magic accesses version 1.3.2 of the jQuery library directly from Microsoft's content delivery network and makes it available to you. Learn more about the idea of a content delivery network from this link: http://www.asp.net/ajaxlibrary/cdn.ashx
The bottom line is that this makes the jQuery library available to you in an optimal fashion.
SECTION 3.0: Core Rotator Functionality
This section is where we define our variable, "i" to keep track of our cat index. We initialize it to the value "2" as this will be the next image we display. Image #1 was already displayed by way of SECTION 1 above.
SECTION 3.1: Heavy Lifting
The interesting stuff happens here.
function RotateCat ()
{
if (i == 4) i = 1; // Loop back to the first image.
$("#CatImage").attr("src","/jQuery/Cat%20Pictures/cat0" + i + ".jpg");
i++;
} // function RotateCat()
We have three cat images. We're using our trusty variable "i" to index to the right image. If i = 4 then we want to loop back to the first image.
This is the golden line of code, however:
$("#CatImage").attr("src","/jQuery/Cat%20Pictures/cat0" + i + ".jpg");
This invoke some jQuery functions to change the src attribute on an HTML tag whose ID = "CatImage". Specifically, we'll change the img attribute to point to a new image in our cat library based on the value of our cat index variable. It's that simple.
SECTION 3.2: Timer
All that remains is to get or image rotator to run on a schedule. Do that with this bit of JavaScript:
<!-- SECTION 3.2: Timer -->
$(function() {
setInterval('RotateCat()',3000);
});
The above is saying, "every 3,000 milliseconds, run a javacript function called 'RotateCat()'."
Extending the Solution
This article describes how to create an image rotator but it has one fairly significant restraint. It assumes a fixed number of images following a strict naming convention. It would be much better if we could point our JavaScript to an image library and rotate through all of the images in the library. Doing that is a bit beyond the scope of this tip, but rest assured, it's possible. Look for a future article to explain how. jQuery isn't required to implement this solution, but it sure makes it easy. It's hard to beat the simplicity of this line of code:
$("#CatImage").attr("src","/jQuery/Cat%20Pictures/cat0" + i + ".jpg");
That one line represents dozens of painstaking plain-old JavaScript code lines, especially since jQuery is works across all major browsers.
Once you get the hang of it, it can take literally minutes to set up your own image rotator. You can have multiple rotators on the same page or across sites.
Now there's nothing in your way to dazzle your end user community with an image rotator.
Networking Solutions
Most Popular Stories
- 1 Building SharePoint Suggestion Boxes and Soliciting Anonymous Feedback
- 2 Provide SharePoint Single Sign-On with Active Directory Federation Services
- 3 Solve Item-Level Permission Performance Problems in SharePoint
- 4 Leveraging SharePoint as a Document Management System
- 5 Developing SharePoint Solutions in Visual Studio Using WSPBuilder
- 1 Building SharePoint Suggestion Boxes and Soliciting Anonymous Feedback
- 2 Solve Item-Level Permission Performance Problems in SharePoint
- 3 Leveraging SharePoint as a Document Management System
- 4 Provide SharePoint Single Sign-On with Active Directory Federation Services
- 5 SharePoint 2010 Makes the Link to LINQ
- 6 Using REST to Add and Delete List Items in SharePoint 2010
- 7 Developing SharePoint Solutions in Visual Studio Using WSPBuilder

While Java helps in fulfilling the promise of "write once, use anywhere", there are practical concerns developers need to address in developing their code, whether it's porting from another language, working in the best IDE or optimizing for today's multi-core computing environments. This eBook will help you in understanding these issues and how you can get the most out of your Java code.