Passing variables in Jquery .css ()

0

I'm having problems with the following code:

//Desktop = D
var posicaoMenuD = $('#desktop-menu').position().top();
$(document).scroll(function(){//Alscutador de scroll da página
    var posicaoScrollD = $(document).scrollTop(); //Obtém o scroll atual (posição)
    if (posicaoMenuD < posicaoScrollD){
        $("#desktop-menu").css('top':$("#desktop-top-menu").height().val());
    }
});

I'm trying to pass variables in the property .css() of jquery , but, without success, I tried declaring the variable before too, but it did not work in console , the message:

scrolls-home.js:6 Uncaught SyntaxError: missing ) after argument list

I would like to know how I can solve this problem, and if possible, an explanation accompanied by the code, because I want to understand what is going on.

    
asked by anonymous 17.01.2017 / 01:50

1 answer

2

Try replacing this line:

$("#desktop-menu").css('top':$("#desktop-top-menu").height().val());

by this:

$("#desktop-menu").css('top', $("#desktop-top-menu").height() + 'px');

According to your error, this is a syntax problem. To set a value, the .css method gets two parameters. The first is the attribute you want to modify, and the second is the value.

When you are going to change values such as top , margin , padding , do not forget to provide the value along with the measure, as if it were to write in the css itself.

EDITED

Edited to insert here the fiddle created by @LeonardoRodrigues with a demo: link

    
17.01.2017 / 02:03