Switch On The Code RSS Button - Click to Subscribe
May
28

Javascript Tutorial - Generic Resizeable Container

So a long while ago (last November), we posted some tutorials on how to create a resizeable textbox (Part 1, Part 2, and Part 3). We even did a post on how you could integrate the new textbox into Wordpress. Well, I got a request recently to expand the resizeable textbox code so that it could be a resizeable container for anything on a web page. So the result of that request is what we are going to be taking a look at today.

I'm not going to go into a lot of detail on the code - partly because there is a lot of it, partly because I'm lazy, and mostly because we have actually already covered it in the tutorials I linked to above. In fact, I would suggest reading those tutorials first (or at least skimming them), so you get a sense of how the resizing code works. What I'm going to focus on here are the changes that needed to be made to the old code to make it support containing any element. Of course, no tutorial would be complete without an example, so play around for a few moments:


So first, lets get the html portion out of the way (not that there is much):

<div id="borderBox" style="position:relative;border:1px solid black;width:530px;height:250px;">
  <img id="resizeableElement" src="Sunset.jpg"/>
  <script type="text/javascript">
    var rc = new ResizeableContainer('resizeableElement');
    rc.GetContainer().style.left = '10px';
    rc.GetContainer().style.top = '10px';
    rc.SetMaxWidth(510);
    rc.SetMaxHeight(230);
  </script>
</div>

Ok, so that outer div is just a border box, just there as to be a simple visual container. The next element is the element that we want to resize, in this case an image. The element that you want to resize can be anywhere in your html - the only requirement is that it comes before the chunk of javascript that creates the resizeable container. This is because the element to resize needs to be created before the resizeable container is created (so that the container can refer to the element). You also need to give the element to resize some sort of unique id - in this case, it is named 'resizeableElement'.

Now comes the javascript initialization chunk. Here, we create a new ResizeableContainer, and the argument to the constructor is the id of the element that we want to make resizeable. This container will get inserted exactly where the script tag is (kind of like Google Adsense), and so all that is left to do is adjust the position a little bit and set the max size values. It should all look very similar to the old resizeable textbox code.

Now onto the javascript. The first change that had to be made is that we renamed the object from ResizeableTextbox to ResizeableContainer - pretty self explanatory, I would hope. The constructor arguments didn't change, but unlike before, the first argument is always required. It used to be that creating a ResizeableTextbox would make the textbox itself, and the first argument to the constructor was an optional id for that textbox. Now, however, the first argument is a required id of the element that will be placed inside of the the ResizeableContainer. So instead of:

function ResizeableTextbox(id, parent)
{
  var MINSIZE = 38;

We have:

function ResizeableContainer(contentID, parent)
{
  var MINSIZE = 38;

The next change is where we used to create the internal textbox. We replace this code:

var _textArea = document.createElement('TEXTAREA');
_textArea.className = 'rtTextArea';
_textArea.RTObject = this;
if(id != null && id != "")
{
  _textArea.id = id;
  _textArea.name = id;
}

With this code:

var _content = document.getElementById(contentID);
_content.ResizeableContainer = this;
_content.className = 'reContent';

As you can tell by looking at that changed code, we went from creating the element to grabbing it out of the document. And that is actually it for the functionality changes - the rest were purely cosmetic. For instance, _textArea got renamed to _content, the function GetTextArea got renamed to GetContentElement, and the functions GetText and SetText were removed because they no longer made sense.

Oh, and we can't forget the one change to the style sheet. We went from this:

.rtTextArea
{
  position: absolute;
  left: 1px;
  top: 1px;
  border: 0px;
  margin: 0px 0px 0px 0px;
  padding: 0px 0px 0px 0px;
  background-color: White;
}

To this:

.reContent
{
  position: absolute;
  left: 1px;
  top: 1px;
  border: 0px;
  margin: 0px 0px 0px 0px;
  padding: 0px 0px 0px 0px;
}

The name was changed (another cosmetic thing), but we also removed the background-color style, because we only needed that specifically for the textbox.

And that is it! Even I was surprised at how few changes needed to be made to the resizeable textbox code to allow it to contain/resize other things. Of course, I'm sure there are things that you could put into the resizeable container that would break it - for instance, I bet putting a resizeable container inside of a resizeable container would do bad things - but hey, you shouldn't do that anyway. As usual, you can grab the code for the example above here, and if you have any comments or questions, feel free to leave them below.




Posted in Javascript, All Tutorials by The Tallest |

2 Responses

  1. zonux.c Says:

    Nice Tutorial

  2. Peter Says:

    Cool!
    I tried to apply it to iframes because I needed html rendering (to be really generic). It didn’t work well,
    possibly because of timing: the borders didn’t follow the mouse mouvements fast enough. When I move the mouse back to where the borders are, I could continue to drag them.

    Any hints to make it work?
    Thanks!

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Powered by WP Hashcash