页面过渡直到刷新后才开始工作

时间:2016-12-14 15:29:46

标签: javascript jquery

我有两页index.html和about.html。我想在两个页面之间进行特殊的转换,所以我按照教程制作了这个标记。它有效,但有一个问题.. 每当我导航到about.html时,其内容都是空的,但是当我刷新时,内容是可见的..

然而,index.html保持完美...... 请问如何实现此页面转换,并保持内容和其他所有细节的完整性。 注意:我在服务器上运行它,所以这不是服务器问题。

JPanel
jQuery(document).ready(function(event){
  var isAnimating = false,
    newLocation = '';
    firstLoad = false;
  
  //trigger smooth transition from the actual page to the new one 
  $('#barba-wrapper').on('click', '[data-type="page-transition"]', function(event){
    event.preventDefault();
    //detect which page has been selected
    var newPage = $(this).attr('href');
    //if the page is not already being animated - trigger animation
    if( !isAnimating ) changePage(newPage, true);
    firstLoad = true;
  });

  //detect the 'popstate' event - e.g. user clicking the back button
  $(window).on('popstate', function() {
  	if( firstLoad ) {
      /*
      Safari emits a popstate event on page load - check if firstLoad is true before animating
      if it's false - the page has just been loaded 
      */
      var newPageArray = location.pathname.split('/'),
        //this is the url of the page to be loaded 
        newPage = newPageArray[newPageArray.length - 1];

      if( !isAnimating  &&  newLocation != newPage ) changePage(newPage, false);
    }
    firstLoad = true;
	});

	function changePage(url, bool) {
    isAnimating = true;
    // trigger page animation
    $('body').addClass('page-is-changing');
    $('.cd-loading-bar').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){
    	loadNewContent(url, bool);
      newLocation = url;
      $('.cd-loading-bar').off('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend');
    });
    //if browser doesn't support CSS transitions
    if( !transitionsSupported() ) {
      loadNewContent(url, bool);
      newLocation = url;
    }
	}

	function loadNewContent(url, bool) {
		url = ('' == url) ? 'index.html' : url;
  	var newSection = 'cd-'+url.replace('.html', '');
  	var section = $('<div class="barba-container '+newSection+'"></div>');
  		
  	section.load(url+' .barba-container > *', function(event){
      // load new content and replace <main> content with the new one
      $('#barba-wrapper').html(section);
      //if browser doesn't support CSS transitions - dont wait for the end of transitions
      var delay = ( transitionsSupported() ) ? 1200 : 0;
      setTimeout(function(){
        //wait for the end of the transition on the loading bar before revealing the new content
        ( section.hasClass('cd-about') ) ? $('body').addClass('cd-about') : $('body').removeClass('cd-about');
        $('body').removeClass('page-is-changing');
        $('.cd-loading-bar').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){
          isAnimating = false;
          $('.cd-loading-bar').off('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend');
        });

        if( !transitionsSupported() ) isAnimating = false;
      }, delay);
      
      if(url!=window.location && bool){
        //add the new page to the window.history
        //if the new page was triggered by a 'popstate' event, don't add it
        window.history.pushState({path: url},'',url);
      }
		});
  }

  function transitionsSupported() {
    return $('html').hasClass('csstransitions');
  }
});
/* -------------------------------- 

Primary style

-------------------------------- */
*, *::after, *::before {
  box-sizing: border-box;
}

html {
  font-size: 62.5%;
}

body {
  font-size: 1.6rem;
  font-family: "Open Sans", sans-serif;
  color: #4089a6;
  background-color: #ffffff;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

a {
  color: #283040;
  text-decoration: none;
}

h1 {
  color: #ffffff;
  margin-bottom: 1em;
  font-size: 2.2rem;
}
@media only screen and (min-width: 768px) {
  h1 {
    font-size: 4.4rem;
    font-weight: 300;
  }
}

/* -------------------------------- 

Patterns - reusable parts of our design

-------------------------------- */

.cd-about  {
  background-color: #4089a6;
}

/* -------------------------------- 

#barba-wrapper Components 

-------------------------------- */
body::after, body::before {
  /* these are the 2 half blocks which cover the content once the animation is triggered */
  content: '';
  height: 50vh;
  width: 100%;
  position: fixed;
  left: 0;
  background-color: #ffffff;
  z-index: 1;
  /* Force Hardware Acceleration */
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  -webkit-transform: translateZ(0);
  -moz-transform: translateZ(0);
  -ms-transform: translateZ(0);
  -o-transform: translateZ(0);
  transform: translateZ(0);
  -webkit-transition: -webkit-transform 0.4s 0.4s;
  -moz-transition: -moz-transform 0.4s 0.4s;
  transition: transform 0.4s 0.4s;
}
body::before {
  top: 0;
  -webkit-transform: translateY(-100%);
  -moz-transform: translateY(-100%);
  -ms-transform: translateY(-100%);
  -o-transform: translateY(-100%);
  transform: translateY(-100%);
}
body::after {
  bottom: 0;
  -webkit-transform: translateY(100%);
  -moz-transform: translateY(100%);
  -ms-transform: translateY(100%);
  -o-transform: translateY(100%);
  transform: translateY(100%);
}
body.page-is-changing::after, body.page-is-changing::before {
  -webkit-transform: translateY(0);
  -moz-transform: translateY(0);
  -ms-transform: translateY(0);
  -o-transform: translateY(0);
  transform: translateY(0);
  -webkit-transition: -webkit-transform 0.4s 0s;
  -moz-transition: -moz-transform 0.4s 0s;
  transition: transform 0.4s 0s;
}

#barba-wrapper {
  height: 100vh;
  padding: 10px;
  text-align: center;
}
#barba-wrapper .cd-main-content {
  position: relative;
  height: calc(100vh - 20px);
}
#barba-wrapper .cd-main-content > div {
  height: 100%;
  overflow: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
#barba-wrapper .cd-main-content::after, #barba-wrapper .cd-main-content::before {
  /* these are the 2 gradient overlay at the top and bottom of the .cd-#barba-wrapper-content - to indicate that you can scroll */
  content: '';
  position: absolute;
  left: 0;
  height: 50px;
  width: 100%;
}
#barba-wrapper .cd-main-content::before {
  top: 0;
}
#barba-wrapper .cd-main-content::after {
  bottom: 0;
}
#barba-wrapper .cd-main-content.cd-index {
  /* .cd-#barba-wrapper-content basic style - index page */
  background-color: #4089a6;
  /* vertically center its content */
  display: table;
  width: 100%;
}
#barba-wrapper .cd-main-content.cd-index > div {
  /* vertically center the content inside the .cd-index */
  display: table-cell;
  vertical-align: middle;
}
#barba-wrapper .cd-main-content.cd-index::after {
  background-color: rgba(64, 137, 166, 0);
  background-image: -webkit-linear-gradient(bottom, #4089a6, rgba(64, 137, 166, 0));
  background-image: linear-gradient(to top,#4089a6, rgba(64, 137, 166, 0));
}
#barba-wrapper .cd-main-content.cd-index::before {
  background-color: rgba(64, 137, 166, 0);
  background-image: -webkit-linear-gradient(top, #4089a6, rgba(64, 137, 166, 0));
  background-image: linear-gradient(to bottom,#4089a6, rgba(64, 137, 166, 0));
}
#barba-wrapper .cd-main-content.cd-about {
  /* .cd-#barba-wrapper-content basic style - about page */
  background-color: #283040;
}
#barba-wrapper .cd-main-content.cd-about > div {
  padding-top: 50px;
}
#barba-wrapper .cd-main-content.cd-about::after {
  background-color: rgba(40, 48, 64, 0);
  background-image: -webkit-linear-gradient(bottom, #283040, rgba(40, 48, 64, 0));
  background-image: linear-gradient(to top,#283040, rgba(40, 48, 64, 0));
}
#barba-wrapper .cd-main-content.cd-about::before {
  background-color: rgba(40, 48, 64, 0);
  background-image: -webkit-linear-gradient(top, #283040, rgba(40, 48, 64, 0));
  background-image: linear-gradient(to bottom,#283040, rgba(40, 48, 64, 0));
}
#barba-wrapper p {
  width: 90%;
  max-width: 768px;
  margin: 3em auto;
  font-size: 1.4rem;
  line-height: 1.6;
  color: #535966;
  text-align: left;
}
@media only screen and (min-width: 768px) {
  #barba-wrapper {
    padding: 20px;
  }
  #barba-wrapper .cd-main-content {
    height: calc(100vh - 40px);
  }
  #barba-wrapper.cd-index > div {
    padding-top: 200px;
  }
  #barba-wrapper.cd-index > div {
    padding-top: 50px;
  }
  #barba-wrapper p {
    font-size: 1.8rem;
    line-height: 2;
  }
}

.cd-cover-layer {
  /* layer that covers the content when the animation is triggered */
  position: fixed;
  left: 50%;
  top: 50%;
  bottom: auto;
  right: auto;
  -webkit-transform: translateX(-50%) translateY(-50%);
  -moz-transform: translateX(-50%) translateY(-50%);
  -ms-transform: translateX(-50%) translateY(-50%);
  -o-transform: translateX(-50%) translateY(-50%);
  transform: translateX(-50%) translateY(-50%);
  height: 100%;
  width: 100%;
  opacity: 0;
  visibility: hidden;
  background-color: #4089a6;
  -webkit-transition: opacity 0.4s 0.4s, visibility 0s 0.8s;
  -moz-transition: opacity 0.4s 0.4s, visibility 0s 0.8s;
  transition: opacity 0.4s 0.4s, visibility 0s 0.8s;
}
.cd-about .cd-cover-layer {
  background-color: #283040;
}
.page-is-changing .cd-cover-layer {
  opacity: 1;
  visibility: visible;
  -webkit-transition: opacity 0.3s 0s, visibility 0s 0s;
  -moz-transition: opacity 0.3s 0s, visibility 0s 0s;
  transition: opacity 0.3s 0s, visibility 0s 0s;
}
@media only screen and (min-width: 768px) {
  .cd-cover-layer {
    height: calc(100% - 40px);
    width: calc(100% - 40px);
  }
}

.cd-loading-bar {
  /* this is the loding bar - visible while switching from one page to the following one */
  position: fixed;
  z-index: 2;
  left: 50%;
  top: 50%;
  height: 2px;
  width: 90%;
  background-color: #4089a6;
  visibility: hidden;
  -webkit-transition: visibility 0s 0.4s, -webkit-transform 0.4s 0s ease-in;
  -moz-transition: visibility 0s 0.4s, -moz-transform 0.4s 0s ease-in;
  transition: visibility 0s 0.4s, transform 0.4s 0s ease-in;
  /* Force Hardware Acceleration */
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  -webkit-transform: translateZ(0);
  -moz-transform: translateZ(0);
  -ms-transform: translateZ(0);
  -o-transform: translateZ(0);
  transform: translateZ(0);
  -webkit-transform: translateX(-50%) translateY(-50%);
  -moz-transform: translateX(-50%) translateY(-50%);
  -ms-transform: translateX(-50%) translateY(-50%);
  -o-transform: translateX(-50%) translateY(-50%);
  transform: translateX(-50%) translateY(-50%);
}
.cd-about .cd-loading-bar {
  background-color: #283040;
}
.cd-about .cd-loading-bar::before {
  background-color: #4089a6;
}
.cd-loading-bar::before {
  /* this is the progress bar inside the loading bar */
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  width: 100%;
  background-color: #283040;
  /* Force Hardware Acceleration */
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
  -webkit-transform: translateZ(0);
  -moz-transform: translateZ(0);
  -ms-transform: translateZ(0);
  -o-transform: translateZ(0);
  transform: translateZ(0);
  -webkit-transform: scaleX(0);
  -moz-transform: scaleX(0);
  -ms-transform: scaleX(0);
  -o-transform: scaleX(0);
  transform: scaleX(0);
  -webkit-transform-origin: left center;
  -moz-transform-origin: left center;
  -ms-transform-origin: left center;
  -o-transform-origin: left center;
  transform-origin: left center;
}
.page-is-changing .cd-loading-bar {
  visibility: visible;
  -webkit-transform: translateX(-50%) translateY(-50%) scaleX(0.3);
  -moz-transform: translateX(-50%) translateY(-50%) scaleX(0.3);
  -ms-transform: translateX(-50%) translateY(-50%) scaleX(0.3);
  -o-transform: translateX(-50%) translateY(-50%) scaleX(0.3);
  transform: translateX(-50%) translateY(-50%) scaleX(0.3);
  -webkit-transition: visibility 0s 0.3s, -webkit-transform 0.4s 0.4s;
  -moz-transition: visibility 0s 0.3s, -moz-transform 0.4s 0.4s;
  transition: visibility 0s 0.3s, transform 0.4s 0.4s;
}
.page-is-changing .cd-loading-bar::before {
  -webkit-transform: scaleX(1);
  -moz-transform: scaleX(1);
  -ms-transform: scaleX(1);
  -o-transform: scaleX(1);
  transform: scaleX(1);
  -webkit-transition: -webkit-transform 0.8s 0.8s ease-in;
  -moz-transition: -moz-transform 0.8s 0.8s ease-in;
  transition: transform 0.8s 0.8s ease-in;
}
@media only screen and (min-width: 768px) {
  .cd-loading-bar {
    width: calc(100% - 40px);
  }
}

0 个答案:

没有答案