javascript - 导致移动导航无法点击的页面转换

标签 javascript jquery html css jquery-mobile

这里有两个主要的 jQuery 元素。一个是用另一个页面替换页面内容的页面转换,另一个是智能导航(移动设备上的移动导航和页面滚动)。

过渡和导航在桌面设备上完美运行。问题是当你跳转到移动导航时,它变得不可点击。

它只是在完成页面转换后才无法点击,所以我确信它们在某种程度上存在冲突。

直播页面在这里https://nathanworking.github.io/page-transition-smart-nav/

下面是两个脚本。 注意:我为组合这些部分所做的工作很简单,在我的 .js 文件中第一个是“动画页面转换”,然后是“智能导航”第二个。

如果我只是将这两个错误地结合在一起,有人想过如何合并它们吗?

感谢您的帮助!

动画页面转换

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

    ----- Animated Page transitions ------

--------------------------------------*/
jQuery(document).ready(function(event){
  var isAnimating = false,
    newLocation = '';
    firstLoad = false;

  //trigger smooth transition from the actual page to the new one
    $('body').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="cd-main-content '+newSection+'"></div>');

    section.load(url+' .cd-main-content > *', function(event){
      // load new content and replace <main> content with the new one
      $('main').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');
  }
});

智能导航

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

    ----- Smart Navigation ------

--------------------------------------*/
jQuery(document).ready(function($){
    // browser window scroll (in pixels) after which the "menu" link is shown
    var offset = 300;

    var navigationContainer = $('#cd-nav'),
        mainNavigation = navigationContainer.find('#cd-main-nav ul');

    //hide or show the "menu" link
    checkMenu();
    $(window).scroll(function(){
        checkMenu();
    });

    //open or close the menu clicking on the bottom "menu" link
    $('.cd-nav-trigger').on('click', function(){
        $(this).toggleClass('menu-is-open');
        //we need to remove the transitionEnd event handler (we add it when scolling up with the menu open)
        mainNavigation.off('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend').toggleClass('is-visible');

    });

    function checkMenu() {
        if( $(window).scrollTop() > offset && !navigationContainer.hasClass('is-fixed')) {
            navigationContainer.addClass('is-fixed').find('.cd-nav-trigger').one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(){
                mainNavigation.addClass('has-transitions');
            });
        } else if ($(window).scrollTop() <= offset) {
            //check if the menu is open when scrolling up
            if( mainNavigation.hasClass('is-visible')  && !$('html').hasClass('no-csstransitions') ) {
                //close the menu with animation
                mainNavigation.addClass('is-hidden').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){
                    //wait for the menu to be closed and do the rest
                    mainNavigation.removeClass('is-visible is-hidden has-transitions');
                    navigationContainer.removeClass('is-fixed');
                    $('.cd-nav-trigger').removeClass('menu-is-open');
                });
            //check if the menu is open when scrolling up - fallback if transitions are not supported
            } else if( mainNavigation.hasClass('is-visible')  && $('html').hasClass('no-csstransitions') ) {
                    mainNavigation.removeClass('is-visible has-transitions');
                    navigationContainer.removeClass('is-fixed');
                    $('.cd-nav-trigger').removeClass('menu-is-open');
            //scrolling up with menu closed
            } else {
                navigationContainer.removeClass('is-fixed');
                mainNavigation.removeClass('has-transitions');
            }
        }
    }
});

来自 ryancdotnet 的评论 - 这是正确的 jQuery:

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

    ----- Animated Page transitions ------

--------------------------------------*/
jQuery(document).ready(function(event){
  var isAnimating = false,
    newLocation = '';
    firstLoad = false;

  //trigger smooth transition from the actual page to the new one
    $('body').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="cd-main-content '+newSection+'"></div>');

    section.load(url+' .cd-main-content > *', function(event){
      // load new content and replace <main> content with the new one
      $('main').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');
        });

//  ----- Animated Page transitions ------
                //open or close the menu clicking on the bottom "menu" link
                jQuery(document).ready(function($){
                    // browser window scroll (in pixels) after which the "menu" link is shown
                    var offset = 300;

                    var navigationContainer = $('#cd-nav'),
                        mainNavigation = navigationContainer.find('#cd-main-nav ul');

                    //hide or show the "menu" link
                    checkMenu();
                    $(window).scroll(function(){
                        checkMenu();
                    });

                    //open or close the menu clicking on the bottom "menu" link
                    $('.cd-nav-trigger').on('click', function(){
                        $(this).toggleClass('menu-is-open');
                        //we need to remove the transitionEnd event handler (we add it when scolling up with the menu open)
                        mainNavigation.off('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend').toggleClass('is-visible');

                    });

                    function checkMenu() {
                        if( $(window).scrollTop() > offset && !navigationContainer.hasClass('is-fixed')) {
                            navigationContainer.addClass('is-fixed').find('.cd-nav-trigger').one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(){
                                mainNavigation.addClass('has-transitions');
                            });
                        } else if ($(window).scrollTop() <= offset) {
                            //check if the menu is open when scrolling up
                            if( mainNavigation.hasClass('is-visible')  && !$('html').hasClass('no-csstransitions') ) {
                                //close the menu with animation
                                mainNavigation.addClass('is-hidden').one('webkitTransitionEnd otransitionend oTransitionEnd msTransitionEnd transitionend', function(){
                                    //wait for the menu to be closed and do the rest
                                    mainNavigation.removeClass('is-visible is-hidden has-transitions');
                                    navigationContainer.removeClass('is-fixed');
                                    $('.cd-nav-trigger').removeClass('menu-is-open');
                                });
                            //check if the menu is open when scrolling up - fallback if transitions are not supported
                            } else if( mainNavigation.hasClass('is-visible')  && $('html').hasClass('no-csstransitions') ) {
                                    mainNavigation.removeClass('is-visible has-transitions');
                                    navigationContainer.removeClass('is-fixed');
                                    $('.cd-nav-trigger').removeClass('menu-is-open');
                            //scrolling up with menu closed
                            } else {
                                navigationContainer.removeClass('is-fixed');
                                mainNavigation.removeClass('has-transitions');
                            }
                        }
                    }
                });
//  ----- Animated Page transitions ------

        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');
  }
});

最佳答案

第一次点击后移动导航似乎总是失败。调试后,您似乎正在删除菜单并重新添加它,但之后没有连接点击事件。

具体来说,这部分似乎没有再次被解雇:

$('.cd-nav-trigger').on('点击',......

关于javascript - 导致移动导航无法点击的页面转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39517582/

相关文章:

javascript - ES6 - 如何在绑定(bind) `this` 类后访问 `this` 元素?

html - 有什么方法可以在 <amp-img> 中使用 onerror() 属性

javascript - 针对元素的进出动画

javascript - jQuery FullCalendar 中未显示的事件

jquery - 使用 "toggle"函数的简单 jQuery 测试,未按预期运行

javascript - 使用 innerHTML 创建具有内联样式的整个 DOM 对象,安全吗?

javascript - addClass 到 wrapAll 结果

javascript - 如何识别onbeforeunload是点击关闭按钮引起的

javascript - 在哪里以及如何使用数据段代码?

javascript - 更新到 Bootstrap 3.2.0 - 导航标题对齐之后变得不对齐