JavaScript
  Home arrow JavaScript arrow Page 4 - Building Image Zooming Controls with the D...
IBM Developerworks
The Best Selling PC Migration Utility.
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Download TestComplete 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Building Image Zooming Controls with the DOM for JavaScript
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-03-25

    Table of Contents:
  • Building Image Zooming Controls with the DOM for JavaScript
  • The previous version of the image zooming application
  • Building zoom controls by using some DOM scripting
  • Source code for the image zooming application

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Building Image Zooming Controls with the DOM for JavaScript - Source code for the image zooming application
    (Page 4 of 4 )

    As I stated in the section you just read, below I listed the definitive source code corresponding to this JavaScript-driven zooming application. This time it will incorporate all of the improvements that were progressively implemented during the previous tutorials of the series:


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>Zooming images with JavaScript (targets any web page element)</title>

    <style type="text/css">

    body{

    padding: 0;

    margin: 0;

    background: #fff;

    }

    h1{

    font: bold 14pt Arial, Helvetica, sans-serif;

    color: #000;

    text-align: center;

    }

    img{

    position: absolute;

    }

    #container{

    position: relative;

    width: 263px;

    height: 150px;

    margin-left: auto;

    margin-right: auto;

    overflow: hidden;

    background: #9cf;

    border: 1px solid #000;

    }

    #controlpanel{

    width: 263px;

    padding: 10px 0 10px 0;

    margin-left: auto;

    margin-right: auto;

    background: #9cf;

    text-align: center;

    border: 1px solid #000;

    }

    </style>

    <script type="text/javascript">

    // zoom in web page element

    function ZoomInElement(id,inc,maxWidth){

     var elem=document.getElementById(id);

     if(!elem||elem.getAttribute('width')>=maxWidth){return};

    elem.setAttribute('width',parseInt(elem.getAttribute('width'))+inc);

    elem.setAttribute('height',parseInt(elem.getAttribute('height'))+inc);

    if(!elem.style.left){elem.style.left='0px'};

    if(!elem.style.top){elem.style.top='0px'};

    elem.style.left=parseInt(elem.style.left)-(inc/2)+'px';

    elem.style.top=parseInt(elem.style.top)-(inc/2)+'px';

    }

    // zoom out web page element

    function ZoomOutElement(id,inc,minWidth){

     var elem=document.getElementById(id);

     if(!elem||elem.getAttribute('width')<=minWidth){return};

    elem.setAttribute('width',parseInt(elem.getAttribute('width'))-inc);

    elem.setAttribute('height',parseInt(elem.getAttribute('height'))-inc);

    if(!elem.style.left){elem.style.left='0px'};

    if(!elem.style.top){elem.style.top='0px'};

    elem.style.left=parseInt(elem.style.left)+(inc/2)+'px';

    elem.style.top=parseInt(elem.style.top)+(inc/2)+'px';

    }

    // build zoom panel

    function buildZoomPanel(){

    var div=document.createElement('div');

    div.setAttribute('id','controlpanel');

    var btnplus=document.createElement('input');

    btnplus.setAttribute('type','button');

    btnplus.setAttribute('value','+');

    var btnminus=document.createElement('input');

    btnminus.setAttribute('type','button');

    btnminus.setAttribute('value','-');

    div.appendChild(btnplus);

    div.appendChild(btnminus);

    document.getElementsByTagName('body')[0].appendChild(div);

    btnplus.onclick=function(){ZoomInElement('image',20,800)};

    btnminus.onclick=function(){ZoomOutElement('image',20,263)};

    }

    // display zoom panel when web page is loaded

    window.onload=function(){

    if(document.getElementById&&document.getElementsByTagName&&document.createTextNode){

    buildZoomPanel();

    }

    }

    </script>

    </head>

    <body>

    <h1>Zooming elements with JavaScript (targets any web page element)</h1>

    <div id="container"><img src="sample_image.jpg" width="263" height="150" id="image" /></div>

    </body>

    </html>


    That's all the source code required to implement this extensible zooming application on your own computer successfully. In addition, I also included a couple of screen shots that show how a sample image can be easily zoomed out using the application in question.

    Here are the aforementioned screen shots:




    Naturally, you're totally free to tweak all of the code samples developed in this tutorial, so that you can start building your own full-blown zooming application by utilizing this friendly guide. Happy coding!

    Final thoughts

    This is the end of the series. I hope you have enjoyed reading it as much as I did writing it, which would mean much to me. Besides, if you're a novice web developer who's taking their first steps into this fascinating terrain, you can learn the basics of how to build JavaScript applications that degrade gracefully when scripting is disabled on the browser.

    See you in the next web development tutorial!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · This last part of the series completes the development of this JavaScript-driven...
     

    JAVASCRIPT ARTICLES

    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...
    - Bulleted Menu of Links
    - Creating Click Loggers and Basic Markers wit...
    - Adding Pan Controls to Yahoo! Maps
    - Adding Zoom Controls to Yahoo! Maps
    - Working with Yahoo! Maps
    - Building Image Zooming Controls with the DOM...
    - Working with Multiple Graphics for a Zoom Ap...
    - Improving an Image Zooming Application with ...
    - Zooming in on Images with JavaScript
    - JavaScript Date Objects: Universal Coordinat...
    - Javascript Objects: More Date Methods
    - JavaScript Objects: Dates
    - JavaScript Objects: Finishing Strings


     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway