JAVASCRIPT   70

fadeslideshow.js

Guest on 28th July 2021 01:05:42 AM

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

Raw Paste


Login or Register to edit or fork this paste. It's free.