2014-03-30 2 views
-1

나는 문제의 메신저 JQuery와 - 1.11.0.min.js오류가 계속 미정 변수. 나는 어디로 잘못 가고 있니?

$(function(){ 
     //catch all clicks 
     $("a").click(function(){ 
      // check if it has a # 
      if(this.hash){ 
       // get rid of the # sign 
       var hash = this.hash.substr(1); 
       // get position ofd the links name 
       var $toElement = $("a[name="+hash+"]"); 
       var toPosition = $toElement.position().top; 
       // scroll animate to postion 
       $("body,html").animate({ 
        scrollTop : toPosition 
       },2000,"easeOutExpo"); 
       return false; 
      } 
     }); 
    }); 

뿐만 아니라 jQuery를 pluging easing.js의 ANS를 사용하여 간단한 쉽게 스크롤 효과 메신저를 만들려고하고 경우에도 뭐죠 찾을 수가 어차피 내가 난 여전히 같은 오류가 같이 그것을 시도 :

 $(function(){ 
     //catch all clicks 
     $("a").click(function(){ 
      // check if it has a # 
      if(this.hash){ 
       // get rid of the # sign 
       var hash = this.hash.substr(1); 
       // get position ofd the links name 
       var $toElement = $("a[name="+hash+"]"); 
       var $toPosition = $toElement.position().top; 
       // scroll animate to postion 
       $("body,html").animate({ 
        scrollTop : toPosition 
       },2000,"easeOutExpo"); 
       return false; 
      } 
     }); 
    }); 
+5

는'undefined' 일을 시도 할 수 있습니다? 두 번째 예제의 첫번째 차이점에서'toPosition'을 사용하고 두 번째 예제에서'$ toPosition'을 사용하는 유일한 차이점은 무엇입니까? –

+0

이 줄에서 오류가 발생합니다. var toPosition = $ toElement.position(). 위쪽; – Code

+0

this.hash? 그게 뭐야? – Bilal

답변

3

내 생각 엔

var $toElement = $("a[name="+hash+"]"); 

이 모든 요소와 일치하지 않습니다.

이어서, $toElement.position()null이다. 따라서

,

$toElement.position().top; 

에러가 발생.

를 해결하려면, 당신은 예상되는 변수

var position = $("a[name="+hash+"]").position(); 
if(!toPosition) return false; 
var topPosition = position.top; 
관련 문제