2017-02-23 3 views
0

변수를 css ('width', 'topPosition + "px"') 메서드로 사용하고 싶지만 'px'또는 다른 그것이 작동하지 않는 방법.jquery css ('width', '') 메서드에서 변수를 배치하는 방법

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

어떻게 할 수 있습니까?

+1

무엇이 'logoBg'입니까 ?? 어떤 수업인가요? –

답변

1

귀하의 topPosition가

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

코드에서이 줄을 교체 코드에서 문자열입니다, 그것은 작동합니다.

1

는 클래스 이름은 "logoBg"

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

또는

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

라고 한 가정 아래 ::

내 코드와 코드의 당신의 $("logoBg").css('margin-top', 'topPosition+"px"') 라인을 교체하십시오 참고 ::

당신의 'logoBg은'클래스는 다음과 같이 사용할 경우 $('.logoBg')(클래스 선택기) 또는 ID는 다음과 같이 사용할 경우 $('#logoBg')(ID 선택)

+0

loogoBg는 클래스 문자열 또는 ID입니까? 너 알아 차 렸니? – rahulsm

+0

나는 그에게 당신이주의 한 질문을 한 적이 있습니까? –

+0

결과적으로, 당신이 코멘트에 언급 했어도 당신의 대답은 틀렸지, 그렇지? – rahulsm

0

이 시도하십시오 logoBg 클래스

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

logoBg는 ID

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

loogoBg는 클래스 문자열 또는 ID입니까? 너 알아 차 렸니? – rahulsm

+0

@rahul_m 감사합니다. –

0

변경 marginTop와 t 경우 opPosition은 var이므로 따옴표가 필요하지 않습니다.

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

loogoBg는 클래스 문자열 또는 ID입니까? 너 알아 차 렸니? – rahulsm

0

따옴표가 'topPosition+"px"'에 잘못이 코드는 작동합니다 :

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

$("logoBg") 내가 $(".logoBg") (의 경우 클래스 이름)이어야한다 가정 logobg라는 이름의 HTML 태그를 존재하지 않기 때문에.

+0

loogoBg는 클래스 문자열 또는 ID입니까? 너 알아 차 렸니? – rahulsm

0

"logoBg"는 jquery 코드의 id 또는 클래스입니다. "#"또는 "."

관련 문제