Code Articles

Easy Sliding Windows

June 23rd, 2008

Easy Sliding Windows With Mootools

With the recent release of mootools 1.2. I thought I’d share something that I have been using for a while. I call them “sliding windows” for lack of a better term. Check out the demo before we get started.

Let’s start by looking at the HTML for the example:
<div id="wrap">
<a href="#one" class="one">Goto 1</a> | <a href="#two" class="two">Goto 2</a> | <a href="#three" class="three">Goto 3</a> | <a href="#four" class="four">Goto 4</a><br /><br />

<div id=”window”>
<div id=”container”>
<div id=”slide_wrap”>
<div id=”one”><a name=”one”></a>
<img src=”images/1.gif” width=”181″ height=”107″ alt=”" border=”0″ /><br />
<span>Prev</span> <a href=”#two” class=”two next”>Next</a>
</div>
<div id=”two”><a name=”two”></a>
<img src=”images/2.gif” width=”181″ height=”107″ alt=”" border=”0″ /><br />
<a href=”#one” class=”one”>Prev</a> <a href=”#three” class=”three next”>Next</a>
</div>
<div id=”three”><a name=”three”></a>
<img src=”images/3.gif” width=”181″ height=”107″ alt=”" border=”0″ /><br />
<a href=”#two” class=”two”>Prev</a> <a href=”#four” class=”four next”>Next</a>
</div>
<div id=”four”><a name=”four”></a>
<img src=”images/4.gif” width=”181″ height=”107″ alt=”" border=”0″ /><br />
<a href=”#three” class=”three”>Prev</a> <span class=”next”>Next</span>
</div>
</div><!– end slide wrap –>
</div><!– end container –>
</div><!– end window –>
</div><!– end wrap –>
<br />
<strong>Scroller</strong>
<div id=”area”>
<div id=”knob”></div><!– end knob –>
</div><!– end area –>

We set up the sliding action by giving our overall #wrap div a width of 742px and a “hidden” overflow property, while giving the #slide_wrap div a width of 2220px. This gives us a nice long div inside an area that has a constrained “window”. Now all we have to do is add a little Javascript to the action and with the power of mootools, we’ll have some really cool stuff.

A dash of Javascript:
// setup the scroll bar
var scroll = new Fx.Scroll('container', {
wait: false, //start immediately
duration: 500, // take .5 seconds to complete the transition
offset: {'x': 0, 'y': 0}, // no padding in the scroll bar
transition: Fx.Transitions.Quad.easeInOut // make it look cool with easing
});
// trying to write for unlimited images
var area = new Array ('one', 'two', 'three', 'four');
// Slider
var mySlide = new Slider($('area'), $('knob'), { //set up the slider based on the position of the scroll bar
steps: 3, // how many positions can we slide too, 0 counts
offset: 0, // no padding
onChange: function(pos){
scroll.toElement(area[pos]); // when the scroll bar is moved, move the sliding div to the appropriate spot
}
}).set(0); // when we load the page, start at the beginning
// setup the links to take you to the correct div when clicked

$$(’a.one’).addEvent(’click’, function(event) {
event = new Event(event).stop();
mySlide.set(0);
});
$$(’a.two’).addEvent(’click’, function(event) {
event = new Event(event).stop();
mySlide.set(1);
});
$$(’a.three’).addEvent(’click’, function(event) {
event = new Event(event).stop();
mySlide.set(2);
});
$$(’a.four’).addEvent(’click’, function(event) {
event = new Event(event).stop();
mySlide.set(3);
});

Now, there’s a little bit of script for the scroll bar I added, but the basic gist of it is as follows…

First, we set up an array of our four areas:
var area = new Array ('one', 'two', 'three', 'four');

Then, we initialize the slider (see the mootools docs for more on the options:)
var mySlide = new Slider($('area'), $('knob'), { //set up the slider based on the position of the scroll bar
steps: 3, // how many positions can we slide too, 0 counts
offset: 0, // no padding
onChange: function(pos){
scroll.toElement(area[pos]); // when the scroll bar is moved, move the sliding div to the appropriate spot
}
}).set(0); // when we load the page, start at the beginning

And, last but not least we tell it where to move when a link is clicked:
$$('a.one').addEvent('click', function(event) {
event = new Event(event).stop();
mySlide.set(0);
});

That’s all there is too it!! The best part about this whole script is that we are using page anchors in our links (#one, #two, etc..) so, even if Javascript is turned off, the correct div should still appear in the window. The End

Live Thumbnails Valid

February 23rd, 2006

While cruising the web yesterday checking my usual sources for cool new things to do, I came upon Live Thumbnails: Watch ‘em Grow (demo). What a cool concept. Using a little slick JavaScript, Chris Klimas transformed boring old thumbnails into living, breathing objects on the page. Better yet, with JavaScript disabled, the page acts like a normal image gallery would. You click an image, you get a bigger image. Sweet right?

Then came the catch… It’s not valid. The demo doesn’t even have a Doctype. C’mon…. Now I’m no purist, but it always seemed to me that it’s better to have valid code, than invalid. The drawbacks of invalid HTML are innumerous, plus, it just seems unprofessional.

With that in mind I set out to fix this little bit of code. There has to be a way… right? The first thing I did was to add a Doctype and some normal XHTML stuff. At this point I realized I was almost done. The only thing holding me back were the “largewidth” and “largeheight” attributes. It couldn’t be this easy… could it?

largewidth=”480″ largeheight=”360″

JavaScript Reference:

img.largeWidth = parseInt(img.getAttribute(’largewidth’));
img.largeHeight = parseInt(img.getAttribute(’largeheight’));

The img.largeWidth variable doesn’t need to be a variable. You have to set it for each image anyway… why not set it in the JavaScript?

img.largeWidth =’480′;

img.largeHeight = ‘360′;

Wow, that was easy… why can’t everything go like that. Now, this makes the image size somewhat rigid. To add a different size image, just create a new class (livethumbnail2) and create a new attribute set for that class (img.largeWidth=’240′;). Pretty simple, huh? See the demo.

Note: I am not assuming that Chris did not see this option, plus this does not address the “I have tons of different sized images” problem. For that, I’d create an array… or maybe see if I can use PHP to deliver the large image dimensions from the server. Anything is possible… right?