2012-02-19 5 views
5

다른 웹 사이트에서는이 멋진 기능을 보았습니다.하지만 직접 찾을 수없는 것 같습니다.html 앵커 링크를 늦추십시오

원하는 ID로 점프하지 않고 앵커 태그 스크롤을 통해 페이지의 다른 부분에 대한 링크를 만들고 싶습니다. 자바 스크립트 파일이 도움이 될 수도 있고 CSS 전환이 될 수도 있지만 전환 요소가 무엇인지 알지 못합니다. 나는 그것을 필요로하는 사람들을위한 나의 발견으로 답변 해 드리겠습니다 전에

<a href="#bottom">scroll to bottom</a> 

<p id="bottom">bottom</p> 

감사합니다 :) 아무도 이후

+0

임, 아무것도 변경 자바 스크립트 의견을보실해야하는 경우 임베디드 코드와 나는 그것을 바꿀 수 없다 ...하지만 당신은 내가 무슨 뜻인지 확신한다. –

+0

[jQuery.ScrollTo] (http://flesler.blogspot.com/2007/10/jqueryscrollto.html) –

+0

어떻게 사용합니까? onclick 함수로이 id를 넣으십시오. onclck = "jQuery.ScrollTo (bottom)" –

답변

6

정말이 질문에 대답했다.

당신이해야 할 모든

<a href="#bottom">scroll to bottom</a> 

<p id="bottom">bottom</p> 

같은 일반 앵커 링크는 다음이 자바 스크립트 코드를 추가하고 당신은 너무 모바일

<script> 
    $(function() { 

     function filterPath(string) { 
      return string 
      .replace(/^\//,'') 
      .replace(/(index|default).[a-zA-Z]{3,4}$/,'') 
      .replace(/\/$/,''); 
     } 

     var locationPath = filterPath(location.pathname); 
     var scrollElem = scrollableElement('html', 'body'); 

     // Any links with hash tags in them (can't do ^= because of fully qualified URL potential) 
     $('a[href*=#]').each(function() { 

      // Ensure it's a same-page link 
      var thisPath = filterPath(this.pathname) || locationPath; 
      if ( locationPath == thisPath 
       && (location.hostname == this.hostname || !this.hostname) 
       && this.hash.replace(/#/,'')) { 

        // Ensure target exists 
        var $target = $(this.hash), target = this.hash; 
        if (target) { 

         // Find location of target 
         var targetOffset = $target.offset().top; 
         $(this).click(function(event) { 

          // Prevent jump-down 
          event.preventDefault(); 

          // Animate to target 
          $(scrollElem).animate({scrollTop: targetOffset}, 400, function() { 

           // Set hash in URL after animation successful 
           location.hash = target; 

          }); 
         }); 
        } 
      } 

     }); 

     // Use the first element that is "scrollable" (cross-browser fix?) 
     function scrollableElement(els) { 
      for (var i = 0, argLength = arguments.length; i <argLength; i++) { 
       var el = arguments[i], 
       $scrollElement = $(el); 
       if ($scrollElement.scrollTop()> 0) { 
        return el; 
       } else { 
        $scrollElement.scrollTop(1); 
        var isScrollable = $scrollElement.scrollTop()> 0; 
        $scrollElement.scrollTop(0); 
        if (isScrollable) { 
         return el; 
        } 
       } 
      } 
      return []; 
     } 

    }); 
    </script> 
관련 문제