Posted September 4th, 2010 17:43 by Bob

A quick demo of using jQuery for parallax. This example just creates a nice visual effect but it could easily be converted for more constructive purposes but I’ll leave that up to you.
Parallax (for the sake of this post) is the effect whereby things at different distances move at different rates, for example looking out of a train window the closer items are the faster they move past you. This is as horribly simplified and inaccurate description but will suffice for the sake of this example.
I’ve used three transparent PNGs for this but you can use as many layers as you like or easily replace the PNGs with div tags containing whatever content you need.
View the demo.
View the javascript source.
The HTML
This is very simple. The html you need is as follows:
-
-
<img src=“images/Bottom.png” />
-
<img src=“images/Middle.png” />
-
<img src=“images/Top.png” />
-
See, told you it was simple. The div is the outer container into which the layers belong. Add the images in order of the bottom one (smallest) first. Job done.
The Images (or layers)
The images (or layers) you use should each be a different size, the bigger the image the more movement. Each image must be at least as big as the viewer, if you set one side of it to be the same size as the viewer and another to be larger it will only move on that axis, which is a nice effect in itself. So if your image is the same height as the viewer but wider it will only move horizontally.
This may be stating the obvious, but I’ve used transparent pngs, you must be able to see the lower images through the upper ones. To create them I used a photoshop file with a group for each image. This meant I could check how they would look before I output them. You can view the three images here:
The CSS
You can view the entire CSS file here. The important parts are as follows.
-
#Parallax {
-
background-color: #880088;
-
height: 500px;
-
overflow: hidden;
-
position: relative;
-
width: 750px;
-
}
The background color can be whatever you fancy. Bear in mind that if all your layers are transparencies the background color will show through. Of course you could make the bottom layer fully colored, patterned or whatever you fancy. You could also give the #Parallax div a background image if you wanted.
The width and height are important, remember your images should all the same dimensions or bigger than this div. Overflow must be set to hidden to ensure that tags within this div will not cause it to change it’s size and will not poke out of the sides.
The relative position is required as we will be setting the tags within the div to absolute and we want them to be positioned relatively to this div, not to the document.
-
#Parallax img {
-
position: absolute;
-
top: 0;
-
left: 0;
-
}
Each of the images within the div (or whatever you use for layers) must be set to use absolute positioning so that we can move them around with jQuery. I’ve set their initial positions to the top left corner but you can set them to wherever suits.
The Javascript
This uses jQuery for the heavy lifting. It could be converted into a handy little jQuery plugin but I wanted to keep it as simple as possible for the sake of the example, plus others have already done that.
-
jQuery(document).ready(function($){
-
$(’#Parallax’).mousemove(
-
function(e){
-
/* Work out mouse position */
-
var offset = $(this).offset();
-
var xPos = e.pageX - offset.left;
-
var yPos = e.pageY - offset.top;
-
-
/* Get percentage positions */
-
var mouseXPercent = Math.round(xPos / $(this).width() * 100);
-
var mouseYPercent = Math.round(yPos / $(this).height() * 100);
-
-
/* Position Each Layer */
-
$(this).children(‘img’).each(
-
function(){
-
var diffX = $(’#Parallax’).width() - $(this).width();
-
var diffY = $(’#Parallax’).height() - $(this).height();
-
-
var myX = diffX * (mouseXPercent / 100);
-
var myY = diffY * (mouseYPercent / 100);
-
var cssObj = {
-
‘left’: myX + ‘px’,
-
‘top’: myY + ‘px’
-
}
-
$(this).animate({left: myX, top: myY},{duration: 50, queue: false, easing: ‘linear’});
-
-
}
-
);
-
-
}
-
);
-
});
What this does is whenever the mouse is moved over the viewer (#Parallax div) it calculates it’s position over the div (as percentages) then moves each of the inner images by that same percent of their overflow.
Firstly work out the xPos and yPos. To do this we subtract the viewers x and y offset from the x and y mouse position. This is because the mouse positions returned are relative to the document not to the viewer. The offset values give us the viewers position relative to the document.
Next, work out the horizontal and vertical position of the mouse pointer as a percentage (0 = the top of the viewer, 100 = the bottom).
Loop through each image, for each one work out the difference in width and height between the image and the viewer. The image’s new position for each axis is the percentage calculated earlier for that axis of the difference for that axis. So if the mouse is dead center on the viewer the horizontal position is 50% of the difference between the image width and the viewer width.
Finally, we create an object containing the new CSS settings and apply it via a very short animation which gives the effect a nice smooth finish. You can just jump to the new position but it looks a little too rough for my tastes.
Links
Posted in Code | Comments Off
Tags: Code, jQuery, Parallax, Tutorial, visual effect
Posted August 16th, 2010 20:49 by Bob
We’ve just launched the new Shinytastic website which gave me a chance to experiment with some new techniques. The most interesting has to be the addition of Facebook like buttons to the website (down in the footer). The ability to directly integrate your website with a social media site is going to really change how we use the web, hopefully in a good way. Making it much easier for users to comment on your website and your business will give you a great instant response when they have an enquiry or something positive to say and will generally improve your communication with customers and potential customers.
There are two types (at the time of writing) of Facebook ‘Like’ buttons you can add to your website, the difference caused a little confusion at first but it’s simple enough. The first type is a general purpose Like button which you can stick on any page anywhere to allow users to like that page, when they do it creates a link on their facebook feed which links back to the page they liked. This is great for a blog or any website where you might want users to be able to like specific pages or pieces of information. The other type (which we have used) is a like box which is linked to a Facebook Page (for a business, entertainer, etc). When a user clicks a like box button it adds an entry to their feed which links back to the Facebook Page the button is associated with. This is better if you want to point people at your organisations Facebook Page and not at specific articles or pieces of information. The implementation is pretty much identical.
There are two ways you can add a like button (either of them) to your website, the easy way using an iframe which I’m not going to cover as facebook generates all the code you need for you and the more complicated but much more flexible manner using XFBML. I wanted to use XFBML because you have a lot more control over how it looks and integrates with your site, plus it was a challenge. Although all the documentation needed is out there (mostly on the Facebook Developers site) it’s a little scattered so here’s the steps involved in as straight-forward a manner as I can put them. I’ll try not to be too verbose with the supporting notes.
Namespaces, Doctypes and Validity
The first thing you need to do to your page to run XFBML is add the Facebook and Open Graph namespaces. Find your root html tag which will look something like this:
-
<html xmlns=“http://www.w3.org/1999/xhtml”>
Add the Open Graph and Facebook namespaces as follows
-
<html xmlns=“http://www.w3.org/1999/xhtml”
-
xmlns:og=“http://opengraphprotocol.org/schema/”
-
xmlns:fb=“http://www.facebook.com/2008/fbml”>
The Facebook namespace is so that you can use the XFBML tags the Open Graph is so that you can use the Open Graph meta tags which I’ll come to in a moment. First off though there’s a bit of a problem to overcome. As a rule I stick to XHTML 1.0 Transitional but HORROR if you use XFBML tags with that Doctype your page will not validate, now this is not an issue for everyone but it’s a serious problem for me. The solution is you need to use the RDFa version instead, which I’m afraid kids means you need to write strict xhtml (unless you’re using HTML5 already in which case check out this handy link). This is not the time for a full explanation, the short of it is you’ll need to change your doctype to this (assuming you are using XHTML 1.0 otherwise just have a little google for whichever HTML version you are using + RDFa):
-
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML+RDFa 1.0//EN”
-
“http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd”>
Our site still doesn’t actually validate 100%, we have some errors referencing the XFBML tags, but I can deal with that (for now) as the code was supplied by Facebook. That’s the hard part out of the way.
Adding the Javascript SDK
Use of the XFBML tags for like buttons requires the Facebook Javascript SDK. This is simply a case of going here to the Facebook SDK page, cutting and pasting the supplied code into your page. Stick it just above the closing body tag. Job done.
Add the XFBML tag
Facebook supplies handy generators for you here. You’ll need a different tag depending on your preference.
Like Buttons (articles, specific web-pages, etc)
Click here to go to the Facebook page to generate the tag. You can pretty much leave the form as it is. You should be given some code a bit like this:
Yup, that’s it. That’s all you need. You can add attributes for specific layout and control features like this if you fancy, see the Facebook documentation for specific info:
-
<fb:like show_faces=“false” width=“300” action=“recommend”></fb:like>
Like Boxes
Like boxes are the slightly more advanced siblings to the Like buttons. They are tied to a Facebook page. Quickest way to get the code is to visit your Facebook page (you must be logged in), click Get Started then click Add Like Box. This will take you to the generator page with all your details pre-filled. Set the settings how you fancy them and click Get Code and you shall receive something like this:
-
<fb:like-box profile_id=“185550966885”></fb:like-box>
Stick your supplied tag wherever in the page you want it to go then configure your Open Graph Meta tags…
Open Graph Meta Tags
You can read all about the Open Graph Protocol here. For the sake of this article the Open Graph tags are additional meta tags you can add to your document to add specific information about the document which will be used by Facebook. There’s some information on the Facebook Like page on how to use them. Add the meta tags to the head section of your document, they should look a little like this (with your information in, not ours obviosuly):
-
<meta property=“og:title” content=“Shinytastic Web Design”/>
-
<meta property=“og:type” content=“website”/>
-
<meta property=“og:email” content=“bob@shinytastic.com”/>
-
<meta property=“og:image” content=“http://www.shinytastic.com/files/ShinyFB.jpg”/>
-
<meta property=“og:phone_number” content=“01628 627289″/>
-
<meta property=“og:site_name” content=“Shinytastic Web Design”/>
-
<meta property=“og:url” content=“http://www.shinytastic.com”/>
-
<meta property=“fb:admins” content=“100001073515935”/>
title, email, phone_number, url and site_name are pretty obvious. type must be one of a very specific list available on the Open Graph website. image image is more important than you expect, I intially didn’t specify it assuming Facebook would either not use an image or would use the first image on the page. Nope, it used one of our clients logos so anyone who liked our page ended up with a confusing link to our website using our client’s logo. Facebook explain the image as follows:
The URL to an image that represents the entity. Images must be at least 50 pixels by 50 pixels. Square images work best, but you are allowed to use images up to three times as wide as they are tall.
Final item of importance is the fb:admins entry. This must contain a comma seperated list of user id’s who are responsible for this page. To get your Facebook id go to your profile and look at the url, your id is the part after the ?id=. My profile is http://www.facebook.com/profile.php?id=100001073515935 so my id is 100001073515935.
To get the like button working one of the users listed in fb:admins must visit the page and click the like button.
That’s it. It’s pretty simple really. I’ve only covered very basic integration here, there’s loads more you can do with a bit of research. Let me know how you get on.
Those Links Again
Posted in Code | 1 Comment »
Tags: Code, Facebook, Social Media, Tutorial, XFBML