The page flip effect used to be the quintessential Flash animation. On the web, it has powered everything from magazines to presentations, with its popularity declining over time, only to be reinvented on mobile devices as ebook reading apps.
In this tutorial we are going to use PHP and the turn.js plugin, an implementation of the page flip effect with pure CSS3 and jQuery, to build a pretty magazine. We will fetch the most popular images from Instagram every hour, and use them as pages.
HTML
First we need to lay down the foundations of today’s example. We will use a single page design, which combines HTML5 markup and PHP in the same file for greater simplicity. You can see the resulting layout below:
index.php
Making an Instagram Magazine , "data": [ "tags": ["beautiful", "sky"], "location": "null", "comments": "count": 31, "data": [...] , "filter": "Normal", "created_time": "1331910134", "link": "http://instagr.am/p/IPNNknqs84/", "likes": "count": 391, "data": [..] , "images": "low_resolution": "url": "http://distilleryimage8.instagram.com/03c80dd86f7911e1a87612313804ec91_6.jpg", "width": 306, "height": 306 , "thumbnail": "url": "http://distilleryimage8.instagram.com/03c80dd86f7911e1a87612313804ec91_5.jpg", "width": 150, "height": 150 , "standard_resolution": "url": "http://distilleryimage8.instagram.com/03c80dd86f7911e1a87612313804ec91_7.jpg", "width": 612, "height": 612 }, "caption": "created_time": "1331910148", "text": "Goodnight.ue056", "from": "username": "jent99", "profile_picture": "http://images.instagram.com/profiles/profile_6227738_75sq_1319878922.jpg", "id": "6227738", "full_name": "jent99" , "id": "148395540733414783" }, "type": "image", "id": "148395420004568888_6227738", "user": "username": "jent99", "website": "", "bio": "Mostly nature pics.ue32bue32bue32b Hope you like them.ue056ue32a ue334giue334 ", "profile_picture": "http://images.instagram.com/profiles/profile_6227738_75sq_1319878922.jpg", "full_name": "jent99", "id": "6227738" }, /* More photos here*/ ] }
The API is limited to returning only 32 pics, but this is plenty for our example. You can see that each photo has three image sizes, but we will only be needing the standard one. There is also various other information that you can use like caption, dimensions, tags, comments, and more.
PHP will cache the results of this API call so we hit Instagram’s servers only once per hour. This will make our application more responsive and limit the number of calls.
index.php
// You can obtain this client ID from the Instagram API page $instagramClientID = '-- place your client id key here --'; $api = 'https://api.instagram.com/v1/media/popular?client_id='.$instagramClientID; $cache = 'cache.txt'; if(file_exists($cache) && filemtime($cache) > time() - 60*60) // If a cache file exists, and it is // fresher than 1 hour, use it $images = unserialize(file_get_contents($cache)); else // Make an API request and create the cache file // Fetch the 32 most popular images on Instagram $response = file_get_contents($api); $images = array(); // Decode the response and build an array foreach(json_decode($response)->data as $item) $title = ''; if($item->caption) $title = mb_substr($item->caption->text,0,70,"utf8"); $src = $item->images->standard_resolution->url; $images[] = array( "title" => htmlspecialchars($title), "src" => htmlspecialchars($src) ); } // Remove the last item, so we still have // 32 items when when the cover is added array_pop($images); // Push the cover in the beginning of the array array_unshift($images,array("title"=>"Cover", "src"=>"assets/img/cover.jpg")); // Update the cache file file_put_contents($cache,serialize($images)); } # Generate the markup $totalPages = count($images); foreach($images as $i=>$image) ?>//Caching is straightforward: we are using a temporary file – cache.txt – to store a serialized representation of the image array. If the cache file is non-existing or is older than an hour, we issue a new API request.
Notice the calls to array_pop and array_unshift. These are necessary as to make room for the custom cover image that is stored in assets/img. The magazine works best if we have an even number of pages, otherwise we would be unable to turn the last one, which would feel unnatural.
We are now ready for the plugin!
jQuery
Using turn.js is really simple. As we already have the markup of the magazine, we just need to call the turn() method. While we are at it, we will also listen for presses on the arrow keys, which will trigger page transitions.
assets/js/script.js
$(function() var mag = $('#magazine'); // initiazlie turn.js on the #magazine div mag.turn(); // turn.js defines its own events. We are listening // for the turned event so we can center the magazine mag.bind('turned', function(e, page, pageObj) if(page == 1 && $(this).data('done')) mag.addClass('centerStart').removeClass('centerEnd'); else if (page == 32 && $(this).data('done')) mag.addClass('centerEnd').removeClass('centerStart'); else mag.removeClass('centerStart centerEnd'); }); setTimeout(function() // Leave some time for the plugin to // initialize, then show the magazine mag.fadeTo(500,1); ,1000); $(window).bind('keydown', function(e) // listen for arrow keys if (e.keyCode == 37) mag.turn('previous'); else if (e.keyCode==39) mag.turn('next'); }); });You can read more about what events the plugin emits and how to use them, in the turn.js reference.
Now let’s make it pretty!
CSS
We need to set explicit dimensions of the magazine and style the pages and page numbers. turn.js will handle the rest.
assets/css/styles.css
#magazine width:1040px; height:520px; margin:0 auto; position:relative; left:0; opacity:0; -moz-transition:0.3s left; -webkit-transition:0.3s left; transition:0.3s left; #magazine .page width:520px; height:520px; background-color:#ccc; overflow:hidden; /* Center the magazine when the cover is shown */ #magazine.centerStart left:-260px; /* Center the magazine when the last page is shown */ #magazine.centerEnd left:260px; .page img height:520px; width:520px; display:block; /* Show a dark shadow when the cover is shown */ .centerStart .turn-page-wrapper:first-child box-shadow:0 0 10px #040404; /* Page Numbers */ span.pageNum background-color: rgba(0, 0, 0, 0.3); bottom: 25px; box-shadow: 0 0 3px rgba(0, 0, 0, 0.25); color: #FFFFFF; font-size: 11px; height: 24px; line-height: 22px; opacity: 0.9; position: absolute; text-align: center; width: 55px; span.pageNum.left left:0; right:auto; span.pageNum.right left:auto; right:0; /* Hide the page number on the cover */ #page1 .pageNum display:none;With this our magazine is complete!
We’re done!
This example works in all recent browsers – Firefox, Chrome, Safari, Opera and even IE. It is even usable on iOS and Android. You can use this effect as part of photo galleries, templates or even real magazines. However you will have to create a fallback version for older browsers, which don’t have what it takes to display it properly.
|
In this tutorial we are going to use PHP and the turn.js plugin, an implementation of the page flip effect with pure CSS3 and jQuery, to build an Instagram powered magazine.}
Read more : Making a Page Flip Magazine with turn.js
0 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.