Feature
Post

Category
Code


Custom Reading Containers

For those who have been following Devlounge for a while, you’ll know that I wrote a script called Custom Reading Width. Custom Reading Width allowed users of a particular site to adjust a container’s width. Soon after publishing the article, it was suggested to me that I write something similar that allows a user to drag the edge of a container in order to adjust the container’s width. In an effort to write that script, Custom Reading Containers was born.

In all honesty, I’ve only tested out the script in Firefox 2.0, IE 6, and Opera 9. I should have labeled the script as a beta, but I’m taking Andrew Faulkner’s advice and leaving the beta out of it.

What are the Features?

  1. Custom Reading Containers (CRC) does not rely on any third-party JavaScript library. The script is lightweight and portable.
  2. The script will work on as many containers as your heart desires.
  3. The script saves the width of the container automatically. Custom Reading Width needed user interaction.
  4. CRC is hidden and unobtrusive.
  5. CRC works just as if you were resizing a window.

What are the Applications?

  • CRC can allow users to adjust the reading width in full-width websites.
  • CRC can potentially control containers in a web application.
  • And more…

See the script in action. Demo1 | Demo2

How Does It Work

Custom Reading Containers works by laying a small overlay over the container you wish to resize. As you can see from the image below, the overlay is placed on top of the container.

Devlounge Custom Reading Containers - Container Div with a blue overlay

The overlay’s width and appearance is controlled by CSS. If you like, you can make the overlay transparent, wide, skinny, or whatever style you prefer.

When a user hovers over the overlay, the cursor changes to indicate that the container can be resized.

Devlounge Custom Reading Containers - Container Div with a blue overlay and mouse cursor

The user resizes the container by clicking on the overlay and dragging the mouse to the left or to the right.

Devlounge Custom Reading Containers - Resized Container

Once the user has finished resizing, the script saves the data to a cookie. When the user returns back to the page, a small PHP script calls the cookie and sets the width of the container to the user’s preferred width.

Where Can I Try It Out?

Here are two locations that you can try out. The first location demonstrates how the script reacts to multiple containers. The second example shows how the script will behave when used on a full-width layout.

Where Can I Download it?

Give Custom Reading Containers a try. Download Custom Reading Containers version 1.

Customizing the Script

The test script includes these three files: crc.js, crc.css, and test.php. I will stick to covering them for simplicity.

Make Sure the Container is Relative Positioned

The first thing to note is that any container having the overlay must be relative positioned. This can be accomplished quite simply in CSS by doing: position: relative; in your CSS file.

Modify the crc.js file

You need to modify the crc.js file to include your containers. Towards the bottom of the file, you’ll see code such as this:

var container1 = containerCollection.Add("sidebar", "30%", false, 10, 30, true);

For each container added, give the container a unique variable name such as container1 in this example. There are five things to pass to the containerCollection.Add() function.

  1. The container name. In this example, the container name was “sidebar.”
  2. The default width of the container. My default width for the “sidebar” container was 30 percent. Since the value must be a string, I entered in “30%”. You could have easily have put in “300px”.
  3. Whether the container is fixed or fluid. If the container is fixed, the value should be true. If the container is fluid, the value should be false.
  4. The minimum width of the container. This is an integer value and can be in pixels or percentages.
  5. The maximum width of the container. This is an integer value and can be in pixels or percentages.
  6. Whether the min and max width of the container are in percentages or pixels. If the widths are in percentages, the value is true. Otherwise, the value is false.

You repeat the above steps for each container to be resized.

Insert the PHP Code

The following code must be inserted on any page utilizing Custom Reading Containers. The code is in PHP, so a PHP file is needed to run the script. The PHP script reads in the cookie set by the JavaScript and sets the styles up accordingly. The code needs to be inserted within the head tag.

<?php
	if (isset($_COOKIE["customContainers"])) {
	$ReadingArray = explode(",", $_COOKIE["customContainers"]);
	$ReadingWidth = "";
	for ($i = 0; $i <= sizeof($ReadingArray); $i++) {
		if ($i % 2 == 0) { //if the number is odd
			if (preg_match("/^[-a-zA-Z0-9]+$/", $ReadingArray[$i])) {
				$ReadingWidth .= "\n#" . $ReadingArray[$i] . " {";
			} else { $ReadingWidth .= "\n#ABCDEFGHIJKLMNOPQRSTUVWXYZ {"; 
			}
		} else {
			if (preg_match("/^\d\d*(px|%)$/", $ReadingArray[$i])) {
				$ReadingWidth .= "width: " . $ReadingArray[$i] . ";}";
			} else { 
				$ReadingWidth .= ";}"; 
			}
		}
	}
	if (sizeof($ReadingArray > 0)) {
?>
<style type="text/css"><?php echo $ReadingWidth ?></style>
<?php }
		
} 
?>

Modify the StyleSheet

The code in crc.css is more-or-less setup to control the overlay. You can easily just paste the overlay code into your existing CSS file. As stated earlier, you can control the overlay’s appearance using your StyleSheet.

.crc-overlay { 
	display: block;
	position: absolute;
	right: 0px;
	top: 0;
	width: 5px;
	cursor: e-resize;
	z-index: 100;
	background-color: #000066;
}

Conclusion

Hopefully Custom Reading Containers can help you out, whether it is assisting your users in adjusting the reading width, or perhaps controlling some function of a web application. If you have any feedback or bug reports, please leave a comment.

If you like this script, or would like to show your support, please help out with a Digg.


  1. By Tom H posted on January 24, 2007 at 5:38 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Works very nicely, however the I question the usefulness of this in web layouts. Shouldn’t we be using the built in ability of the browser that allows people to change reading widths, ie resizing the window?

    I can see great potential in this, however, in allowing you to change the widths of columns (or panes), particularly in complex web apps. Would it be possible to do this with this script?

  2. By Ronalfy posted on January 24, 2007 at 9:54 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Tom H,

    Changing the browser width would be ideal, but that would require resizing the browser window each time for each full-width layout. If you read my “inspiration” post (under Related Posts above), you’ll see why I wrote the script.

    The script does have the potential to be used in web apps. I’ll try to add some functionality and update the article this weekend so that the containers can talk to eachother. Right now, each container is independent.

  3. By Chris posted on April 12, 2007 at 3:56 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    I like it! I modified the script to take a linked container that would be resized in the opposite direction when the main container was resized. This gives the illusion of a movable divider between the two containers.

  4. By Ronald Huereca posted on April 12, 2007 at 8:34 pm
    Want an avatar? Get a gravatar! • You can link to this comment

    Nice Chris. Do you have a working example?

  5. By feha posted on January 7, 2009 at 8:24 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Hi, I really like the idea.
    Some modification in the code to stop warnings:

    Checks if array item is not empty:
    if (isset($ReadingArray[$i]) && preg_match(“/^\d\d*(px|%)$/”, $ReadingArray[$i])) {
    and
    if (isset($ReadingArray[$i]) && preg_match(“/^\d\d*(px|%)$/”, $ReadingArray[$i])) {

    I would like to see re size in height too (right-down corner) and possibly drag and place so it can remember positions of containers.
    Oh yes this is very useful and great feature for website users as they can decide how to display the website.

    Thanks for this great idea!
    :-)
    Second Comment, corrected email address!

  6. TrackbackDevlounge | Prebuilt 3 Concepts ReleasedWeb-Sites of the Month: The Best of January 2007 | Smashing Magazine煎蛋 » 2007 年一月最佳网站收集Good websites that you shouldn’t have missed | TechBlogyPowerful CSS-Techniques For Effective Coding | How-To | Smashing MagazinePowerful CSS-Techniques For Effective CodingTips Trick, Software dan Aplikasi Review » Blog Archive » Powerfull CSS TeknikbdITjobs.com : : Blog » Blog Archive » Powerful CSS-Techniques For Effective Coding50种强大的CSS技术 | CodeLogUmair’s Blog » Blog Archive » Super Powerful CSS - Techniques For Effective CodingBest Solution for Web :: Powerful CSS-Techniques For Effective Coding :: July :: 200850种强大的CSS技术 | 伊莱利奥的博客50 técnicas avanzadas CSS para una codificación efectiva. | AlainAlemanyPowerful CSS-Techniques For Effective Coding « Rohit Dubal我想网 » Blog Archive » 50种强大的CSS技术50 técnicas avanzadas CSS para una codificación efectiva. | WebizzimaPowerful CSS-Techniques For Effective Coding « Ramesh50种强大的CSS技术 | 帕兰映像Powerful CSS-Techniques For Effective Coding | WEBDESIGN FANPowerful CSS-Techniques For Effective Coding « Sejix Blog