Change banner according to screen size?

Asked

Viewed 7,455 times

3

Aiming for the best performance in the load time of my site I wanted to make the input page banner smarter.

For screens of up to 1600px would have to be loaded a banner simpler, of lower quality and smaller side size since it doesn’t have to be bigger, but for larger screens that this I would bring a banner with more details and better resolution.

inserir a descrição da imagem aqui

Supposing I had one <img id="banner"> how to make this move to change the src dynamically?

It’s common for that to happen on websites these days?

  • Media Queries believe it can solve your problem. It would only need some adaptations in your code. http://tableless.com.br/introducao-sobre-media-queries/ Practical examples: https://developers.google.com/web/fundamentals/media/images/images-in-css or http://www.smashingmagazine.com/2013/07/22/simple-responsive-images-withcss-background-images/

2 answers

5

yes quite normal. Voce can handle in css or js layers. Nowadays js offers a lot of screen resize support, with components in jquery ui. if you need to change size, bg and component layout, I suggest you use the CSS as follows:

#div{
   width: 600px;
   position: relative;
   margin: 10px auto;
}
@media (max-width:960px){
#div{
     width: 450px;
     margin: 0; 
    }
 }
 @media (min-width:480px){
#div{
     width: 100%;
     margin: 0; 
    }
 }

so you can make "if" species inside the css for each maximum or minimum screen size. overwriting only what will change from the pardrão css. Tools like Bootstrap help you manipulate screen elements with already written base css and quite responsive to standard screen sizes.

2

If you want to do in javascript:

// convém estar no onload da janela
window.onload = function()
{

  // primeiro tens de sacar a dimensão da janela
  // neste caso a área visível do browser
  var largura = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);

  // depois é brincar com if's e colocar o banner que queiras:

  if(largura>=1600)
  {
    // mete o banner com mais detalhe
  } else {
    // mete o banner com menos detalhe
  }
} // fim do window.onload

Browser other questions tagged

You are not signed in. Login or sign up in order to post.