Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
change header image on scroll

Right now I have one fixed image in the header. I want to say that when half the page was scrolled, the picture in the header changed to another.

Do I need to use javascript for this or can I get by with css?

body {
    height:1200px;
    width:100%;
    background-color:#fbfbfb;
}
.header_nav {
    width:100%;
    height:150px;
    background-color:#666;
    position:fixed;
    top:0;
    left:0;
    color:#fff;
    background-repeat: no-repeat;
    background-position: center center;
}

img {
  width: 150px;
  display: block;
  margin: 0 auto;
}
<div class="header_nav"><img src="https://i.picsum.photos/id/244/200/200.jpg?hmac=Q1gdvE6ZPZUX3nXkxvmzuc12eKVZ9XVEmSH3nCJ2OOo"></div>

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use scrollTop() and specify when exactly the image will change

$(function () { 
    $(window).scroll(function () {
        if ($(this).scrollTop() > 600) { 
            $('.header_nav img').attr('src','https://i.picsum.photos/id/912/200/200.jpg?hmac=tYYyMFni6bya5yEVkwmmFekjWGedHVByLtPI5q1lcyw');
        }
        if ($(this).scrollTop() < 600) { 
            $('.header_nav img').attr('src','https://i.picsum.photos/id/244/200/200.jpg?hmac=Q1gdvE6ZPZUX3nXkxvmzuc12eKVZ9XVEmSH3nCJ2OOo');
        }
    })
});
body {
    height:1200px;
    width:100%;
    background-color:#fbfbfb;
}
.header_nav {
    width:100%;
    height:150px;
    background-color:#666;
    position:fixed;
    top:0;
    left:0;
    color:#fff;
    background-repeat: no-repeat;
    background-position: center center;
}

img {
  width: 150px;
  display: block;
  margin: 0 auto;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="header_nav"><img src="https://i.picsum.photos/id/244/200/200.jpg?hmac=Q1gdvE6ZPZUX3nXkxvmzuc12eKVZ9XVEmSH3nCJ2OOo"></div>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda