Advertisement
Guest User

Página

a guest
Aug 19th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. <!doctype html>
  2. <html lang="pt-br">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Imagem de Fundo Preenchendo Toda a Tela</title>
  6. <style>
  7. * {
  8. margin: 0;
  9. padding:0;
  10. }
  11.  
  12. /* para garantir que estes elementos ocuparão toda a tela */
  13. body, html {
  14. width: 100%;
  15. height: 100%;
  16. font-family: Arial, Tahoma, sans-serif;
  17. }
  18.  
  19. #fundo-externo {
  20. overflow: hidden; /* para que não tenha rolagem se a imagem de fundo for maior que a tela */
  21. width: 100%;
  22. height: 100%;
  23. position: relative; /* criamos um contexto para posicionamento */
  24. }
  25.  
  26. #fundo {
  27. position: fixed; /* posição fixa para que a possível rolagem da tela não revele espaços em branco */
  28. width: 100%;
  29. height: 100%;
  30. }
  31.  
  32. #fundo img {
  33. width: 100%; /* com isso imagem ocupará toda a largura da tela. Se colocarmos height: 100% também, a imagem irá distorcer */
  34. position: absolute;
  35. }
  36.  
  37. #site {
  38. position: absolute;
  39. top: 50%;
  40. left: 50%;
  41.  
  42. }
  43.  
  44. p {
  45. margin-bottom: 1.5em;
  46. }
  47. </style>
  48. <script>
  49. function adaptImage(targetimg) {
  50. var wheight = $(window).height(); // altura da janela do navegador
  51. var wwidth = $(window).width(); // largura da janela do navegador
  52.  
  53. // removemos os atributos de largura e altura da imagem
  54. targetimg.removeAttr("width")
  55. .removeAttr("height")
  56. .css({ width: "", height: "" }); // removemos possíveis regras css também
  57.  
  58. var imgwidth = targetimg.width(); // largura da imagem
  59. var imgheight = targetimg.height(); // altura da imagem
  60.  
  61. var destwidth = wwidth; // largura que a imagem deve ter
  62. var destheight = wheight; // altura que a imagem deve ter
  63.  
  64. // aqui vamos determinar o tamanho final da imagem
  65. if(imgheight < wheight) {
  66. // se a altura da imagem for menor que a altura da tela, fazemos um cálculo
  67. // para redefinir a largura da imagem para bater com a altura que queremos
  68. destwidth = (imgwidth * wheight)/imgheight;
  69.  
  70. $('#fundo img').height(destheight);
  71. $('#fundo img').width(destwidth);
  72. }
  73.  
  74. // aqui utilizamos um cálculo simples para determinar o posicionamento da imagem
  75. // para que a mesma fique no meio da tela
  76. // posição = dimensão da imagem/2 - dimensão da tela/2
  77. destheight = $('#fundo img').height();
  78. var posy = (destheight/2 - wheight/2);
  79. var posx = (destwidth/2 - wwidth/2);
  80.  
  81. //se o cálculo das posições der resultado positivo, trocamos para negativo
  82. if(posy > 0) {
  83. posy *= -1;
  84. }
  85. if(posx > 0) {
  86. posx *= -1;
  87. }
  88.  
  89. // colocamos através da função css() do jquery o posicionamento da imagem
  90. $('#fundo').css({'top': posy + 'px', 'left': posx + 'px'});
  91. }
  92.  
  93. //quando a janela for redimensionada, adaptamos a imagem
  94. $(window).resize(function() {
  95. adaptImage($('#fundo img'));
  96. });
  97.  
  98. //quando a página carregar, fazemos o mesmo
  99. $(window).load(function() {
  100. $(window).resize();
  101. });
  102. </script>
  103. </head>
  104. <body>
  105.  
  106. <div id="fundo-externo">
  107. <div id="fundo">
  108. <img src="http://i.imgur.com/gxwbFyu.jpg" alt="" />
  109. </div>
  110. </div>
  111. <div id="site">
  112. <h1>Site Exemplo</h1>
  113. <p>
  114. blablabla...
  115. </p>
  116. </div>
  117.  
  118. </body>
  119. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement