jquery.magnifier.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* jQuery Image Magnify script v1.1
  2. * 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. * Nov 16th, 09 (v1.1): Adds ability to dynamically apply/reapply magnify effect to an image, plus magnify to a specific width in pixels.
  6. * Feb 8th, 11 (v1.11): Fixed bug that caused script to not work in newever versions of jQuery (ie: v1.4.4)
  7. */
  8. jQuery.noConflict()
  9. jQuery.imageMagnify={
  10. dsettings: {
  11. magnifyby: 3, //default increase factor of enlarged image
  12. duration: 500, //default duration of animation, in millisec
  13. imgopacity: 0.2 //opacify of original image when enlarged image overlays it
  14. },
  15. cursorcss: 'url(magnify.cur), -moz-zoom-in', //Value for CSS's 'cursor' attribute, added to original image
  16. zIndexcounter: 100,
  17. refreshoffsets:function($window, $target, warpshell){
  18. var $offsets=$target.offset()
  19. var winattrs={x:$window.scrollLeft(), y:$window.scrollTop(), w:$window.width(), h:$window.height()}
  20. warpshell.attrs.x=$offsets.left //update x position of original image relative to page
  21. warpshell.attrs.y=$offsets.top
  22. warpshell.newattrs.x=winattrs.x+winattrs.w/2-warpshell.newattrs.w/2
  23. warpshell.newattrs.y=winattrs.y+winattrs.h/2-warpshell.newattrs.h/2
  24. if (warpshell.newattrs.x<winattrs.x+5){ //no space to the left?
  25. warpshell.newattrs.x=winattrs.x+5
  26. }
  27. else if (warpshell.newattrs.x+warpshell.newattrs.w > winattrs.x+winattrs.w){//no space to the right?
  28. warpshell.newattrs.x=winattrs.x+5
  29. }
  30. if (warpshell.newattrs.y<winattrs.y+5){ //no space at the top?
  31. warpshell.newattrs.y=winattrs.y+5
  32. }
  33. },
  34. magnify:function($, $target, options){
  35. var setting={} //create blank object to store combined settings
  36. var setting=jQuery.extend(setting, this.dsettings, options)
  37. var attrs=(options.thumbdimensions)? {w:options.thumbdimensions[0], h:options.thumbdimensions[1]} : {w:$target.outerWidth(), h:$target.outerHeight()}
  38. var newattrs={}
  39. newattrs.w=(setting.magnifyto)? setting.magnifyto : Math.round(attrs.w*setting.magnifyby)
  40. newattrs.h=(setting.magnifyto)? Math.round(attrs.h*newattrs.w/attrs.w) : Math.round(attrs.h*setting.magnifyby)
  41. $target.css('cursor', jQuery.imageMagnify.cursorcss)
  42. if ($target.data('imgshell')){
  43. $target.data('imgshell').$clone.remove()
  44. $target.css({opacity:1}).unbind('click.magnify')
  45. }
  46. var $clone=$target.clone().css({position:'absolute', left:0, top:0, visibility:'hidden', border:'1px solid gray', cursor:'pointer'}).appendTo(document.body)
  47. $clone.data('$relatedtarget', $target) //save $target image this enlarged image is associated with
  48. $target.data('imgshell', {$clone:$clone, attrs:attrs, newattrs:newattrs})
  49. $target.bind('click.magnify', function(e){ //action when original image is clicked on
  50. var $this=$(this).css({opacity:setting.imgopacity})
  51. var imageinfo=$this.data('imgshell')
  52. jQuery.imageMagnify.refreshoffsets($(window), $this, imageinfo) //refresh offset positions of original and warped images
  53. var $clone=imageinfo.$clone
  54. $clone.stop().css({zIndex:++jQuery.imageMagnify.zIndexcounter, left:imageinfo.attrs.x, top:imageinfo.attrs.y, width:imageinfo.attrs.w, height:imageinfo.attrs.h, opacity:0, visibility:'visible', display:'block'})
  55. .animate({opacity:1, left:imageinfo.newattrs.x, top:imageinfo.newattrs.y, width:imageinfo.newattrs.w, height:imageinfo.newattrs.h}, setting.duration,
  56. function(){ //callback function after warping is complete
  57. //none added
  58. }) //end animate
  59. }) //end click
  60. $clone.click(function(e){ //action when magnified image is clicked on
  61. var $this=$(this)
  62. var imageinfo=$this.data('$relatedtarget').data('imgshell')
  63. jQuery.imageMagnify.refreshoffsets($(window), $this.data('$relatedtarget'), imageinfo) //refresh offset positions of original and warped images
  64. $this.stop().animate({opacity:0, left:imageinfo.attrs.x, top:imageinfo.attrs.y, width:imageinfo.attrs.w, height:imageinfo.attrs.h}, setting.duration,
  65. function(){
  66. $this.hide()
  67. $this.data('$relatedtarget').css({opacity:1}) //reveal original image
  68. }) //end animate
  69. }) //end click
  70. }
  71. };
  72. jQuery.fn.imageMagnify=function(options){
  73. var $=jQuery
  74. return this.each(function(){ //return jQuery obj
  75. var $imgref=$(this)
  76. if (this.tagName!="IMG")
  77. return true //skip to next matched element
  78. if (parseInt($imgref.css('width'))>0 && parseInt($imgref.css('height'))>0 || options.thumbdimensions){ //if image has explicit width/height attrs defined
  79. jQuery.imageMagnify.magnify($, $imgref, options)
  80. }
  81. else if (this.complete){ //account for IE not firing image.onload
  82. jQuery.imageMagnify.magnify($, $imgref, options)
  83. }
  84. else{
  85. $(this).bind('load', function(){
  86. jQuery.imageMagnify.magnify($, $imgref, options)
  87. })
  88. }
  89. })
  90. };
  91. jQuery.fn.applyMagnifier=function(options){ //dynamic version of imageMagnify() to apply magnify effect to an image dynamically
  92. var $=jQuery
  93. return this.each(function(){ //return jQuery obj
  94. var $imgref=$(this)
  95. if (this.tagName!="IMG")
  96. return true //skip to next matched element
  97. })
  98. };
  99. //** The following applies the magnify effect to images with class="magnify" and optional "data-magnifyby" and "data-magnifyduration" attrs
  100. //** It also looks for links with attr rel="magnify[targetimageid]" and makes them togglers for that image
  101. jQuery(document).ready(function($){
  102. var $targets=$('.magnify')
  103. $targets.each(function(i){
  104. var $target=$(this)
  105. var options={}
  106. if ($target.attr('data-magnifyto'))
  107. options.magnifyto=parseFloat($target.attr('data-magnifyto'))
  108. if ($target.attr('data-magnifyby'))
  109. options.magnifyby=parseFloat($target.attr('data-magnifyby'))
  110. if ($target.attr('data-magnifyduration'))
  111. options.duration=parseInt($target.attr('data-magnifyduration'))
  112. $target.imageMagnify(options)
  113. })
  114. var $triggers=$('a[rel^="magnify["]')
  115. $triggers.each(function(i){
  116. var $trigger=$(this)
  117. var targetid=$trigger.attr('rel').match(/\[.+\]/)[0].replace(/[\[\]']/g, '') //parse 'id' from rel='magnify[id]'
  118. $trigger.data('magnifyimageid', targetid)
  119. $trigger.click(function(e){
  120. $('#'+$(this).data('magnifyimageid')).trigger('click.magnify')
  121. e.preventDefault()
  122. })
  123. })
  124. })