/* app-like one page layout by jayjnu to apply one page layout to your web page, it is crucial to set key values of frame, container and sections. app-like one page layout provides active radio nav animation. to activate this feature, create html elements in accordance with markup and style guides on readme.md. after that, set radio and radioon key values. { frame: "#id", container: "#id", sections: ".class", radio: "#id", radioon: "#id", speed: 500, easing: "swing" } */ function startonepage(myinput){ 'use strict'; var settings = myinput; console.log(myinput); // input values var frame = $(settings.frame), container = $(settings.container), sections = $(settings.sections), speed = settings.speed || 500, radio = $(settings.radio), radioon = $(settings.radioon), easing = settings.easing || "swing", afterload = settings.afterload, onleave= settings.onleave; /* boolean values to enable/disable default scroll action linked to 1) init() 2) animatescr() 3) scroll, keydown bound event handler default: true; */ var didscroll = true, isfocused = true; // common variables var height = $(window).height(); // index values for sections elements var totalsections = sections.length - 1; // currently displayed section number // modifying this variable will cause buggy behaviors. var num = 0; // keyboard input values // add more if necessary var pressedkey = {}; pressedkey[36] = "top"; // home pressedkey[38] = "up"; // upward arrow pressedkey[40] = "down"; // downward arrow pressedkey[33] = "up"; // page up pressedkey[34] = "down"; // page down pressedkey[35] = "bottom"; // end // init function to initialize/reassign values of the variables // to prevent section misplacement caused by a window resize. function init(){ height = $(window).height(); frame.css({"overflow":"hidden", "height": height + "px"}); sections.css({"height": height + "px"}); didscroll = true; isfocused = true; end = - height * ( totalsections ); container.stop().animate({margintop : 0}, 0, easing, function(){ num = 0; didscroll = true; turnonradio(0, 0); }); } // event binding to init function $(window).bind("load resize", init); // animate scrolling effect var now, end; function animatescr(moveto, duration, distance){ var top; duration = duration || speed; switch(moveto){ case "down": top = "-=" + ( height * distance ) + "px"; num += distance; break; case "up": top = "+=" + ( height * distance ) + "px"; num -= distance; break; case "bottom": top = end; num = totalsections; break; case "top": top = 0; num = 0; break; default: console.log("(error) wrong argument passed"); return false; } container.not(":animated").animate({margintop : top}, duration, easing, function(){ didscroll = true; }); if(radio){turnonradio(num, speed);} } // show active radio button function turnonradio(index, duration){ duration = duration || speed; onleave(index); settimeout(function(){ afterload(index); },speed); radioon.stop().animate({"top": index * radioon.outerheight( true )+ "px"}, speed, easing); } radio.children("li:not(" + settings.radioon + ")").click(function(){ var to = $(this).index(); var dif = math.abs( num - to ); if(num < to){ animatescr("down", speed, dif); }else if(num > to){ animatescr("up", speed, dif); } }); /* 1. get a type of event and handle accordingly 2. enable/disable default keyboard behavior */ $(document).bind("dommousescroll mousewheel keydown", function(e){ var etype = e.type; now = parseint( container.css("margintop") ); end = - height * ( totalsections ); // handles the event if( didscroll && isfocused ){ // prevent multiple event handling didscroll = false; // on wheel if( etype == "dommousescroll" || etype == "mousewheel" ){ var mvmt = e.originalevent.wheeldelta; if(!mvmt){ mvmt = -e.originalevent.detail; } // 휠로 스크롤을 올렸을때 if(mvmt > 0){ //만약 첫번째 영역이라면 if( now == 0){ didscroll = true; }else{ animatescr("up", 500, 1); } }else if(mvmt < 0){ //만약 마지막 영역이라면 if( now == end ){ didscroll = true; }else{ animatescr("down", 500, 1); } }else{ didscroll = true; } } // on keydown else if( etype == "keydown" ){ // 위아래로 움직이는 키를 눌렀을 때 발동 if( pressedkey[e.which] ){ e.preventdefault(); if( pressedkey[e.which] == "up" ){ // 만약 첫번째 영역이라면 if( now == 0 ){ animatescr("bottom"); }else{ animatescr("up", speed, 1); } }else if( pressedkey[e.which] == "down" ){ //만약 마지막 영역이라면 첫번째 화면으로 가기 if( now == end ){ animatescr("top"); }else{ animatescr("down", speed, 1); } }else{ // page down 또는 page up일 때 animatescr( pressedkey[e.which] ); } }else{ didscroll = true; } } } // enable default keyboard behavior when an input or textarea is being focused $("input, textarea").focus(function(){isfocused = false;}) .blur(function(){isfocused = true;}); }); }