Touching DOM as less as possible

This commit is contained in:
Dmitry Baranovskiy 2010-11-01 12:35:25 +08:00 committed by Vadim Makeev
parent 8550d1aa10
commit e3b69b8962
1 changed files with 27 additions and 19 deletions

View File

@ -1,49 +1,57 @@
$(function() {
var slides = $( 'section.slide' );
var domSlides = $( 'section.slide' ),
location = document.location,
slides = [],
backhash = {},
linkScreen = $( 'link[title=screen]' ),
linkProjection = $( 'link[title=projection]' );
for (var i = 0, len = domSlides.length; i < len; i++) {
var id = domSlides[i].id;
slides[i] = '#' + id;
backhash['#' + id] = i;
}
function fullScreen() {
return (
window.screenX == 0 &&
return window.screenX == 0 &&
window.screenY == 0 &&
screen.width == window.outerWidth &&
screen.height == window.outerHeight
) ? true : false;
screen.height == window.outerHeight;
}
function turnSlide( e ) {
if( !fullScreen() ) return;
var current = $( document.location.hash );
var current = backhash[location.hash],
target,
command;
switch ( e.which ) {
case 33 : // PgUp
case 38 : // Up
case 37 : // Left
var target = current.prev( slides );
current--;
break;
case 34 : // PgDown
case 40 : // Down
case 39 : // Right
var target = current.next( slides );
current++;
break;
case 32 : // Space
var target = current[ e.shiftKey ? 'prev' : 'next' ]( slides );
current += e.shiftKey ? -1 : 1;
break;
default:
return;
}
target = slides[current];
e.preventDefault();
if( !target.length ) return;
else document.location.hash = '#' + target.attr( 'id' );
if( target ) location.hash = target;
}
function updateView() {
var screen = $( 'link[title=screen]' ),
projection = $( 'link[title=projection]' ),
fullscreen = fullScreen();
screen.attr( 'disabled', fullscreen );
projection.attr( 'disabled', !fullscreen );
if( !fullscreen || $( document.location.hash ).length ) return;
else document.location.hash = '#' + slides.first().attr( 'id' );
var fullscreen = fullScreen();
linkScreen.attr( 'disabled', fullscreen );
linkProjection.attr( 'disabled', !fullscreen );
if( fullscreen && !backhash[location.hash]) location.hash = slides[0];
}
$( document ).
@ -53,4 +61,4 @@ $(function() {
ready( updateView ).
resize( updateView );
});
});