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

208
Visualizações
JQuery animate property is not working on :not selector

I want to animate the opacity of all other divs expect one, so I have used css :not property, but it doesn't seem to be working. Because instead of doing what I want, it changed opacity of all div elements including the div which I don't want to be changed... This is my code:

function urlHighlight() {
const queryString = window.location.search
    const urlParams = new URLSearchParams(queryString)
    const product = urlParams.get('h')

    //To check whether Url param and Div with data Id of given Url param exists
    if (product && $(`div[data-id=${product}]`).length == 1){
        $('html, body').animate({
            scrollTop: $(`div[data-id=${product}]`).offset().top - 100
        }, 2000);

        //I want to change opacity of the div elements which don't have the Url param as value of data-id
        $(`body:not(div[data-id=${product}])`).animate({
            opacity: 0.5
        }, 5000, function(){$(this).removeAttr("style")})
    }
}

$(function(){
    $.getJSON("data.json", function(data) {
         //some functions
         urlHighlight()
})
})

What is the problem and how should I fix it?

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

0

The selector should be body *:not(div[data-id=${product}]) like this. all tags inside body that are not the set condition.

function urlHighlight() {
const queryString = window.location.search
    const urlParams = new URLSearchParams(queryString)
    const product = 'test' || urlParams.get('h') //defaulting to test

    //To check whether Url param and Div with data Id of given Url param exists
    if (product && $(`div[data-id=${product}]`).length == 1){
    console.log('in');
        $('html, body').animate({
            scrollTop: $(`div[data-id=${product}]`).offset().top - 100
        }, 2000);

        //I want to change opacity of the div elements which don't have the Url param as value of data-id
        console.log($(`body *:not(div[data-id=${product}])`));
        $(`body *:not(div[data-id=${product}])`).animate({
            opacity: 0.5
        }, 5000, function(){$(this).removeAttr("style")})
    }
}

$(function(){
    // $.getJSON("data.json", function(data) {
         //some functions
         setTimeout(() => {
         urlHighlight()
         }, 1000);
// })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<div data-id="test" style="height:100px;width:100px">test</div>
<div data-id="testy" style="height:100px;width:100px">test2</div>
</body>

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