首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >smoothstate.js - back按钮有时不起作用?

smoothstate.js - back按钮有时不起作用?
EN

Stack Overflow用户
提问于 2018-02-27 17:54:58
回答 1查看 612关注 0票数 2

我在我的网站上使用插件smoothstate.js。由于某些原因,当我使用“后退”和“前进”按钮浏览页面时,“后退”按钮就会停止工作。

URL更改了,但内容保持不变?

我已经检查了控制台中显示的错误:

代码语言:javascript
复制
TypeError: Cannot read property 'state' of undefined

有人知道为什么会这样吗?就像我说的,大多数情况下,它可以正常工作,但突然之间就不能工作了。

我使用的代码是这样的:

代码语言:javascript
复制
$(function(){
  'use strict';
  var options = {
    prefetch: true,
    debug:true,
    cacheLength: 0,
    repeatDelay: 500,


    onStart: {
      duration: 0, // Duration of our animation
      render: function ($container) {
        // Add your CSS animation reversing class
        $container.addClass('is-exiting');

        // Restart your animation
        smoothState.restartCSSAnimations();
      }
    },

    onProgress: {
    // How long this animation takes
    duration: 0,
    // A function that dictates the animations that take place
    render: function ($container) {
                    $container.addClass('is-loading');

                    $('#progressBar').append('<div id="bar"></div>');   
                    var progress = '100%';

                    $('#bar').animate({
                        width: progress
                    }, 400);
    }
    },

    onReady: {
      duration: 0,
      render: function ($container, $newContent) {
        $container.removeClass('is-loading is-exiting');
        // Inject the new content
        $container.html($newContent);
      },
    },

    onAfter: function() {
            navbarAnimate();
            closeMenu();
            ImageSliders();
            initPhotoSwipeFromDOM('.gallery');
            ImageOverlay(); 
            window.parsePinBtns();
            backToTop();

    }
  },


  smoothState = $('#main').smoothState(options).data('smoothState');
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-26 23:23:51

我也遇到了这个问题,在页面没有完全加载的情况下,后退和前进按钮点击得太快就会发生。对于我来说,一个棘手的解决方案是如果page.cachepage.href是未定义的,就重新加载页面。

代码语言:javascript
复制
/** Handles the popstate event, like when the user hits 'back' */
onPopState = function(e) {
  if(e.state !== null || typeof e.state !== undefined) {
    var url = window.location.href;
    var $page = $('#' + e.state.id);	
    var page = $page.data('smoothState');
	
	if (typeof(page.cache[page.href]) !== 'undefined') {
		var diffUrl = (page.href !== url && !utility.isHash(url, page.href));
		var diffState = (e.state !== page.cache[page.href].state); 

		if(diffUrl || diffState) {
		  if (diffState) {
			page.clear(page.href);
		  }
		  page.load(url, false);
		}
	}
	else {
		//reload the page if page.cache[page.href] is undefined
		location.reload();
	}
    
  }
},

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49005788

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档