0

I would like to use variable in css('width', 'topPosition+"px"')method but whether I try 'px' or other ways it is not working.

$("document").ready(function () {
$(window).scroll(function () {
    var topPosition = $('#cialo').scrollTop();
    console.log(topPosition);
    if (topPosition != 0) {
        $("logoBg").css('margin-top', 'topPosition+"px"')
    };
});

How to do it?

1
  • 1
    what is 'logoBg'?? is it any class? Commented Feb 23, 2017 at 9:58

6 Answers 6

3

Please try this: If logoBg is class

$(".logoBg").css('margin-top', topPosition+'px');

If logoBg is id

$("#logoBg").css('margin-top', topPosition+'px');
Sign up to request clarification or add additional context in comments.

1 Comment

what is loogoBg is it class string or id ? have you noticed ?
2

Replace your $("logoBg").css('margin-top', 'topPosition+"px"') line of code with my code below::

Assuming you have class name called "logoBg"

$(".logoBg").css('margin-top', topPosition);

OR

$(".logoBg").css('margin-top', topPosition+'px');

PLEASE NOTE::

If your 'logoBg' is class use it like $('.logoBg') (class selector) or if ID then use it like$('#logoBg') (ID selector)

4 Comments

what is loogoBg is it class string or id ? have you noticed ?
I have asked him a question have you noticed?
Consequently, your answer is wrong then even if you mentioned it in comment, right ?
@rahul_m OK. Thank you.
1

Your topPosition will be string in your code,

$(".logoBg").css('margin-top', topPosition+"px");   
// replace .logoBg with . if class and with # if id of element

Replace this line in your code, it will work.

Comments

1

The quotes are wrong in 'topPosition+"px"', this code should work:

$("document").ready(function () {
    $(window).scroll(function () {
        var topPosition = $('#cialo').scrollTop();
        console.log(topPosition);
        if (topPosition != 0) {
            $("logoBg").css('margin-top', String(topPosition) + 'px');
        };
    });

$("logoBg") i suppose should be $(".logoBg") (if its a class name), because doesn't exist an html tag named logobg.

1 Comment

what is loogoBg is it class string or id ? have you noticed ?
1

mention that "logoBg" is id or class in jquery code . there is no "#" or "."

Comments

1

Change marginTop and topPosition is a var, no need quotes.

$(".logoBg").css('marginTop', topPosition+'px')

1 Comment

what is loogoBg is it class string or id ? have you noticed ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.