bitetti

init of web-page parallax scrolling

Nov 24th, 2011
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
  2.  
  3.  
  4. <!--
  5.     Parallax Effect
  6.             http://www.wildwitchproject.com/
  7.             http://www.rpgvale.com.br/
  8.             htpp://blogletar.blogspot.com/
  9.            
  10.     Licenced by: Creative Commons 3.0
  11.    
  12.     Requeriments:
  13.         JQuery Framework
  14.    
  15.     Parallax é um efeito clássico de simulação de 3D muito usado no Super Nintendo
  16.     ( http://en.wikipedia.org/wiki/Parallax_scrolling ) e outros consoles.
  17.     A diferença deste script para à maioria dos efeitos parallax de páginas web se
  18.     deve ao fato dele trabalhar com objetos e não apenas com scroll de imagens.
  19.    
  20.     instalação
  21.     O coneúdo padrão do seu blog deve ficar um nível acima do corpo. Assim:
  22.     #main {
  23.         position: relative;
  24.         z-index: 1;
  25.     }
  26.    
  27.     Criando layers
  28.     Use a função
  29.     function parallax_addLayer(ob, scale, x, y)
  30.     onde:
  31.         ob = Jquery Object, como por exemplo $("#minhaImagem")
  32.         scale = Quantidade de vezes que o movimento é menor doque a página. Ex.: 8 para o background
  33.         x = posicionamento X apartir do centro absoluto da tela (este script é para blogs e este é o posicionamento clássico)
  34.             exemplo: se o corpo do seu blog tem 900px de largura e você esta usando uma imagem de 256px de largura você pode
  35.             posicionar ela em x= -450 - 256  (onde -450 é a metade da largura para a imagem aparecer na margem esquerda e -256
  36.             é para ela sair de baixo da margem esquerda)
  37.         y = posicionamento Y apartir do topo do documento, ele aceita 2 (duas) formas:
  38.             Número inteiro => um valor absoluto de posicionamento
  39.             Fração => Entre 0 e 1, exemplo 0.5. Posiciona o elemento proporcionalmente em relação ao documento. Por exemplo o
  40.                 valor 0.5 posiciona o elemento na metade do scroll.
  41.    
  42.     Adicionalmente além de você poder usar imagens o script suporta objetos Flash e SVG
  43.     Para inserir o SVG:
  44.     parallax_initSVGContainer(id,src,w,h)
  45.     onde:
  46.         id = nome do elemento (ID), único para o documento
  47.         src = url do arquivo SVG
  48.         w = largura do SVG
  49.         h = altura do SVG
  50.    
  51.     Exexmplos:
  52.         // SVG
  53.         parallax_addLayer(parallax_initSVGContainer("parlx_L0","a.svg",256,256),  2, 0,0.5);
  54.         // SWF (Flash)
  55.         parallax_addLayer(parallax_initSWFContainer("parlx_L0","a.swf",256,256),  2, 0,0.5);
  56.         // Imagem
  57.         parallax_addLayer(parallax_initIMGContainer("parlx_L2","rundog.gif",64,64),  4, 100,0.5);
  58.    
  59.    
  60.     Compatibilidade
  61.         O script é compatível com HTML4 e HTML5
  62.         SVG não funciona no Internet Explorer, se precisar de gráficos vetoriais nele use o Flash.
  63. -->
  64. <style>
  65. #parallaxContainer {
  66.     position: fixed;
  67.     z-index: 0;
  68.     width: 100%;
  69.     height: 100%;
  70.     left: 0px;
  71.     top: 0px;
  72.     margin: 0;
  73.     padding: 0;
  74.     overflow: hidden;
  75.     background: fixed;
  76.     background-position: 0 0;
  77. }
  78.  
  79. .parallaxLayers {
  80.     position: absolute;
  81.     z-index: 0;
  82.     border: 0px none;
  83.     margin: 0px;
  84.     padding: 0px;
  85.     overflow: hidden;
  86. }
  87. </style>
  88. <script type="text/javascript">
  89. <!-- //
  90. var parallax_Box;
  91. var parallax_Layer0;
  92. var parallax_Layer0Inf = {mp:0,sc:1};
  93. var parallax_layers = [];
  94.  
  95. function parallax_initSVGContainer (id,src,w,h)
  96. {
  97.     var namespacesvg = "http://www.w3.org/2000/svg";
  98.     var svg;
  99.     // verifica suporte à SVG, se não tiver com certeza é IE
  100.     if(document.createElementNS)
  101.     {
  102.         try
  103.         {
  104.             svg = document.createElement("iframe");
  105.             svg.className = "parallaxLayers";
  106.             svg.setAttribute("id", id);
  107.             $(svg).css("width",w.toString()+"px").css("height",h.toString()+"px");
  108.             svg.src = src;
  109.             parallax_Box.appendChild(svg);
  110.             return $(svg);
  111.         } catch (e) {/*alert(e.message);*/};
  112.     }
  113.     return null;
  114. }
  115.  
  116. function parallax_initSWFContainer (id,src,w,h)
  117. {
  118. }
  119.  
  120. function parallax_initIMGContainer (id,src,w,h)
  121. {
  122.     var img = document.createElement("img");
  123.     img.className = "parallaxLayers";
  124.     img.setAttribute("id", id);
  125.     $(img).css("width",w.toString()+"px").css("height",h.toString()+"px");
  126.     img.setAttribute("src",src);
  127.     parallax_Box.appendChild(img);
  128.     return $(img);
  129. }
  130.  
  131. function parallax_setBackground (src, maxPixels, scale)
  132. {
  133.     parallax_Layer0.css("background","url('" + src + "')");
  134.     parallax_Layer0Inf = {mp:maxPixels, sc:(1/scale)};
  135. }
  136.  
  137. function parallax_top(ph,s,sc,x)
  138. {
  139.     s = 1/s;
  140.     return parseInt(ph*x*s + ph*sc*s);
  141. }
  142.  
  143. function parallax_bottom(ph,s,sc,x)
  144. {
  145.     s = 1/s;
  146.     return parseInt(ph*x*s + ph*sc*s);
  147. }
  148.  
  149. function parallax_addLayer(ob, scale, x, y)
  150. {
  151.     var _y = 0;
  152.     if (y<1 && y>=0) // medida em proporção
  153.         _y = y;
  154.     else
  155.         _y = y / ($(document).height() - $(window).height());
  156.     parallax_layers.push({ob:ob, sc:scale,x:x,y:_y});
  157. }
  158.  
  159. function parallax_size(e)
  160. {
  161.     var w = Math.floor($(window).width() / 2);
  162.     for( var l in parallax_layers)
  163.     {
  164.         var ly = parallax_layers[l];
  165.         ly.ob.css("left", String(w+ly.x) + "px");
  166.     }
  167. }
  168.  
  169. function parallax_run(e)
  170. {
  171.     var wh = $(window).height();
  172.     var ph = $(document).height() - wh;
  173.     var scT = $(document).scrollTop();
  174.     var mSc = -scT / ph;
  175.  
  176.     parallax_Layer0.css("background-position","center "+parseInt(parallax_Layer0Inf.mp*mSc*parallax_Layer0Inf.sc).toString()+"px");
  177.     //parallax_Layer1.css("top",parseInt(ph*0.5/8 + ph*mSc/8).toString()+"px");
  178.     for( var l in parallax_layers)
  179.     {
  180.         var ly = parallax_layers[l];
  181.         ly.ob.css("top", parallax_top(ph,ly.sc,mSc,ly.y).toString() + "px");
  182.     }
  183. }
  184.  
  185. /**
  186. * Inicializa o widget parallax
  187. **/
  188. function parallax_init()
  189. {
  190.     //Nucleo do widget (não mecha)
  191.     parallax_Box = document.createElement("div");
  192.     parallax_Box.id = "parallaxContainer";
  193.     document.body.insertBefore(parallax_Box, document.body.firstChild);
  194.  
  195.     // Modificar aqui para atender suas necessidades
  196.     // Ver tutorial em  para detalhes
  197.     parallax_Layer0 = $(parallax_Box);
  198.     parallax_addLayer(parallax_initSVGContainer("parlx_L0","a.svg",256,256),  2, 0,0.5);
  199.     parallax_addLayer(parallax_initSVGContainer("parlx_L1","a.svg",692,500),  4, 0,0.5);
  200.     //parallax_addLayer(parallax_initIMGContainer("parlx_L2","rundog.gif",64,64),  4, 100,0.5);
  201.     parallax_addLayer(parallax_initSWFContainer("parlx_L3","rundog.gif",64,64),  4, 100,0.5);
  202.    
  203.     parallax_setBackground("Ledremoto.png",500,8);
  204.    
  205.     //Ajustes finais
  206.     $(window).scroll(parallax_run);
  207.     $(window).resize(parallax_size);
  208.     parallax_size(null);
  209.     parallax_run(null);
  210. }
  211.  
  212.  
  213. $("document").ready(parallax_init);
  214.  
  215.  
  216. // -->
  217. </script>
  218.  
Advertisement
Add Comment
Please, Sign In to add comment