Advertisement
Guest User

Untitled

a guest
May 17th, 2011
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //iWebKit 6.0 Fullscreen + Slide + touch=hover
  2. function initcheckbox(){
  3.     var togglebox = "<div class='toggle_box'></div>";
  4.     $('input[type=checkbox]').css('display','none');
  5.     $('fieldset[data-input-type=checkbox]').append(togglebox);
  6.     var mouseDown = false;
  7.     var beginTouchDown = 0;
  8.     create_elements();
  9.     set_listeners();
  10.    
  11.    
  12. };
  13. function create_elements()
  14. {
  15.  
  16.     var inside_html        =    "<div class='switch' onmousedown='event.preventDefault ? event.preventDefault() : event.returnValue = false'></div>";
  17.     $('.toggle_box').each(function(){
  18.         $(this).attr('state','on');
  19.         $(this).html(inside_html);
  20.         $('.switch',this).css({'top':($(this).height()/2 - $('.switch',this).height()/2)});
  21.        
  22.         $('fieldset input[type=checkbox]:checked').parent().find('.switch').attr('state', 'on').css({ 'left': "52px" }).parent().css({ 'background-position': "147px -37px" });
  23.         });
  24.        
  25.        
  26. }
  27.  
  28.  
  29. function set_listeners()
  30. {
  31.     $('.toggle_box').mousedown(slide_begin);
  32.     $('.toggle_box').mousemove(slide_move);
  33.     $('.toggle_box').mouseup(slide_end);
  34.     $('.toggle_box').mouseout(slide_end);
  35.     $('.toggle_box').bind('touchstart',touch_start);
  36.     $('.toggle_box').bind('touchmove',touch_move);
  37.     $('.toggle_box').bind('touchend',slide_end);    
  38.  
  39. }
  40.  
  41.  
  42. function touch_start(e)
  43. {
  44.      
  45.     var parent         =    $(e.currentTarget);
  46.     var target         =    $('.switch',parent);
  47.     target.stop();
  48.    
  49.    
  50.     beginTouchDown = parseInt(e.originalEvent.touches[0].pageX,10)-(target.offset().left - parent.offset().left);
  51. }
  52.  
  53. function touch_move(e)
  54. {
  55.     e.preventDefault();
  56.     var parent         =    $(e.currentTarget);
  57.     var parentOffset   =    parent.offset().left;
  58.     var target         =    $('.switch',parent);
  59.     var currentLeft    =    0;
  60.     var toMove         =    parseInt(e.originalEvent.touches[0].pageX,10)-beginTouchDown+currentLeft;
  61.     if(!(toMove+parentOffset+target.width() > parentOffset+parent.width())&&(toMove+parentOffset > parentOffset))
  62.     {var color = $(target).parent().parent().find('input').data('color');
  63.     if(color == 'orange'){var bgy = '-1'}
  64.         else{var bgy = '-37'};
  65.        
  66.             $(target).css({'left':toMove+"px",'background':'black'});
  67.             $(target).parent().css({'background-position': + 94 + toMove+"px" + " " + bgy + "px"});
  68.            
  69. check_place(target);
  70.     }
  71.  
  72. }
  73.  
  74. function slide_move(e)
  75. {
  76.     if(mouseDown === true)
  77.     {
  78.         var parent         =    $(e.currentTarget);
  79.         var target         =    $('.switch',parent);
  80.         var parentOffset   =    parent.offset().left;
  81.         var toMove         =    e.pageX-parentOffset-target.width()/2;    
  82.         if(!(toMove+parentOffset+target.width() > parentOffset+parent.width())&&(toMove+parentOffset > parentOffset))
  83.         {var color = $(target).parent().parent().find('input').data('color');
  84.     if(color == 'orange'){var bgy = '-1'}
  85.         else{var bgy = '-37'};
  86.                
  87.             target.css({'left':toMove+"px",'background':'black'});
  88.             $(target).parent().css({'background-position': + 94 + toMove+"px" + " " + bgy + "px"});
  89.             check_place(target);
  90.         }
  91.  
  92.     }
  93.  
  94. }
  95. function slide_begin(e)
  96. {
  97.    var target        =    $('.switch',e.currentTarget);
  98.    if(target.is(':animated'))
  99.    {
  100.        target.stop();
  101.    }
  102.    mouseDown        =    true;
  103. }
  104.  
  105. function slide_end(e)
  106. {
  107.    mouseDown        =    false;
  108.    check_place($('.switch',e.currentTarget));
  109.    slide_to_state($('.switch',e.currentTarget));
  110.        
  111. }
  112.  
  113.  
  114. function slide_to_state(target)
  115. {
  116.     var toMove = target.attr('state') === "on" ? target.parent().width()-target.width() : 0;
  117.     target.css({'left':toMove+"px",'background':'transparent'});
  118.     var color = $(target).parent().parent().find('input').data('color');
  119.     if(color == 'orange'){var bgy = '-1'}
  120.         else{var bgy = '-37'};
  121.      $(target).parent().css({'background-position': + 94 + toMove+"px" + " " + bgy + "px"});
  122. }
  123. function check_place(target)
  124. {
  125.    
  126.     if(target.offset().left+target.width()/2 > target.parent().offset().left+target.parent().width()/2)
  127.     {
  128.         target.attr('state','on').addClass('active');
  129.        
  130.     }
  131.     else
  132.     {
  133.         target.attr('state','off').removeClass('active');
  134.        
  135.     }
  136. }
  137. function hijackLinks() {
  138.     initcheckbox();
  139.     $('fieldset input[type=checkbox]:checked').parent().find('.switch').attr('state','on') ;
  140.     $('a').live("click", function (e) {
  141.         e.preventDefault();        
  142.         if(direction != "leftnav" && direction != "rightnav"){setTimeout(function () {$(this).addClass('click')}, 10);};
  143.         var linkhref = $(this).attr('href');       
  144.         window.location.hash = linkhref;       
  145.     });    
  146.         $('a,input,fieldset[data-input-type=button],select,fieldset[data-input-type=select],footer a').live("touchstart", function (e) {
  147.             direction = $(this).attr('class');
  148.             if(direction != "leftnav" && direction != "rightnav"){$(this).addClass('temp');
  149.             setTimeout(function () {
  150.                 $('.temp').addClass('click');
  151.             }, 100);}
  152.             else{$(this).addClass('click');};
  153.             if(direction == "leftnav" || direction == "rightnav"){$('.click').live("touchend",          function (e) {
  154.                 $('.click').removeClass('click');
  155.             });
  156.         }
  157.            
  158.         });
  159.        
  160.         $('a').live("touchmove", function (e) {
  161.             $('.temp').removeClass('temp');
  162.             $('.click').removeClass('click');
  163.         });
  164.         $('select,fieldset[data-input-type=select]').live("click", function (e) {
  165.             $('.temp').removeClass('temp');
  166.             $('.click').removeClass('click');
  167.         });    
  168.        
  169.             $('input,select,fieldset[data-input-type=select]').live("touchmove change", function (e) {
  170.                 var inputvalue = $(this).val()
  171.                 $('input[value="' + inputvalue + '"] + label').css({
  172.                     '-webkit-transition': 'opacity 0.2s linear'});
  173.             if (e.type == 'touchmove'){
  174.                 $('input[value="' + inputvalue + '"]  + label').css({'-webkit-transition': '0',
  175.                     'opacity': '1'});
  176.            
  177.             $('.temp').removeClass('temp');
  178.             $('.click').removeClass('click');}
  179.             else{$('input[value="' + inputvalue + '"] + label').addClass('click')
  180.                 var inputvalue = $(this).val()
  181.              $('input[value="' + inputvalue + '"] + label').css({
  182.                     'opacity': '0'
  183.                 });
  184.             setTimeout(function () {
  185.                 $('input[value="' + inputvalue + '"]  + label').css({'-webkit-transition': '0',
  186.                     'opacity': '1'});
  187.             $('.temp').removeClass('temp');
  188.             $('.click').removeClass('click');
  189.             }, 200);}
  190.         });
  191.        };
  192.        
  193.        
  194.        
  195.  
  196.  
  197. function loadPage(url) {
  198.     currenthash = window.location.hash.substring(1);
  199.     if (currenthash === "") {
  200.         currenthash = "window.location";
  201.     }
  202.     if (url === undefined && currenthash != "") {
  203.         $('body').load(currenthash, 'header:first,#content,footer', hijackLinks);
  204.     } else {
  205.         $.get(url, function (data) {
  206.             $('body').append(data);
  207.             sm = $(window).width();
  208.             sn = $(window).scrollTop();
  209.             window.scrollTo(0, 0);   
  210.                  if (direction === "leftnav") {
  211.                 $('body>#content:last,footer:last,body>header:last>h1').css("-webkit-transform", "translate( -" + sm + "px,0px)");
  212.                 $('body>header:last>nav').css("-webkit-transform", "translate(-" +sm/2+"px, 0px)");
  213.                
  214.                
  215.                 footerheight = $('body>#content:last').outerHeight(false) + $('body>header:last').outerHeight(true);
  216.                 $('footer:last').css("top", footerheight);
  217.                 $('body>header,body>#content,footer,body>header:last>nav,body>header:last>h1,body>header:first>nav,body>header:first>h1,body>header:first').css("-webkit-transition-duration", "0.4s");
  218.                 $('body>header:first>h1,body>#content:first,footer:first').css("-webkit-transform", "translate(" + sm + "px,0px)");
  219.                 $('body>header:first>nav').css("-webkit-transform", "translate(" +sm/2+"px, 0px)");
  220.                 $('body>#content:last,footer:last,body>header:last>nav,body>header:last>h1,body>header:last div').css({
  221.                     "-webkit-transform": "translate(0,0)"
  222.                 });
  223.                
  224.                 $('body>header:last a.leftnav,body>header:last a.rightnav').css({
  225.                     '-webkit-transition': 'opacity 0.4s linear',
  226.                     'opacity': '1'
  227.                 });
  228.                 $('body>header:first a.leftnav,body>header:first a.rightnav').css({
  229.                     '-webkit-transition': 'opacity 0.4s linear',
  230.                     'opacity': '0'
  231.                 });
  232.                  setTimeout(function () {
  233.                 $('body>header:not(:last),body>footer:not(:last),body>#content:not(:last),body>meta,body>title,body>link,body>style,body>script').remove();
  234.                 $('body>header,body>footer,body>#content').removeAttr('style');
  235.             }, 400);
  236.                
  237.             }
  238.             if (direction !== "leftnav") {
  239.                 $('body>#content:last,footer:last,body>header:last>h1').css("-webkit-transform", "translate( " + sm + "px,0px)");
  240.                 $('body>header:last>nav').css("-webkit-transform", "translate(" +sm/2+"px, 0px)");
  241.                
  242.                
  243.                 footerheight = $('body>#content:last').outerHeight(false) + $('body>header:last').outerHeight(true);
  244.                 $('footer:last').css("top", footerheight);
  245.                 $('body>header,body>#content,footer,body>header:last>nav,body>header:last>h1,body>header:first>nav,body>header:first>h1,body>header:first').css("-webkit-transition-duration", "0.4s");
  246.                 $('body>header:first>h1,body>#content:first,footer:first').css("-webkit-transform", "translate(-" + sm + "px,0px)");
  247.                 $('body>header:first>nav').css("-webkit-transform", "translate(-" +sm/2+"px, 0px)");
  248.                 $('body>#content:last,footer:last,body>header:last>nav,body>header:last>h1,body>header:last div').css({
  249.                     "-webkit-transform": "translate(0,0)"
  250.                 });
  251.                
  252.                 $('body>header:last a.leftnav,body>header:last a.rightnav').css({
  253.                     '-webkit-transition': 'opacity 0.4s linear',
  254.                     'opacity': '1'
  255.                 });
  256.                 $('body>header:first a.leftnav,body>header:first a.rightnav').css({
  257.                     '-webkit-transition': 'opacity 0.4s linear',
  258.                     'opacity': '0'
  259.                 });
  260.                  setTimeout(function () {
  261.                 $('body>header:not(:last),body>footer:not(:last),body>#content:not(:last),body>meta,body>title,body>link,body>style,body>script').remove();
  262.                 $('body>header,body>footer,body>#content').removeAttr('style');
  263.                 initcheckbox()         
  264.             }, 400);
  265.             }
  266.         });
  267.     }
  268. }
  269. $(document).ready(function () {
  270.    
  271.   loadPage();
  272.            
  273.    currenthash = window.location.hash.substring(1);
  274.     $(window).bind('hashchange', function () {
  275.       currenthash = window.location.hash.substring(1);
  276.     loadPage(currenthash); 
  277.    
  278.    });
  279.    
  280. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement