JavaScriptベースの美しいページ遷移アニメーション効果をソースコードで実現 download_javascriptスキル

WBOY
リリース: 2016-05-16 15:35:03
オリジナル
1627 人が閲覧しました

ページの左側にあるメニューをクリックすると、対応するページが読み込まれ、スライド フィルター アニメーションとプログレス バー効果が表示されます。もちろん、ページの読み込みは Ajax 主導で行われ、読み込み移行プロセス全体が非常にスムーズで、非常に優れたユーザー エクスペリエンスを提供します。

HTML

HTML 構造では、.cd-main にはページのメイン コンテンツが含まれ、.cd-side-navigation にはサイド ナビゲーション バーが含まれ、#cd-loading-bar は進行状況バーのアニメーションに使用されます。

<nav class="cd-side-navigation"> 
  <ul> 
    <li> 
      <a href="index.html" class="selected" data-menu="index"> 
        <svg><!-- svg content here --></svg> 
        Intro 
      </a> 
    </li> 
    <li> 
      <!-- ... --> 
    </li> 
    <!-- other list items here --> 
  </ul> 
</nav> <!-- .cd-dashboard --> 
<main class="cd-main"> 
  <section class="cd-section index visible"> 
    <header> 
      <div class="cd-title"> 
        <h2>Animated Page Transition #2</h2> 
        <span>Some text here</span> 
      </div> 
      <a href="#index-content" class="cd-scroll">Scroll Down</a> 
    </header> 
    <div class="cd-content" id="index-content"> 
      <!-- content here --> 
    </div> <!-- .cd-content --> 
  </section> <!-- .cd-section --> 
</main> <!-- .cd-main --> 
<div id="cd-loading-bar" data-scale="1" class="index"></div> <!-- lateral loading bar --> 

ログイン後にコピー

CSS

ページの左側の .cd-side-navigation を修正し、その高さを 100% に設定しました。これにより、右側のメイン コンテンツがスクロールされると、左側のナビゲーション メニューが常に左側のサイドバーを占めるようになります。ナビゲーションメニューが動かない。

.cd-side-navigation { 
 position: fixed; 
 z-index: 3; 
 top: 0; 
 left: 0; 
 height: 100vh; 
 width: 94px; 
 overflow: hidden; 
} 
.cd-side-navigation ul { 
 height: 100%; 
 overflow-y: auto; 
} 
.cd-side-navigation::before { 
 /* background color of the side navigation */ 
 content: ''; 
 position: absolute; 
 top: 0; 
 left: 0; 
 height: 100%; 
 width: calc(100% - 4px); 
 background-color: #131519; 
} 
.cd-side-navigation li { 
 width: calc(100% - 4px); 
} 
.cd-side-navigation a { 
 display: block; 
 position: relative; 
} 
.cd-side-navigation a::after { 
 /* 4px line to the right of the item - visible on hover */ 
 content: ''; 
 position: absolute; 
 top: 0; 
 right: -4px; 
 height: 100%; 
 width: 4px; 
 background-color: #83b0b9; 
 opacity: 0; 
} 
.no-touch .cd-side-navigation a:hover::after { 
 opacity: 1; 
}
ログイン後にコピー

JavaScript

左側のメニューをクリックすると、triggerAnimation() 関数が呼び出され、この関数は読み込み進行状況バー アニメーション関数loadingBarAnimation() をトリガーし、次にページ コンテンツ関数loadNewContent()を読み込みます。

function loadingBarAnimation() { 
  var scaleMax = loadingBar.data('scale'); 
  if( scaleY + 1 < scaleMax) { 
    newScaleValue = scaleY + 1; 
  } 
  // ... 
   
  loadingBar.velocity({ 
    scaleY: newScaleValue 
  }, 100, loadingBarAnimation); 
}
ログイン後にコピー

新しいページが選択されると、新しい要素 .cd-section が作成されて DOM に挿入され、新しい URL コンテンツがload()されます。

function loadNewContent(newSection) { 
  var section = $('<section class="cd-section overflow-hidden '+newSection+'"></section>').appendTo(mainContent); 
   
  //load the new content from the proper html file 
  section.load(newSection+'.html .cd-section > *', function(event){ 
     
    loadingBar.velocity({ 
      scaleY: scaleMax //this is the scaleY value to cover the entire window height (100% loaded) 
    }, 400, function(){ 
       
      section.addClass('visible'); 
 
      var url = newSection+'.html'; 
 
      if(url!=window.location){ 
        //add the new page to the window.history 
        window.history.pushState({path: url},'',url); 
      } 
 
      // ... 
    }); 
  }); 
}
ログイン後にコピー

非同期で読み込まれたページから前のページの閲覧履歴に戻りたい場合は、ブラウザ上で「戻る」をクリックしてください。前のページに戻ると、遷移アニメーション効果もあります。

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!