简体   繁体   English

如何处理jQuery中的后退按钮?

[英]How do you handle the back button in jQuery?

I just can't find a satisfying way to handle the Browser's History. 我找不到令人满意的方式来处理浏览器的历史记录。 Sure there are lovely plugins like History.js , which works fine for smaller things. 当然有像History.js这样的可爱插件,适用于较小的东西。 But let me give you an example: 但让我举一个例子:

I have a multi-paged form that asks the user for different things. 我有一个多页面的表单,要求用户提供不同的东西。 After every submit to the next page (actually the same, it just changes the view depending on HTTP-POST variables), there are some animations and changes on the page: 每次提交到下一页(实际上相同,它只是根据HTTP-POST变量更改视图)后,页面上会有一些动画和更改:

  • the main content changes to the next form (with a slideUp/slideDown) 主要内容更改为下一个表单(使用slideUp / slideDown)
  • the progress bar changes it's state with an animation 进度条通过动画更改其状态
  • below this bar, some information about some of the input fades in 下面这条,对一些输入一些信息淡入

So that's pretty basic right now, but I'm already struggling, because all the animations (the last 2 parts) are different on every state (different div, different input-sources) and I have no idea how to make them so generic, that I have not to specify them separatly. 所以这现在很基本,但是我已经很努力了,因为每个状态(不同的div,不同的输入源)的所有动画(最后2个部分)都不相同,而且我不知道如何使它们如此通用,我不必分开指出它们。

I could live with that though, but when a user hits the back button I have to redefine all those animations again (in reverse). 我可以忍受这一点,但是当用户点击后退按钮时,我必须再次重新定义所有这些动画(反过来)。 The content-load sure is no problem, it basically just reloads the file with an ajax-load: 内容加载肯定没有问题,它基本上只是使用ajax-load重新加载文件:

  • I execute a pushstate with the input data as the stateObj on submit 我使用输入数据执行一个pushstate作为提交时的stateObj
  • so I can execute the content-load in the bound 'statechange' event 所以我可以在绑定的'statechange'事件中执行content-load

As said though, the rest drives me insane. 如上所述,其余的让我感到疯狂。

I would highly recommend grabbing and learning Backbone.js. 我强烈建议学习和学习Backbone.js。 It plays great with jQuery and sets up your website in a modified MVC pattern. 它与jQuery配合使用,并以修改后的MVC模式设置您的网站。 It also has a great system for handling URL changes and executing the appropriate code. 它还有一个很好的系统来处理URL更改和执行适当的代码。

As for sliding, if you want to reverse animations, that could be a bit of a pain to keep track of. 至于滑动,如果要反转动画,要跟踪它可能会有些麻烦。 What I did with my app when I was sliding between screens is instead of sometimes sliding up and sometimes sliding down, I would always move the element in the DOM that I was sliding to so that it was after the element I was sliding from. 当我在屏幕之间滑动时,我对应用程序所做的操作不是有时向上滑动有时是向下滑动,而是始终将要滑动的DOM中的元素移动到要从其滑动的元素之后。 That way the slide was always going in the same direction and my animations wouldn't get all goofy. 这样幻灯片总是朝着同一个方向前进,我的动画也不会让所有人都高兴。

You could try JavaScriptMVC , which includes jQuery. 您可以尝试JavaScriptMVC ,其中包括jQuery。 Not sure if it meets your exact requirements but it is JavaScript and MVC... 不确定它是否符合您的确切要求,但它是JavaScript和MVC ...

虽然不是非常严格的MVC(其中有几个用于JavaScript)我认为骨干js是一个很好的框架,它基于MVC的概念,但重新工作以更好地适应JavaScript语言和环境。

Well after a lot of punching around and stuff, I decided to degrade to the simplest and best solution for my problem: Using the basics. 经过大量的努力和解决之后,我决定为我的问题降级为最简单和最好的解决方案:使用基础知识。

Instead of animating everything backwards/forwards when the user hits the back/forward buttons, I just reloaded the whole site with ajax so I can use the HTML/PHP fallback with the stateObj of the selected site. 当用户点击后退/前进按钮时,我只是用ajax重新加载整个站点,而不是向后/向前设置动画,所以我可以使用所选站点的stateObj的HTML / PHP回退。

Problem with History.js though is that it can't distinguish between pushState and back/forward button in the onstatechange-event. History.js的问题是它无法区分onstatechange事件中的pushState和back / forward按钮。 So I degraded from History.js to the standard W3C behaviour with pushstate. 因此,我使用pushstate从History.js降级到标准的W3C行为。 There will be an update with 1.8.0 that allows to distinguish between internal and external statechanges. 将有1.8.0的更新,允许区分内部和外部状态变化。

Thanks for all the answers, but I guess this is the most accurate way of dealing with it, while using MVC alone doesn't solve the problem - why I didn't accept any other answer! 感谢所有答案,但我想这是处理它的最准确方法,而单独使用MVC并不能解决问题 - 为什么我不接受任何其他答案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM