Dynamically rotating images with jQuery Cycle and ColdFusion
Posted on August 22, 2009, under
ColdFusion
| 7648 Views
jQuery Cycle provides a lightweight slideshow plugin. It is super simple to drop in and create a great looking slideshow. The real trick to is to ColdFusion to grab the images from a directory then randomly sort the results to create a unique slideshow for each visitor.
Production sites may need to add caching for the directory calls.
Complete Code
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.cycle.all.min.js"></script>
<style>
.pics {
height:187px;
width:250px;
padding:3px 5px 5px 3px;
background:#f1f1f1;
border-right:1px solid #d0d0d0;
border-bottom:1px solid #d0d0d0;
}
.slideshow {
position: relative;
width: 260px;
height: 195px;
overflow: hidden;
}
</style>
<cfsilent>
<cfdirectory
action="list"
directory="#expandPath("./images")#"
filter="*.jpg"
name="getImages">
<cfset picsList = valueList(getImages.name)>
<!---
Taken from these helpful posts:
http://www.bennadel.com/blog/219-Randomly-Sorting-A-ColdFusion-List.htm
http://www.mollerus.net/tom/blog/2008/03/creating_randomlyordered_lists.html
--->
<cfset picListArray = ListToArray(picsList)>
<cfset result = arrayNew(1)>
<!--- set to = maximum images to display --->
<cfloop from="1" to="#getImages.recordCount#" index="i">
<cfset randomPos = RandRange(1, ArrayLen(PiclistArray)) />
<cfset ArrayAppend(result, PiclistArray[randomPos]) />
<cfset ArrayDeleteAt(PiclistArray, randomPos) />
</cfloop>
<cfset picsList = arrayToList(result)>
</cfsilent>
<div class="slideshow">
<cfloop list="#picsList#" index="i">
<cfoutput><img src="images/#i#" class="pics" <cfif listgetAt(picsList,1) neq i>style="display:none;"</cfif>></cfoutput>
</cfloop>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('.slideshow').cycle({
fx: 'fade'
});
});
</script>
<script type="text/javascript" src="jquery.cycle.all.min.js"></script>
<style>
.pics {
height:187px;
width:250px;
padding:3px 5px 5px 3px;
background:#f1f1f1;
border-right:1px solid #d0d0d0;
border-bottom:1px solid #d0d0d0;
}
.slideshow {
position: relative;
width: 260px;
height: 195px;
overflow: hidden;
}
</style>
<cfsilent>
<cfdirectory
action="list"
directory="#expandPath("./images")#"
filter="*.jpg"
name="getImages">
<cfset picsList = valueList(getImages.name)>
<!---
Taken from these helpful posts:
http://www.bennadel.com/blog/219-Randomly-Sorting-A-ColdFusion-List.htm
http://www.mollerus.net/tom/blog/2008/03/creating_randomlyordered_lists.html
--->
<cfset picListArray = ListToArray(picsList)>
<cfset result = arrayNew(1)>
<!--- set to = maximum images to display --->
<cfloop from="1" to="#getImages.recordCount#" index="i">
<cfset randomPos = RandRange(1, ArrayLen(PiclistArray)) />
<cfset ArrayAppend(result, PiclistArray[randomPos]) />
<cfset ArrayDeleteAt(PiclistArray, randomPos) />
</cfloop>
<cfset picsList = arrayToList(result)>
</cfsilent>
<div class="slideshow">
<cfloop list="#picsList#" index="i">
<cfoutput><img src="images/#i#" class="pics" <cfif listgetAt(picsList,1) neq i>style="display:none;"</cfif>></cfoutput>
</cfloop>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('.slideshow').cycle({
fx: 'fade'
});
});
</script>
UPDATE: Michael Evangelista has a very good advanced post on this subject - Visit his post here
