Online Shopping : Computers : Programming : Languages : JavaScript : Images

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

24 of 30 people (80%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How can I animate zooming in and out of an image?

Mar 31st, 2009 10:33
engatoo engatoo, Roj Merry, Mansour suliman, alex martin, John Martin, Martin Honnen,


The following code works with IE4+ and NN6 which allow dynamically 
setting of the width and height of an IMG element with
  document.imageName.width = someWidth
  document.imageName.height = someHeight
<HTML>
<HEAD>
<TITLE>
Image zoom animation
</TITLE>
<SCRIPT>
ImageZoom.prototype.ratio = 2;
ImageZoom.prototype.step = 10;
ImageZoom.prototype.delay = 100;
function ImageZoom (args) {
  this.id = ImageZoom.objects.length;
  ImageZoom.objects[this.id] = this;
  for (var p in args)
    this[p] = args[p];
  this.initZoom();
}
function ImageZoom_initZoom () {
  this.startWidth = this.width = parseInt(this.imgElement.width);
  this.startHeight = this.height = parseInt(this.imgElement.height);
  if (typeof this.endWidth == 'undefined')
    this.endWidth = this.startWidth * this.ratio;
  if (typeof this.endHeight == 'undefined')
    this.endHeight = this.startHeight * this.ratio;
  this.zoom();
}
ImageZoom.prototype.initZoom = ImageZoom_initZoom;
function ImageZoom_zoom () {
  if (this.step > 0) {
    if (this.width + this.step > this.endWidth)
      this.width = this.endWidth;
    else 
      this.width += this.step;
    if (this.height + this.step > this.endHeight)
      this.height = this.endHeight;
    else
      this.height += this.step;
  }
  else {
    if (this.width + this.step < this.endWidth)
      this.width = this.endWidth;
    else 
      this.width += this.step;
    if (this.height + this.step < this.endHeight)
      this.height = this.endHeight;
    else
      this.height += this.step;
  }
  this.imgElement.width = this.width;
  this.imgElement.height = this.height;
  if (this.width != this.endWidth || this.height != this.endHeight)
    this.tid = setTimeout('ImageZoom.objects[' + this.id + '].zoom
();', 
this.delay);
}
ImageZoom.prototype.zoom = ImageZoom_zoom;
ImageZoom.objects = new Array();
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="gui">
<INPUT TYPE="button" 
       VALUE="zoom 200%"
       ONCLICK="new ImageZoom({imgElement: document.imageName})"
>
<INPUT TYPE="button" 
       VALUE="zoom 50%"
       ONCLICK="new ImageZoom({imgElement: document.imageName, step: -
10, ratio: 0.5})"
>
<INPUT TYPE="button" 
       VALUE="zoom out"
       ONCLICK="new ImageZoom({imgElement: document.imageName, step: -
10, endWidth: 1, endHeight: 1})"
>
</FORM>
<IMG NAME="imageName"
     SRC="kiboInside.gif"
     WIDTH="1" HEIGHT="1"
     ONLOAD="new ImageZoom ({imgElement: this, endWidth: 200, 
endHeight: 150});"
>
</BODY>
</HTML>
Note that the IMG starts with initial WIDTH/HEIGHT="1" and zooms out 
to 
that value instead of 0 due to a bug in NN6.
Here follows the extension of the above code for NN4 where you need to 
rewrite a layer with an IMG tag of varying dimensions. The code is of 
restricted use as NN4 can't reflow the page so a growing image might 
hide other page elements.
<HTML>
<HEAD>
<TITLE>
Image zoom animation
</TITLE>
<SCRIPT>
ImageZoom.prototype.ratio = 2;
ImageZoom.prototype.step = 10;
ImageZoom.prototype.delay = 100;
function ImageZoom (args) {
  this.id = ImageZoom.objects.length;
  ImageZoom.objects[this.id] = this;
  for (var p in args)
    this[p] = args[p];
  this.initZoom();
}
function ImageZoom_initZoom () {
  if (!document.layers || document.layers && !this.imgElement.ol) {
    this.startWidth = this.width = parseInt(this.imgElement.width);
    this.startHeight = this.height = parseInt(this.imgElement.height);
  }
  else {
    this.startWidth = this.width = 
      parseInt(this.imgElement.ol.document.images[0].width);
    this.startHeight = this.height = 
      parseInt(this.imgElement.ol.document.images[0].height);
  }
  if (typeof this.endWidth == 'undefined')
    this.endWidth = this.startWidth * this.ratio;
  if (typeof this.endHeight == 'undefined')
    this.endHeight = this.startHeight * this.ratio;
  if (document.layers) {
    if (!this.imgElement.ol) {
      var ol = this.imgElement.ol = new Layer(this.width);
      ol.left = this.imgElement.x;
      ol.top = this.imgElement.y;
      ol.bgColor = document.bgColor;
    }
    this.ol = this.imgElement.ol;
  }
  this.zoom();
}
http://automobile.easyfindindia.com/
http://bollywood.easyfindindia.com/
http://cloth-shopping.onlineindiadiary.com/
http://easyfind.j4ulive.com/
http://www.onlineindiadiary.com/
http://finance-guide.easyfindindia.com/
http://freelance-jobs.onlineindiadiary.com/
http://gadgets.easyfindindia.com/
http://www.easyfindindia.com/
http://gift-ideas.easyfindindia.com/
http://health-fitness.easyfindindia.com/
http://home-improvement.j4ulive.com/
http://homedecor.easyfindindia.com/
http://mortgage.onlineindiadiary.com/
http://online-business.j4ulive.com/
http://online-education.j4ulive.com/
http://onlinein.j4ulive.com/
http://pets-online.easyfindindia.com/
http://realestate.easyfindindia.com/
http://sports-in-india.easyfindindia.com/
http://travel-guide.easyfindindia.com/
http://wedding-shopping.j4ulive.com/
http://www.j4ulive.com/
ImageZoom.prototype.initZoom = ImageZoom_initZoom;
function ImageZoom_zoom () {
  if (this.step > 0) {
    if (this.width + this.step > this.endWidth)
      this.width = this.endWidth;
    else 
      this.width += this.step;
    if (this.height + this.step > this.endHeight)
      this.height = this.endHeight;
    else
      this.height += this.step;
  }
  else {
    if (this.width + this.step < this.endWidth)
      this.width = this.endWidth;
    else 
      this.width += this.step;
    if (this.height + this.step < this.endHeight)
      this.height = this.endHeight;
    else
      this.height += this.step;
  }
  if (document.layers) {
    var ol = this.ol;
    var html = '';
    html += '<IMG SRC="' + this.imgElement.src + '"';
    html += ' WIDTH="' + this.width + '"';
    html += ' HEIGHT="' + this.height + '"';
    html += '>';
    ol.document.open();
    ol.document.write(html);
    ol.document.close();
    ol.clip.width = 
      this.width < this.imgElement.width ? this.imgElement.width : 
this.width;
    ol.clip.height =
      this.height < this.imgElement.height ? this.imgElement.height : 
this.height;
    ol.visibility = 'show';
  }
  else {
    this.imgElement.width = this.width;
    this.imgElement.height = this.height;
  }
  if (this.width != this.endWidth || this.height != this.endHeight)
    this.tid = setTimeout('ImageZoom.objects[' + this.id + '].zoom
();', 
this.delay);
}
ImageZoom.prototype.zoom = ImageZoom_zoom;
ImageZoom.objects = new Array();
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="gui">
<INPUT TYPE="button" 
       VALUE="zoom 200%"
       ONCLICK="new ImageZoom({imgElement: document.imageName})"
>
<INPUT TYPE="button" 
       VALUE="zoom 50%"
       ONCLICK="new ImageZoom({imgElement: document.imageName, step: -
10, ratio: 0.5})"
>
<INPUT TYPE="button" 
       VALUE="zoom out"
       ONCLICK="new ImageZoom({imgElement: document.imageName, step: -
10, endWidth: 1, endHeight: 1})"
>
</FORM>
<IMG NAME="imageName"
     SRC="kiboInside.gif"
     WIDTH="1" HEIGHT="1"
     ONLOAD="new ImageZoom ({imgElement: this, endWidth: 200, 
endHeight: 150});"
>
</BODY>
</HTML>
http://www.webs4soft.com/Links4.htm
http://hotelsinindia.webs4soft.com/hotels-in-delhi.htm
http://indiatravel.webs4soft.com/Resources.htm
http://indianmovies.webs4soft.com/Kuch-Kuch-Hota-Hai.htm
http://real-estate.webs4soft.com/property-tips.htm
http://foodhealthcaretips.webs4soft.com/Resources.htm
http://www.websitecompanyindia.com/seo-Links.htm
http://www.websitecompanyindia.com/seo-Links.htm
http://indianjewelry.websitecompanyindia.com/dimond-ring.htm
http://www.bestindiaeducation.com/Link-Exchange.htm
http://bestjobconsultant.bestindiaeducation.com/PARTNERS.htm
http://eloctronicandmobilestore.bestindiaeducation.com/mobile-links.htm
http://easyloanservice.bestindiaeducation.com/Home-Insurance-Links.htm
http://www.rajhealthcenter.com/Cosmetic-Surgery.htm
http://creativebusinessgroup.rajhealthcenter.com/Business-Links.htm
http://onlinefreeinternetgames.rajhealthcenter.com/Games-Links.htm
http://top-beauty-tips.rajhealthcenter.com/Beauty-Links.htm
http://www.indiatourpoint.com/Travel-Links.htm
http://four-wheeler-buy-tips.indiatourpoint.com/Auto-Links.htm
http://watch-online-free-cricket-match.indiatourpoint.com/Sports-
Links.htm
http://automobile.easyfindindia.com/
http://bollywood.easyfindindia.com/
http://cloth-shopping.onlineindiadiary.com/
http://easyfind.j4ulive.com/
http://www.onlineindiadiary.com/
http://finance-guide.easyfindindia.com/
http://freelance-jobs.onlineindiadiary.com/
http://gadgets.easyfindindia.com/
http://www.easyfindindia.com/
http://gift-ideas.easyfindindia.com/
http://health-fitness.easyfindindia.com/
http://home-improvement.j4ulive.com/
http://homedecor.easyfindindia.com/
http://mortgage.onlineindiadiary.com/
http://online-business.j4ulive.com/
http://online-education.j4ulive.com/
http://onlinein.j4ulive.com/
http://pets-online.easyfindindia.com/
http://realestate.easyfindindia.com/
http://sports-in-india.easyfindindia.com/
http://travel-guide.easyfindindia.com/
http://wedding-shopping.j4ulive.com/
http://www.j4ulive.com/
http://wildlifeholidayindia.blogspot.com/
http://enjoywildlife.blogspot.com/
http://enjoytourismindia.blogspot.com/
http://directory.indiatourpoint.com/
http://www.bestlifeindia.com/Resources1.htm
http://directory.bestlifeindia.com/
http://freeorkutscrapandsms.bestlifeindia.com/SMS-Links.htm
http://onlinegiftshop.bestlifeindia.com/Gift-Links.htm
http://www.freemusicpoint.com/Music-Links.htm
http://love-dating.freemusicpoint.com
http://online-art-presentation.freemusicpoint.com/
http://onlinefurnitureshop.freemusicpoint.com/
http://www.roo7e.com
http://www.roo7e.com
http://www.al33ab.com
http://ksacam.com
http://chat.ksacam.com
http://ksacam.info
http://oo2o.com
http://s4cam.com
http://saudi4cam.com				
http://voice.ksacam.com
http://سعودي-كام.oo2o.com
http://سعوديكام.com
http://xn--mgbply5cnrr.com
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/
http://www.3rabstarz.com/vb
http://www.3rabstarz.com/
http://www.dir.3rabstarz.com/
http://www.up.3rabstarz.com
http://www.3rabstarz.com/vb/forumdisplay.php?f=5
http://www.3rabstarz.com/vb/forumdisplay.php?f=116
http://www.3rabstarz.com/vb/forumdisplay.php?f=66
http://www.3rabstarz.com/vb/forumdisplay.php?f=78
http://www.3rabstarz.com/vb/forumdisplay.php?f=147
http://www.3rabstarz.com/vb/forumdisplay.php?f=2
http://www.3rabstarz.com/vb/forumdisplay.php?f=31
http://www.3rabstarz.com/vb/forumdisplay.php?f=136
http://www.3rabstarz.com/vb/forumdisplay.php?f=34
http://www.3rabstarz.com/vb/forumdisplay.php?f=89
http://www.3rabstarz.com/vb/forumdisplay.php?f=26
http://www.3rabstarz.com/vb/forumdisplay.php?f=72
http://www.3rabstarz.com/vb/forumdisplay.php?f=73
http://www.3rabstarz.com/vb/forumdisplay.php?f=149
http://www.3rabstarz.com/vb/forumdisplay.php?f=150
http://www.3rabstarz.com/vb/forumdisplay.php?f=151
http://www.3rabstarz.com/vb/forumdisplay.php?f=152
http://www.3rabstarz.com/vb/forumdisplay.php?f=67
http://www.3rabstarz.com/vb/forumdisplay.php?f=3
http://www.3rabstarz.com/vb/forumdisplay.php?f=37
http://www.3rabstarz.com/vb/forumdisplay.php?f=115
http://www.3rabstarz.com/vb/forumdisplay.php?f=76
http://www.3rabstarz.com/vb/forumdisplay.php?f=77
http://www.3rabstarz.com/vb/forumdisplay.php?f=36
http://www.3rabstarz.com/vb/forumdisplay.php?f=153
http://www.3rabstarz.com/vb/forumdisplay.php?f=122
http://www.3rabstarz.com/vb/forumdisplay.php?f=118
http://www.3rabstarz.com/vb/forumdisplay.php?f=119
http://www.3rabstarz.com/vb/forumdisplay.php?f=120
http://www.3rabstarz.com/vb/forumdisplay.php?f=121
http://www.3rabstarz.com/vb/forumdisplay.php?f=35
http://www.3rabstarz.com/vb/forumdisplay.php?f=65
http://www.3rabstarz.com/vb/forumdisplay.php?f=7
http://www.3rabstarz.com/vb/forumdisplay.php?f=9
http://www.3rabstarz.com/vb/forumdisplay.php?f=11
http://www.3rabstarz.com/vb/forumdisplay.php?f=142
http://www.3rabstarz.com/vb/forumdisplay.php?f=143
http://www.3rabstarz.com/vb/forumdisplay.php?f=146
http://www.3rabstarz.com/vb/forumdisplay.php?f=140
http://www.3rabstarz.com/vb/forumdisplay.php?f=137
http://www.3rabstarz.com/vb/forumdisplay.php?f=114
http://www.3rabstarz.com/vb/forumdisplay.php?f=117
http://www.3rabstarz.com/vb/forumdisplay.php?f=113
http://www.3rabstarz.com/vb/forumdisplay.php?f=51
http://www.3rabstarz.com/vb/forumdisplay.php?f=84
http://www.3rabstarz.com/vb/forumdisplay.php?f=138
http://www.3rabstarz.com/vb/forumdisplay.php?f=139
http://www.3rabstarz.com/vb/forumdisplay.php?f=15
http://www.3rabstarz.com/vb/forumdisplay.php?f=130
http://www.3rabstarz.com/vb/forumdisplay.php?f=97
http://www.3rabstarz.com/vb/forumdisplay.php?f=102
http://www.3rabstarz.com/vb/forumdisplay.php?f=101
http://www.3rabstarz.com/vb/forumdisplay.php?f=104
http://www.3rabstarz.com/vb/forumdisplay.php?f=131
http://www.3rabstarz.com/vb/forumdisplay.php?f=103
http://www.3rabstarz.com/vb/forumdisplay.php?f=99
http://www.3rabstarz.com/vb/forumdisplay.php?f=59
http://www.3rabstarz.com/vb/forumdisplay.php?f=60
http://www.3rabstarz.com/vb/forumdisplay.php?f=39
http://www.3rabstarz.com/vb/forumdisplay.php?f=40
http://www.3rabstarz.com/vb/forumdisplay.php?f=42
http://www.3rabstarz.com/vb/forumdisplay.php?f=4
http://www.3rabstarz.com/vb/forumdisplay.php?f=1
http://www.3rabstarz.com/vb/forumdisplay.php?f=88
http://www.3rabstarz.com/vb/forumdisplay.php?f=148
http://www.3rabstarz.com/vb/forumdisplay.php?f=62
http://www.3rabstarz.com/vb/forumdisplay.php?f=63
http://www.3rabstarz.com/vb/forumdisplay.php?f=64
http://www.3rabstarz.com/vb/forumdisplay.php?f=6
http://www.3rabstarz.com/vb/forumdisplay.php?f=12
http://www.3rabstarz.com/vb/forumdisplay.php?f=96
http://www.3rabstarz.com/vb/forumdisplay.php?f=58
http://www.3rabstarz.com/vb/forumdisplay.php?f=38