jQuery는 앵커 포인트 동적 변위를 작동합니다.

php中世界最好的语言
풀어 주다: 2018-04-19 15:56:08
원래의
2138명이 탐색했습니다.

이번에는 jQuery의 앵커포인트 연산의 동적 변위에 대해 다루겠습니다. jQuery의 동적 앵커포인트 연산의 주의사항은 무엇인가요? 실제 사례를 살펴보겠습니다.

페이지에서 부드러운 스크롤 효과를 얻으려면 jQuery animate() 메서드를 사용하세요

$('html, body').animate({
scrollTop: $(hash).offset().top
}, 800, function(){
window.location.hash = hash;
});
로그인 후 복사

간단한 예제 코드:

<!DOCTYPE html>
<html>
<head>
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
 // Add smooth scrolling to all links
 $("a").on('click', function(event) {
  
  // Make sure this.hash has a value before overriding default behavior
  if (this.hash !== "") {
   // Prevent default anchor click behavior
   event.preventDefault();
  
   // Store hash
   var hash = this.hash;
  
   // Using jQuery's animate() method to add smooth page scroll
   // The optional number (800) specifies the number of milliseconds it takes to scroll to the specified area
   $('html, body').animate({
    scrollTop: $(hash).offset().top
   }, 800, function(){
   
    // Add hash (#) to URL when done scrolling (default click behavior)
    window.location.hash = hash;
   });
  } // End if
 });
});
</script>
 <style>
body, html, .main {
  height: 100%;
}
  
section {
  min-height: 100%;
}
</style>
</head>
<body>
<ahref="#section2"rel="external nofollow"style="
   font-size: 30px;
   font-weight: bold;
   text-align: center;
">点击此处平滑滚动到第二部分</a>
<pclass="main">
 <section></section>
</p>
<pclass="main"id="section2">
 <sectionstyle="
   background-color: #03c03c;
   color: #fff;
   font-size: 30px;
   text-align: center">
   SECTION 2
 </section>
</p>
</body>
</html>
로그인 후 복사

                                                                            ~ 

추천 도서:

jQuery는 맨 위로 돌아가기 기능을 구현합니다


jQuery는 마우스 휠 조작 이미지를 확대/축소합니다


위 내용은 jQuery는 앵커 포인트 동적 변위를 작동합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!