bmstu-mt-wp/scripts/script.js

110 lines
2.7 KiB
JavaScript
Raw Normal View History

2010-11-15 16:45:40 +03:00
(function() {
2011-07-12 15:08:29 +04:00
var url = window.location,
2011-07-12 15:08:29 +04:00
body = document.body;
slides = document.querySelectorAll('div.slide'),
slideList = [], hashList = {},
2010-11-15 16:45:40 +03:00
progress = document.querySelector('div.progress div'),
fullscreen = false;
2010-10-30 04:16:19 +04:00
for(var i = 0, slidesLength = slides.length; i < slidesLength; i++) {
var id = slides[i].id;
slideList[i] = '#' + id;
hashList['#' + id] = i;
2011-07-12 15:08:29 +04:00
slides[i].addEventListener('click', function(){
enterSingle();
turnSlide(hashList['#' + this.id]);
}, false);
}
2010-11-01 07:35:25 +03:00
2011-07-12 15:08:29 +04:00
function resizeBody(p) {
if(typeof p == 'boolean' && p) {
var transform = 'none';
} else {
var sx = body.clientWidth / window.innerWidth,
sy = body.clientHeight / window.innerHeight,
transform = 'scale(' + (1/Math.max(sx, sy)) + ')';
}
body.style.MozTransform = transform;
body.style.WebkitTransform = transform;
body.style.OTransform = transform;
body.style.msTransform = transform;
body.style.transform = transform;
}
2011-07-12 15:18:02 +04:00
function turnSlide(e) {
var current = hashList[url.hash],
target;
2011-07-12 15:08:29 +04:00
if(e.type == 'keyup') { // Key-Based
switch(e.which) {
case 33 : // PgUp
case 38 : // Up
case 37 : // Left
case 72 : // h
case 75 : // k
current--;
break;
case 34 : // PgDown
case 40 : // Down
case 39 : // Right
case 76 : // l
case 74 : // j
current++;
break;
case 36 : // Home
current = 0;
break;
case 35 : // End
current = slideList.length-1;
break;
case 32 : // Space
current += e.shiftKey ? -1 : 1;
break;
}
e.preventDefault();
2011-07-12 15:08:29 +04:00
} else {
current = e || 0;
2010-11-08 02:26:01 +03:00
}
2011-07-12 15:08:29 +04:00
target = slideList[current];
if(target) url.hash = target;
2010-11-15 16:45:40 +03:00
updateProgress();
2010-11-08 02:26:01 +03:00
}
2010-10-30 04:16:19 +04:00
2011-07-12 15:08:29 +04:00
function enterSingle() {
body.className = 'full';
2011-07-12 15:08:29 +04:00
resizeBody();
updateProgress();
window.addEventListener('resize', resizeBody, false);
document.addEventListener('keyup', turnSlide, false);
document.addEventListener('keyup', exitSingleEsc, false);
}
function exitSingle() {
body.className = 'list';
2011-07-12 15:08:29 +04:00
resizeBody(true);
window.removeEventListener('resize', resizeBody, false);
window.removeEventListener('keyup', turnSlide, false);
document.removeEventListener('keyup', exitSingleEsc, false);
}
function exitSingleEsc(e) {
if(e.which != 27) return;
exitSingle();
url.hash = '';
}
function checkHash() {
// if(!window.location.search.length) history.pushState(null, null, window.location.href + '?list');
2011-07-12 15:08:29 +04:00
if(typeof hashList[url.hash] != 'undefined') {
enterSingle();
}
}
2011-07-12 15:08:29 +04:00
2010-11-15 16:45:40 +03:00
function updateProgress() {
2010-11-16 00:39:10 +03:00
if(!progress) return;
2010-11-15 16:45:40 +03:00
progress.style.width = (100/(slideList.length-1) * hashList[url.hash]).toFixed(2) + '%';
}
2010-10-30 04:16:19 +04:00
2011-07-12 15:08:29 +04:00
window.addEventListener('DOMContentLoaded', checkHash, false);
2010-11-08 02:26:01 +03:00
})();