Solved

growing and shrinking div in javascript

Posted on 2009-05-14
6
677 Views
Last Modified: 2012-05-07
Write a simple DHTML page with one centered div, height 200px, width 200px, that is blue, on a white background.
When clicking on the div it should slowly grow to 500px (width) and then shrink to 200px, and repeat this motion until it is clicked again.
Do not use 3rd party JavaScript libraries in your solution.


0
Comment
Question by:SunScreenCert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 3

Assisted Solution

by:GregTSmith
GregTSmith earned 125 total points
ID: 24392844
10 minutes of playing around... of course it will be up to you to make this work in the browsers you need to support, but it's an idea... I've only tested in IE7.
<html>
<head>
<script>
var page = {
  active: false,
  action: null,
  reference: null,
  speed: null, 
  initialize: function () {
    document.onclick = page.onClick;
  },
  onClick: function () {
    var o = event.srcElement;
    if (o.id === 'box') {
      if (page.active) {
        page.action = 'shrink';
        page.active = false;
        page.speed = 20;
      }
      else {
        page.speed = 3;
        page.active = true;
        page.action = 'grow';
        page.reference = setInterval(page.adjust, 25);
      }
    }
  },
  adjust: function () {
    var o = page.find('box'), 
      w = o.style.width || '200px',
      i = parseInt(w.substring(0, w.length - 2), 10);
    switch (page.action) {
      case 'grow':
        i += page.speed;
        if (i >= 500) {
          page.action = 'shrink';
        }
        break;
      case 'shrink':
        i -= page.speed;
        if (i <= 200) {
          page.action = 'grow';
          if (!page.active) {
            clearInterval(page.reference);
          }
        }
        break;
    }
    o.style.width = i + 'px';
  },
  find: function (id) {
    return document.getElementById(id);
  }
}
window.onload = page.initialize;
</script>
<style>
* {
  margin:0;
  padding:0;
}
body {
  margin-top:100px;
  text-align:center;
}
#box {
  background-color:blue;
  height:200px;
  width:200px;
}
</style>
</head>
<body>
<form id="form1">
 
<div id="box"></div>
 
</form>
</body>
</html>

Open in new window

0
 
LVL 20

Expert Comment

by:ddayx10
ID: 24400980
Well if the author is not going to comment on that I will...

Nice effort. Doesn't work cross browser but I still admire your work. I would help out and write one that works cross-browser, but it doesnt seem as though it would be used, and you deserve the points anyway!

:)
0
 
LVL 2

Author Comment

by:SunScreenCert
ID: 24401150
No please help me with the cross browser one
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 2

Author Comment

by:SunScreenCert
ID: 24425100
hey this is not working
0
 
LVL 2

Author Comment

by:SunScreenCert
ID: 24425292
hey i have firefox and it is not working
0
 
LVL 3

Accepted Solution

by:
GregTSmith earned 125 total points
ID: 24425540
A couple of small tweaks and this works for me in FireFox 3.0.1.
<html>
<head>
<script>
var page = {
  active: false,
  action: null,
  reference: null,
  speed: null, 
  initialize: function () {
    document.onclick = page.onClick;
  },
  onClick: function (e) {
    var o = (e) ? e.target : event.srcElement;
    if (o.id === 'box') {
      if (page.active) {
        page.action = 'shrink';
        page.active = false;
        page.speed = 20;
      }
      else {
        page.speed = 3;
        page.active = true;
        page.action = 'grow';
        page.reference = setInterval(page.adjust, 25);
      }
    }
  },
  adjust: function () {
    var o = page.find('box'), 
      w = o.style.width || '200px',
      i = parseInt(w.substring(0, w.length - 2), 10);
    switch (page.action) {
      case 'grow':
        i += page.speed;
        if (i >= 500) {
          page.action = 'shrink';
        }
        break;
      case 'shrink':
        i -= page.speed;
        if (i <= 200) {
          page.action = 'grow';
          if (!page.active) {
            clearInterval(page.reference);
          }
        }
        break;
    }
    o.style.width = i + 'px';
  },
  find: function (id) {
    return document.getElementById(id);
  }
}
window.onload = page.initialize;
</script>
<style>
* {
  margin:0;
  padding:0;
}
body {
  margin:100px 10px 10px 10px;
  text-align:center;
}
#box {
  background-color:blue;
  height:200px;
  margin:auto;
  width:200px;
}
</style>
</head>
<body>
<form id="form1">
 
<div id="box"></div>
 
</form>
</body>
</html>

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question