Posts tagged javascript

iPhone orientation change

iPhone web development [Part 2]

0

iPhone orientation changeIn part one of my tutorial on iPhone web development, we talked about the basic stuff (Recognizing iPhones or iPods, setting up special HTML and CSS tags... ) we will now move on to the really clever bit: Orientation detection.

For many sites, we may just have the same thing both on portrait and landscape. But sometimes we will want to use that extra space to display our content differently, and here is how.

Orientation detection with CSS:

We can use some smart CSS3 selectors to ensure that some styles only apply to landscape, or to portrait modes:

/* Portrait */
@media screen and (max-width: 320px){
	h1{ font-size:20px; }
}
/* Landscape */
@media screen and (min-width: 321px){
	h1{ font-size:24px; }
}

Remember that for this to work we must use the viewport meta tag I showed you in my previous tutorial
This media query works (According to Apple's documentation) since iPhone OS 1.0, so there shouldn't be any problem with it, Firefox mobile should also support this, so by using max-device-width and min-width we should be able to develop specific CSS for every device without the need of PHP to include only certain style sheets

Orientation detection with JavaScript:

We can easily detect the device orientation by checking the innerWidth. This method will also work for other PDAs and SmartPhones:

var updateLayout = function() {
  if (window.innerWidth != currentWidth) {
    currentWidth = window.innerWidth;
    var orient = (currentWidth == 320) ? "profile" : "landscape";
    document.body.setAttribute("orient", orient);
    window.scrollTo(0, 1);
  }
};
 
iPhone.DomLoad(updateLayout);
setInterval(updateLayout, 500);

This code will add a new attribute (orient) to the body element, which will be either portrait or landscape. All we have to do now is use CSS to filter out whatever orientation we need:
There is another method, which will work only on the iPhone though:

body[orient="landscape"]{
// CSS code here...
}
// ... or ...
body[orient="portrait"]{
// CSS code here...
}

That JavaScript also hides the toolbar by using the scrollTo() function when the orientation changes,

Next release:

On my next release in this series of iPhone web development tutorials, I will get into the really advanced JavaScript API we have for the iPhone (touch detection, gestures... etc) so stay tuned!


 


Hope you found this useful,
Alex

Play a sound via JavaScript

3

Working in a project recently I found myself in the need of playing a sort of "alert" sound with JavaScript. It turns out there are not so many good options out there. The first thing that came to my mind was to use an embed and the Play and Stop JavaScript controls.

Well that only works in IE and some versions of Netscape so let's just forget about all those tags (embed, bgsound... etc)

So in my search for an easy solution I came across a very neat jQuery plugin that does exactly that job, the jQuery Sound Plugin by Jorn Zaefferer. I have been completely unable to find the plugin Website, so I will post it's code myself here. This plugin is a port from scriptaculous' version by Jules Gravinese.

How to use:

Using this to play a sound couldn't be easier, let's see an example with no parameters except for the file we want to play:

$.sound.play('files/sample.mp3')

This way the mp3 file sample will start playing. If we prefer the sound to start playing stopping the previous one, we can use tracks, this way we can play one sound per track, and every time we set a new sound for a certain track it stops the current sound there:

$.sound.play('files/sample.mp3',{
   track: "track1"
});

Other options are:

// timeout: Specify for how long the sound can play in milliseconds
$.sound.play('files/sample.mp3', {
   timeout: 4000
});

We can store a sound reference in an object so that we can later on stop it:

var sound = $.sound.play('files/sample.mp3');
sound.remove();

And finally we have the option to enable/disable sounds at all:

$.sound.enabled = false;
$.sound.enabled = true

The plugin code:

(function($) {
$.sound = {
	tracks: {},
	enabled: true,
	template: function(src) {
		return '<embed style="height:0" loop="false" src="' + src + '" autostart="true" hidden="true"/>';
	},
	play: function(url, options){
		if (!this.enabled)
			return;
		var settings = $.extend({
			url: url,
			timeout: 2000
		}, options);
		if(settings.track){
			if (this.tracks[settings.track]) {
				var current = this.tracks[settings.track];
				current.Stop && current.Stop();
				current.remove();
			}
		}
		var element = $.browser.msie
		  	? $('<bgsound/>').attr({
		        src: settings.url,
				loop: 1,
				autostart: true
		      })
		  	: $(this.template(settings.url));
		element.appendTo("body");
		if (settings.track) {
			this.tracks[settings.track] = element;
		}
		setTimeout(function() {
			element.remove();
		}, options.timeout)
		return element;
	}
};
})(jQuery);

I will probably minify this code before I upload it to the production server, so if I don't forget I will also post here the minified version,

Hope you found it useful,
Alex

The Programmers Hierarchy

0
The Programmers Hierarchy

The Programmers Hierarchy

I hope you enjoy this ;)

Our of the oven Flash Templates:

0

Flash MintI was building a site for a relative new company a couple weeks ago, and they wanted "A professional looking, brilliant display" for their products, I know Flash, and can do decent stuff with it, but seriously, how long does it take to do something really cool?

So I headed to the only place where I know this can be found for sure, Flash Mint, it was right then when I realized how much I had grown accustomed to it, the ease of use and friendliness of the design, and always full of new stuff!

You can even customize the products, get full website designs, and many flash templates for almost anything (Along with HTML, CSS, WordPress themes... etc) the list looks almost endless haha

Not so long ago I even bought a WordPress theme for a client I didn't have enough time to spend on. He was looking for something like that and I told him I could offer him a better price if he bought a pre-made template, so I showed him the ones that best fit his description and he ended up getting the "Personal Blog Theme", which looked really nice and fitted my client perfectly,

So if you are in a rush, or simply need a professional-looking Flash Movie, Template, or anything of the sort, this is a great alternative!

Perfect jQuery UI rotating tabs

3

Tabs are nice. They create a very elegant interface, and jQuery UI does this marvelously, here we have a little preview of this:

jQuery UI Tabs

jQuery UI Tabs

How to do that?

Include the files

With jQuery and jQuery UI it is dead simple. First, load the JS libraries, I recommend using the hosted files at Google for jQuery:

<script src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js" type="text/javascript"><!--mce:0--></script>
<script src="path/to/jquery UI" type="text/javascript"><!--mce:1--></script>

Remember to include also the CSS files for the UI Theme! Otherwise the tabs won't seem to work!

The HTML for the tabs:

We need a little HTML for our tabs to work:
A wrapper, a few divs with the content, and an unordered list with the tabs:

 
<div id="tabs-rotate">
<ul>
<li><a href="#tab1"><span>Tab 1</span></a></li>
<li><a href="#tab2"><span>Tab 2</span></a></li>
<li><a href="#tab3"><span>Tab 3</span></a></li>
</ul>
<div id="tab1">Tab 1 content</div>
<div id="tab2">Tab 2 content</div>
<div id="tab3">Tab 3 content</div>
</div>
 

The JavaScript:

Now comes the cool part, basically we want to have tabs. But although tabs are nice, people may not realize there is more content, or they might be just too lazy to browse through it, so, why not rotate through the tabs?
jQuery UI does that by itself with a very simple commant, but it is not perfect! So we need to program our tabs to rotate, unless the mouse is over them. This way when a user is looking for a link, the tab will wait!

So here is how this is achieved:

$(document).ready(function(){
	$("#tabs-rotate").tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 3000);
	$('#tabs-rotate').hover(function(){
			$(this).tabs('rotate', 0, false);
		},function(){
			$(this).tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 3000);
		}
	);
});

First we instruct jQuery UI to set up tabs in that div, and to rotate them. Then we use a hover event to control the rotation. On hover no rotation, and on out resume rotation.

Further actions:

Now you may want to stop rotation completely when a tab is clicked. This can be done "easily": First we will add a new handler for the onclick event to the tabs-rotate, which will remove rotation. But we also have to unbind the hover handler we had setup, using the unbind jQuery function:

 
$('#tabs').click(function(){
	$(this).tabs('rotate', 0, false);
	$(this).unbind('hover');
});
 

See it in action

You can see it working on the DJs Music homepage

As with the best stuff, it's terribly simple ;)

Hope you enjoy,
Alex

Go to Top