Posts Tagged ‘new element

David Walsh Slideshow

I’ve been creating a lot of slideshow posts lately. Why, you ask? Because they help me get chicks. A quick formula for you:

var numChicks = $$('.slideshow').length; //simple!

The following code snippet will show you how to create a simple slideshow with MooTools; the script will also preload the images and feature a progress message. Why preload images? They make the slideshow more elegant and you can avoid an onLoad mess. Oh, and chicks…loads and loads of chicks.

The HTML

<div id="slideshow-holder">
	<div id="progress"></div>
</div>

Basically just two DIVs which will hold content.

The CSS

#slideshow-holder	{ width:600px; height:400px; background:url(spinner.gif) center center no-repeat #fff; position:relative; }
#progress			{ position:absolute; width:100%; text-align:center; color:#999; top:225px; }

The image holder is given set dimensions, starts with a background spinner, and its position set to relative; the images will all be positioned absolutely. The progress holder is set right below the spinner.

The MooTools Javascript

window.addEvent('domready',function() {
	/* preloading */
	var imagesDir = 'epics/';
	var images = ['2.jpg','3.jpg','1.jpg','4.jpg','5.jpg'];
	var holder = $('slideshow-holder');
	images.each(function(img,i){ images[i] = imagesDir + '' + img; }); //add dir to images
	var progressTemplate = 'Loading image {x} of ' + images.length;
	var updateProgress = function(num) {
		progress.set('text',progressTemplate.replace('{x}',num));
	};
	var progress = $('progress');
	updateProgress('text','0');
	var loader = new Asset.images(images, {
		onProgress: function(c,index) {
			updateProgress('text',index + 1);
		},
		onComplete: function() {
			var slides = [];
			/* put images into page */
			images.each(function(im) {
				slides.push(new Element('img',{
					src:im,
					width: 600,
					height: 400,
					styles: {
						opacity:0,
						top:0,
						left:0,
						position:'absolute',
						'z-index': 10
					}
				}).inject(holder));
				holder.setStyle('background','url(logo.png) center 80px no-repeat');
			});
			var showInterval = 5000;
			var index = 0;
			progress.set('text','Images loaded.  MooTools FTW.');
			(function() {slides[index].tween('opacity',1); }).delay(1000);
			var start = function() {
				(function() {
					holder.setStyle('background','');
					slides[index].fade(0);
					++index;
					index = (slides[index] ? index : 0);
					slides[index].fade(1);
				}).periodical(showInterval);
			};

			/* start the show */
			start();
		}
	});
});

The first set of variable declarations represent basic settings for the preloader: images, preload-message-updating, etc. We pass our Asset.images instance an array of images. When each image loads, we update the status message. When every image has loaded, we remove the status message and start the slideshow. That’s it!

Of course the above could be turned into the class….I’m just slightly lazy…Feel free to turn it into a class and share with everyone!

Don’t forget to follow me on Twitter and be sure to visit Script & Style for the best Javascript and CSS articles around!Sponsor the David Walsh Blog and get your brand in front of several thousand users per day!

Create a Quick MooTools Slideshow with Preloading Images

Related posts:

  1. Create a Simple Slideshow Using MooTools
  2. Create a Simple Slideshow Using MooTools, Part II: Controls and Events
  3. MooTools Image Preloading with Progress Bar
  4. Create a Simple Slideshow Using MooTools, Part IV: Thumbnails and Captions
  5. Create a Simple Slideshow Using MooTools, Part III: Creating a Class


Go to Source

It’s hardly a secret that all of the major location-based players are planning big updates to their services to coincide with the SXSW festival starting next week in Austin, Texas. One of them peeked out a bit early: Foursquare.

Earlier today, the latest version of Foursquare, 1.6, went live in the App Store for a brief period of time. I’ve been using the build for a couple of days, and while the functionality isn’t all that different from the previous versions, the look-and-feel has been completely revamped.

This is notable because the vast majority of Foursquare users are still using the iPhone (67%). So this update will be a welcome change for many, especially as Foursquare and Gowalla continue to compete. Gowalla, while smaller than Foursquare, is generally considered to be the prettier of the two. Certainly, with its new website revamp, Gowalla still holds that title on the web, but the new Foursquare app looks pretty nice compared to the Gowalla iPhone app now.

So what’s different? The entire color palette is now a mixture of silver, blue, white, and bright green. Some may not like the bright green elements, but it’s effective to let people know where to click when you want to check-in. Also new is the fact that the “Shout” button is emphasized on the upper left part of the main screen. “Shouting” is basically the equivalent of tweeting out a message, it allows you to send a message to your followers without having to check-in at a place. It’s a bit odd that this is now a main button on the left side while the “Check-in” button is on the right side (considering most people read left to right).

Another new element is the idea of categories. As we wrote about a couple of days ago, Foursquare is starting to categorize venues into certain categories. While there isn’t much you can do with these yet on the new iPhone app, you are able to see icons that represent how a venue is categorized.

Something else new that is nice is that you can click on individual venues in your friend stream to load that venue’s main screen (which now shows who is there, right away). Previously, you had to click on your friend, then click over to see the venue — so this saves time.

While using the app, I wondered if Apple would approve it given that it uses the text, email, and phone icons used by the iPhone itself within the app (see screenshot below), but apparently if they accidentally put it in the store today, they’re going to be okay with those icons. These icons are shown on the new profile pages, which also show how many mayorships a member as, as well as how many badges they’ve earned. Interestingly, the point-based element of the app has been depreciated a bit, as that’s how a harder-to-get-to area of the app.

As a bonus to this advanced iPhone preview that some users got today, Foursquare also launched a new version of its Android app tonight. I’ve been playing with that for a few minutes, and that seems very solid as well. Look for the new version of Foursquare iPhone app at some point late next week when SXSW starts.



Go to Source

A while back I shared a few MooTools class creation tips that I live by. More experience in creating MooTools plugins has given me some new guidelines to share.

Code “Inline” First, Then Class-ize; Don’t Force It

The number one tip I give to MooTools Class rookies is this: create the desired class functionality before turning it into a class. Once you have the code working using “inline” MooTools javascript, evaluate what you have. You may decide that your functionality is too specific to the task at hand that making a Class isn’t the right choice.

If you do think you should turn the code into a class, take a look at all of your initial “var {key} = {value}” declarations — those will likely be your arguments and options. Take any repeated code and turn them into class methods. Do the same for specific functionalities the Class should have. Here’s a very basic example:

window.addEvent('domready',function() {
	/* settings */
	var showDuration = 3000;
	var container = $('slideshow-container');
	var images = container.getElements('img');
	var currentIndex = 0;
	var interval;
	var toc = [];
	var tocWidth = 20;
	var tocActive = 'toc-active';

	/* new: starts the show */
	var start = function() { interval = show.periodical(showDuration); };
	var stop = function() { $clear(interval); };
	/* worker */
	var show = function(to) {
		images[currentIndex].fade('out');
		toc[currentIndex].removeClass(tocActive);
		images[currentIndex = ($defined(to) ? to : (currentIndex < images.length - 1 ? currentIndex+1 : 0))].fade('in');
		toc[currentIndex].addClass(tocActive);
	};

	/* new: control: table of contents */
	images.each(function(img,i){
		toc.push(new Element('a',{
			text: i+1,
			href: '#',
			'class': 'toc' + (i == 0 ? ' ' + tocActive : ''),
			events: {
				click: function(e) {
					if(e) e.stop();
					stop();
					show(i);
				}
			},
			styles: {
				left: ((i + 1) * (tocWidth + 10))
			}
		}).inject(container));
		if(i > 0) { img.set('opacity',0); }
	});

	/* new: control: next and previous */
	var next = new Element('a',{
		href: '#',
		id: 'next',
		text: '>>',
		events: {
			click: function(e) {
				if(e) e.stop();
				stop(); show();
			}
		}
	}).inject(container);
	var previous = new Element('a',{
		href: '#',
		id: 'previous',
		text: '<<',
		events: {
			click: function(e) {
				if(e) e.stop();
				stop(); show(currentIndex != 0 ? currentIndex -1 : images.length-1);
			}
		}
	}).inject(container);

	/* new: control: start/stop on mouseover/mouseout */
	container.addEvents({
		mouseenter: function() { stop(); },
		mouseleave: function() { start(); }
	});

	/* start once the page is finished loading */
	window.addEvent('load',function(){
		start();
	});
});

And now the Class version:

var SimpleSlideshow = new Class({
	options: {
		showControls: true,
		showDuration: 3000,
		showTOC: true,
		tocWidth: 20,
		tocClass: 'toc',
		tocActiveClass: 'toc-active'
	},
	Implements: [Options,Events],
	initialize: function(container,elements,options) {
		//settings
		this.container = $(container);
		this.elements = $$(elements);
		this.currentIndex = 0;
		this.interval = '';
		if(this.options.showTOC) this.toc = [];

		//assign
		this.elements.each(function(el,i){
			if(this.options.showTOC) {
				this.toc.push(new Element('a',{
					text: i+1,
					href: '#',
					'class': this.options.tocClass + '' + (i == 0 ? ' ' + this.options.tocActiveClass : ''),
					events: {
						click: function(e) {
							if(e) e.stop();
							this.stop();
							this.show(i);
						}.bind(this)
					},
					styles: {
						left: ((i + 1) * (this.options.tocWidth + 10))
					}
				}).inject(this.container));
			}
			if(i > 0) el.set('opacity',0);
		},this);

		//next,previous links
		if(this.options.showControls) {
			this.createControls();

		}
		//events
		this.container.addEvents({
			mouseenter: function() { this.stop(); }.bind(this),
			mouseleave: function() { this.start(); }.bind(this)
		});

	},
	show: function(to) {
		this.elements[this.currentIndex].fade('out');
		if(this.options.showTOC) this.toc[this.currentIndex].removeClass(this.options.tocActiveClass);
		this.elements[this.currentIndex = ($defined(to) ? to : (this.currentIndex < this.elements.length - 1 ? this.currentIndex+1 : 0))].fade('in');
		if(this.options.showTOC) this.toc[this.currentIndex].addClass(this.options.tocActiveClass);
	},
	start: function() {
		this.interval = this.show.bind(this).periodical(this.options.showDuration);
	},
	stop: function() {
		$clear(this.interval);
	},
	//"private"
	createControls: function() {
		var next = new Element('a',{
			href: '#',
			id: 'next',
			text: '>>',
			events: {
				click: function(e) {
					if(e) e.stop();
					this.stop();
					this.show();
				}.bind(this)
			}
		}).inject(this.container);
		var previous = new Element('a',{
			href: '#',
			id: 'previous',
			text: '<<',
			events: {
				click: function(e) {
					if(e) e.stop();
					this.stop();
					this.show(this.currentIndex != 0 ? this.currentIndex -1 : this.elements.length-1);
				}.bind(this)
			}
		}).inject(this.container);
	}
});

See how easy the transition is when you have the inline code in front of you? Simple!

Add Events When Possible

Adding events to your classes gives a completely new level of control over the class functionality. Consider my Overlay class. I spent 95% of the time creating the plugin functionality and 5% implementing events but I consider events essential to the Overlay. Here’s a quick example of implementing events:

//.....
Implements: [Options,Events],
open: function() {
	this.fireEvent('open');
	//do stuff
},
//.....

Now my Overlay class allows you fire events at 4 different times. More flexible and a perfect usage of events.

Evaluate Options vs. Arguments

Judge your list of options and your class’ primary responsibilities to determine if all of the options should be kept as options and not as arguments, and visa versa. When I come to a 50/50 decision I choose to make the parameter an option. If the class cannot live without the given option/argument and there’s no suitable default, keep it as an argument.

Chaining: return this;

One of the great advantages of using a javascript frameworks and classes is the ability to chain method class to make your code shorter. And implementing that type of functionality is so easy!

//.....
store: function(key,value) {
	this.data[key] = value;
	return this;
}
//.....

Don’t let your methods end with nothing — return this!

Put it in the Forge!

Share your awesome plugin by posting it in the MooTools Forge. Sharing your plugins in the Forge allows you to easily get feedback, improvements, and requests.

There you have it. Feel free to share any of your tips!

Don’t forget to follow me on Twitter and be sure to visit Script & Style for the best Javascript and CSS articles around!

Sponsor the David Walsh Blog and get your brand in front of several thousand users per day!

MooTools Class Creation Tips II

Related posts:

  1. MooTools Class Creation Tips
  2. Create a Simple Slideshow Using MooTools, Part III: Creating a Class
  3. MooTools-Like Element Creation in jQuery
  4. Check for Function and Class Existence Using PHP
  5. MooTools 1.2 Tooltips: Customize Your Tips


Go to Source


 Powered by Max Banner Ads 

About this blog

This blog delivers stylish and dynamic news for designers and web-developers on all subjects of design, ranging from: CSS, Ajax, Javascript, web design, graphics, typography, advertising & much more. Our goal is to help you communicate effectively on the web with an engaging website or functional interface.

New offer:


 Powered by Max Banner Ads 
Internet MegaMeeting, LLC Microsoft Store LinkShare  Referral  Program Iolo technologies, LLC Artisteer - Web Design Generator FTPress.com (Pearson Education) Mobile Security: Parental Controls and Monitoring Atom Entertainment (formerly AtomShockwave)
.
Web Analytics