Posted By

damanlovett on 08/11/10


Tagged


Versions (?)

Image Slideshow JS file


 / Published in: JavaScript
 

  1. /* Ultimate Fade-in slideshow (v2.4)
  2. * Last updated: May 24th, 2010. This notice must stay intact for usage
  3. * Author: Dynamic Drive at http://www.dynamicdrive.com/
  4. * Visit http://www.dynamicdrive.com/ for full source code
  5. */
  6.  
  7. //Oct 6th, 09' (v2.1): Adds option to randomize display order of images, via new option displaymode.randomize
  8. //May 24th, 10' (v2.4): Adds new "peakaboo" option to "descreveal" setting. oninit and onslide event handlers added.
  9.  
  10. var fadeSlideShow_descpanel={
  11. controls: [['x.png',7,7], ['restore.png',10,11], ['loading.gif',54,55]], //full URL and dimensions of close, restore, and loading images
  12. fontStyle: 'normal 11px Verdana', //font style for text descriptions
  13. slidespeed: 200 //speed of description panel animation (in millisec)
  14. }
  15.  
  16. //No need to edit beyond here...
  17.  
  18. jQuery.noConflict()
  19.  
  20. function fadeSlideShow(settingarg){
  21. this.setting=settingarg
  22. settingarg=null
  23. var setting=this.setting
  24. setting.fadeduration=setting.fadeduration? parseInt(setting.fadeduration) : 500
  25. setting.curimage=(setting.persist)? fadeSlideShow.routines.getCookie("gallery-"+setting.wrapperid) : 0
  26. setting.curimage=setting.curimage || 0 //account for curimage being null if cookie is empty
  27. setting.currentstep=0 //keep track of # of slides slideshow has gone through (applicable in displaymode='auto' only)
  28. setting.totalsteps=setting.imagearray.length*(setting.displaymode.cycles>0? setting.displaymode.cycles : Infinity) //Total steps limit (applicable in displaymode='auto' only w/ cycles>0)
  29. setting.fglayer=0, setting.bglayer=1 //index of active and background layer (switches after each change of slide)
  30. setting.oninit=setting.oninit || function(){}
  31. setting.onslide=setting.onslide || function(){}
  32. if (setting.displaymode.randomize) //randomly shuffle order of images?
  33. setting.imagearray.sort(function() {return 0.5 - Math.random()})
  34. var preloadimages=[] //preload images
  35. setting.longestdesc="" //get longest description of all slides. If no desciptions defined, variable contains ""
  36. for (var i=0; i<setting.imagearray.length; i++){ //preload images
  37. preloadimages[i]=new Image()
  38. preloadimages[i].src=setting.imagearray[i][0]
  39. if (setting.imagearray[i][3] && setting.imagearray[i][3].length>setting.longestdesc.length)
  40. setting.longestdesc=setting.imagearray[i][3]
  41. }
  42. var closebutt=fadeSlideShow_descpanel.controls[0] //add close button to "desc" panel if descreveal="always"
  43. setting.closebutton=(setting.descreveal=="always")? '<img class="close" src="'+closebutt[0]+'" style="float:right;cursor:hand;cursor:pointer;width:'+closebutt[1]+'px;height:'+closebutt[2]+'px;margin-left:2px" title="Hide Description" />' : ''
  44. var slideshow=this
  45. jQuery(document).ready(function($){ //fire on DOM ready
  46. var setting=slideshow.setting
  47. var fullhtml=fadeSlideShow.routines.getFullHTML(setting.imagearray) //get full HTML of entire slideshow
  48. setting.$wrapperdiv=$('#'+setting.wrapperid).css({position:'relative', visibility:'visible', background:'black', overflow:'hidden', width:setting.dimensions[0], height:setting.dimensions[1]}).empty() //main slideshow DIV
  49. if (setting.$wrapperdiv.length==0){ //if no wrapper DIV found
  50. alert("Error: DIV with ID \""+setting.wrapperid+"\" not found on page.")
  51. return
  52. }
  53. setting.$gallerylayers=$('<div class="gallerylayer"></div><div class="gallerylayer"></div>') //two stacked DIVs to display the actual slide
  54. .css({position:'absolute', left:0, top:0, width:'100%', height:'100%', background:'black'})
  55. .appendTo(setting.$wrapperdiv)
  56. var $loadingimg=$('<img src="'+fadeSlideShow_descpanel.controls[2][0]+'" style="position:absolute;width:'+fadeSlideShow_descpanel.controls[2][1]+';height:'+fadeSlideShow_descpanel.controls[2][2]+'" />')
  57. .css({left:setting.dimensions[0]/2-fadeSlideShow_descpanel.controls[2][1]/2, top:setting.dimensions[1]/2-fadeSlideShow_descpanel.controls[2][2]}) //center loading gif
  58. .appendTo(setting.$wrapperdiv)
  59. var $curimage=setting.$gallerylayers.html(fullhtml).find('img').hide().eq(setting.curimage) //prefill both layers with entire slideshow content, hide all images, and return current image
  60. if (setting.longestdesc!="" && setting.descreveal!="none"){ //if at least one slide contains a description (versus feature is enabled but no descriptions defined) and descreveal not explicitly disabled
  61. fadeSlideShow.routines.adddescpanel($, setting)
  62. if (setting.descreveal=="always"){ //position desc panel so it's visible to begin with
  63. setting.$descpanel.css({top:setting.dimensions[1]-setting.panelheight})
  64. setting.$descinner.click(function(e){ //asign click behavior to "close" icon
  65. if (e.target.className=="close"){
  66. slideshow.showhidedescpanel('hide')
  67. }
  68. })
  69. setting.$restorebutton.click(function(e){ //asign click behavior to "restore" icon
  70. slideshow.showhidedescpanel('show')
  71. $(this).css({visibility:'hidden'})
  72. })
  73. }
  74. else if (setting.descreveal=="ondemand"){ //display desc panel on demand (mouseover)
  75. setting.$wrapperdiv.bind('mouseenter', function(){slideshow.showhidedescpanel('show')})
  76. setting.$wrapperdiv.bind('mouseleave', function(){slideshow.showhidedescpanel('hide')})
  77. }
  78. }
  79. setting.$wrapperdiv.bind('mouseenter', function(){setting.ismouseover=true}) //pause slideshow mouseover
  80. setting.$wrapperdiv.bind('mouseleave', function(){setting.ismouseover=false})
  81. if ($curimage.get(0).complete){ //accounf for IE not firing image.onload
  82. $loadingimg.hide()
  83. slideshow.paginateinit($)
  84. slideshow.showslide(setting.curimage)
  85. }
  86. else{ //initialize slideshow when first image has fully loaded
  87. $loadingimg.hide()
  88. slideshow.paginateinit($)
  89. $curimage.bind('load', function(){slideshow.showslide(setting.curimage)})
  90. }
  91. setting.oninit.call(slideshow) //trigger oninit() event
  92. $(window).bind('unload', function(){ //clean up and persist
  93. if (slideshow.setting.persist) //remember last shown image's index
  94. fadeSlideShow.routines.setCookie("gallery-"+setting.wrapperid, setting.curimage)
  95. jQuery.each(slideshow.setting, function(k){
  96. if (slideshow.setting[k] instanceof Array){
  97. for (var i=0; i<slideshow.setting[k].length; i++){
  98. if (slideshow.setting[k][i].tagName=="DIV") //catches 2 gallerylayer divs, gallerystatus div
  99. slideshow.setting[k][i].innerHTML=null
  100. slideshow.setting[k][i]=null
  101. }
  102. }
  103. })
  104. slideshow=slideshow.setting=null
  105. })
  106. })
  107. }
  108.  
  109. fadeSlideShow.prototype={
  110.  
  111. navigate:function(keyword){
  112. var setting=this.setting
  113. clearTimeout(setting.playtimer)
  114. if (setting.displaymode.type=="auto"){ //in auto mode
  115. setting.displaymode.type="manual" //switch to "manual" mode when nav buttons are clicked on
  116. setting.displaymode.wraparound=true //set wraparound option to true
  117. }
  118. if (!isNaN(parseInt(keyword))){ //go to specific slide?
  119. this.showslide(parseInt(keyword))
  120. }
  121. else if (/(prev)|(next)/i.test(keyword)){ //go back or forth inside slide?
  122. this.showslide(keyword.toLowerCase())
  123. }
  124. },
  125.  
  126. showslide:function(keyword){
  127. var slideshow=this
  128. var setting=slideshow.setting
  129. if (setting.displaymode.type=="auto" && setting.ismouseover && setting.currentstep<=setting.totalsteps){ //if slideshow in autoplay mode and mouse is over it, pause it
  130. setting.playtimer=setTimeout(function(){slideshow.showslide('next')}, setting.displaymode.pause)
  131. return
  132. }
  133. var totalimages=setting.imagearray.length
  134. var imgindex=(keyword=="next")? (setting.curimage<totalimages-1? setting.curimage+1 : 0)
  135. : (keyword=="prev")? (setting.curimage>0? setting.curimage-1 : totalimages-1)
  136. : Math.min(keyword, totalimages-1)
  137. var $slideimage=setting.$gallerylayers.eq(setting.bglayer).find('img').hide().eq(imgindex).show() //hide all images except current one
  138. var imgdimensions=[$slideimage.width(), $slideimage.height()] //center align image
  139. $slideimage.css({marginLeft: (imgdimensions[0]>0 && imgdimensions[0]<setting.dimensions[0])? setting.dimensions[0]/2-imgdimensions[0]/2 : 0})
  140. $slideimage.css({marginTop: (imgdimensions[1]>0 && imgdimensions[1]<setting.dimensions[1])? setting.dimensions[1]/2-imgdimensions[1]/2 : 0})
  141. if (setting.descreveal=="peekaboo" && setting.longestdesc!=""){ //if descreveal is set to "peekaboo", make sure description panel is hidden before next slide is shown
  142. clearTimeout(setting.hidedesctimer) //clear hide desc panel timer
  143. slideshow.showhidedescpanel('hide', 0) //and hide it immediately
  144. }
  145. setting.$gallerylayers.eq(setting.bglayer).css({zIndex:1000, opacity:0}) //background layer becomes foreground
  146. .stop().css({opacity:0}).animate({opacity:1}, setting.fadeduration, function(){ //Callback function after fade animation is complete:
  147. clearTimeout(setting.playtimer)
  148. try{
  149. setting.onslide.call(slideshow, setting.$gallerylayers.eq(setting.fglayer).get(0), setting.curimage)
  150. }catch(e){
  151. alert("Fade In Slideshow error: An error has occured somwhere in your code attached to the \"onslide\" event: "+e)
  152. }
  153. if (setting.descreveal=="peekaboo" && setting.longestdesc!=""){
  154. slideshow.showhidedescpanel('show')
  155. setting.hidedesctimer=setTimeout(function(){slideshow.showhidedescpanel('hide')}, setting.displaymode.pause-fadeSlideShow_descpanel.slidespeed)
  156. }
  157. setting.currentstep+=1
  158. if (setting.displaymode.type=="auto"){
  159. if (setting.currentstep<=setting.totalsteps || setting.displaymode.cycles==0)
  160. setting.playtimer=setTimeout(function(){slideshow.showslide('next')}, setting.displaymode.pause)
  161. }
  162. }) //end callback function
  163. setting.$gallerylayers.eq(setting.fglayer).css({zIndex:999}) //foreground layer becomes background
  164. setting.fglayer=setting.bglayer
  165. setting.bglayer=(setting.bglayer==0)? 1 : 0
  166. setting.curimage=imgindex
  167. if (setting.$descpanel){
  168. setting.$descpanel.css({visibility:(setting.imagearray[imgindex][3])? 'visible' : 'hidden'})
  169. if (setting.imagearray[imgindex][3]) //if this slide contains a description
  170. setting.$descinner.empty().html(setting.closebutton + setting.imagearray[imgindex][3])
  171. }
  172. if (setting.displaymode.type=="manual" && !setting.displaymode.wraparound){
  173. this.paginatecontrol()
  174. }
  175. if (setting.$status) //if status container defined
  176. setting.$status.html(setting.curimage+1 + "/" + totalimages)
  177. },
  178.  
  179. showhidedescpanel:function(state, animateduration){
  180. var setting=this.setting
  181. var endpoint=(state=="show")? setting.dimensions[1]-setting.panelheight : this.setting.dimensions[1]
  182. setting.$descpanel.stop().animate({top:endpoint}, (typeof animateduration!="undefined"? animateduration : fadeSlideShow_descpanel.slidespeed), function(){
  183. if (setting.descreveal=="always" && state=="hide")
  184. setting.$restorebutton.css({visibility:'visible'}) //show restore button
  185. })
  186. },
  187.  
  188. paginateinit:function($){
  189. var slideshow=this
  190. var setting=this.setting
  191. if (setting.togglerid){ //if toggler div defined
  192. setting.$togglerdiv=$("#"+setting.togglerid)
  193. setting.$prev=setting.$togglerdiv.find('.prev').data('action', 'prev')
  194. setting.$next=setting.$togglerdiv.find('.next').data('action', 'next')
  195. setting.$prev.add(setting.$next).click(function(e){ //assign click behavior to prev and next controls
  196. var $target=$(this)
  197. slideshow.navigate($target.data('action'))
  198. e.preventDefault()
  199. })
  200. setting.$status=setting.$togglerdiv.find('.status')
  201. }
  202. },
  203.  
  204. paginatecontrol:function(){
  205. var setting=this.setting
  206. setting.$prev.css({opacity:(setting.curimage==0)? 0.4 : 1}).data('action', (setting.curimage==0)? 'none' : 'prev')
  207. setting.$next.css({opacity:(setting.curimage==setting.imagearray.length-1)? 0.4 : 1}).data('action', (setting.curimage==setting.imagearray.length-1)? 'none' : 'next')
  208. if (document.documentMode==8){ //in IE8 standards mode, apply opacity to inner image of link
  209. setting.$prev.find('img:eq(0)').css({opacity:(setting.curimage==0)? 0.4 : 1})
  210. setting.$next.find('img:eq(0)').css({opacity:(setting.curimage==setting.imagearray.length-1)? 0.4 : 1})
  211. }
  212. }
  213.  
  214.  
  215. }
  216.  
  217. fadeSlideShow.routines={
  218.  
  219. getSlideHTML:function(imgelement){
  220. var layerHTML=(imgelement[1])? '<a href="'+imgelement[1]+'" target="'+imgelement[2]+'">\n' : '' //hyperlink slide?
  221. layerHTML+='<img src="'+imgelement[0]+'" style="border-width:0;" />\n'
  222. layerHTML+=(imgelement[1])? '</a>\n' : ''
  223. return layerHTML //return HTML for this layer
  224. },
  225.  
  226. getFullHTML:function(imagearray){
  227. var preloadhtml=''
  228. for (var i=0; i<imagearray.length; i++)
  229. preloadhtml+=this.getSlideHTML(imagearray[i])
  230. return preloadhtml
  231. },
  232.  
  233. adddescpanel:function($, setting){
  234. setting.$descpanel=$('<div class="fadeslidedescdiv"></div>')
  235. .css({position:'absolute', visibility:'hidden', width:'100%', left:0, top:setting.dimensions[1], font:fadeSlideShow_descpanel.fontStyle, zIndex:'1001'})
  236. .appendTo(setting.$wrapperdiv)
  237. $('<div class="descpanelbg"></div><div class="descpanelfg"></div>') //create inner nav panel DIVs
  238. .css({position:'absolute', left:0, top:0, width:setting.$descpanel.width()-8, padding:'4px'})
  239. .eq(0).css({background:'black', opacity:0.7}).end() //"descpanelbg" div
  240. .eq(1).css({color:'white'}).html(setting.closebutton + setting.longestdesc).end() //"descpanelfg" div
  241. .appendTo(setting.$descpanel)
  242. setting.$descinner=setting.$descpanel.find('div.descpanelfg')
  243. setting.panelheight=setting.$descinner.outerHeight()
  244. setting.$descpanel.css({height:setting.panelheight}).find('div').css({height:'100%'})
  245. if (setting.descreveal=="always"){ //create restore button
  246. setting.$restorebutton=$('<img class="restore" title="Restore Description" src="' + fadeSlideShow_descpanel.controls[1][0] +'" style="position:absolute;visibility:hidden;right:0;bottom:0;z-index:1002;width:'+fadeSlideShow_descpanel.controls[1][1]+'px;height:'+fadeSlideShow_descpanel.controls[1][2]+'px;cursor:pointer;cursor:hand" />')
  247. .appendTo(setting.$wrapperdiv)
  248.  
  249.  
  250. }
  251. },
  252.  
  253.  
  254. getCookie:function(Name){
  255. var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
  256. if (document.cookie.match(re)) //if cookie found
  257. return document.cookie.match(re)[0].split("=")[1] //return its value
  258. return null
  259. },
  260.  
  261. setCookie:function(name, value){
  262. document.cookie = name+"=" + value + ";path=/"
  263. }
  264. }

Report this snippet  

You need to login to post a comment.