Javascript 如何使这个jQuery驱动的页面成为snappier?
我有一个联系脚本。它使用jQuery处理ajax请求和动画 我还将其与hashchange插件一起使用,以修复back按钮。慢的部分就在那里 完成“翻转”动画后,形状将慢慢淡出。浏览器似乎会阻塞一秒钟。我正在努力使它快速(无阻塞) 下面是负责处理哈希更改事件的函数:Javascript 如何使这个jQuery驱动的页面成为snappier?,javascript,jquery,optimization,Javascript,Jquery,Optimization,我有一个联系脚本。它使用jQuery处理ajax请求和动画 我还将其与hashchange插件一起使用,以修复back按钮。慢的部分就在那里 完成“翻转”动画后,形状将慢慢淡出。浏览器似乎会阻塞一秒钟。我正在努力使它快速(无阻塞) 下面是负责处理哈希更改事件的函数: handleHashChange : function () { // Get the name of the object and cache it var self = this, // Get th
handleHashChange : function () {
// Get the name of the object and cache it
var self = this,
// Get the current hash
hash = window.location.hash,
// Cache the form height var
formHeight = '';
// If the hash is #send or #error, don't do anything
if (hash === "#sent" || hash === "#error") {
return;
}
// Change the page title back to the default
if(self.documentTitle && self.documentTitle != undefined) {
document.title = self.documentTitle;
}
// Reset all inputs in the form
self.inputs.val('').removeAttr('checked').removeAttr('selected');
// Get the height of the form
formHeight = self.getHeight(self.form);
// Show the transition
self.showTransition(self.response, formHeight, function() {
// Show the form
self.form.fadeIn('fast', function() {
// Show the button
self.button[0].style.display = 'block';
// Focus the first input
self.inputs[0].focus();
})
})
}
整个代码可以从下面的链接中看到,并有完整的文档记录:
你可以看到我使用了很多在互联网上找到的技巧来优化这个脚本,除了使用javascript的原生“for”代替“$.each()”,但这并不是什么大问题
如果有人想看到速度缓慢,请尝试从下面的链接发送一条空消息(验证被禁用),然后单击浏览器中的“上一步”按钮:
(注意:它不是用英语写的,但我猜它很容易解释)
那么,我怎样才能让它更活泼呢 我认为这已经相当快了,但我注意到您的代码中有一些东西 这个“if”语句有点冗余
if(self.documentTitle && self.documentTitle != undefined) {
document.title = self.documentTitle;
}
如果“self.documentTitle”的值为“undefined”,那么对它的调用将返回“false”,因此不需要第二个“self.documentTitle!=undefined”
您可以只使用以下内容:
if(self.documentTitle){
document.title = self.documentTitle;
}
请记住,值false、null、undefined、0和空字符串的计算结果都是一个假布尔值。我没有答案,但您已经创建了一个漂亮的GUI!我觉得它已经很快了!!我同意,这里看起来很不错。你用的是什么浏览器?谢谢大家,@cambraca:我用FF、IE、Opera、safari和chrome进行了测试。FF的反应最慢。在FF上看起来也一样好。。在IE9上,它运行良好,但存在一些样式问题,您可能想看看(基本上,除了第一个字段外,您无法看到您在所有字段上写的内容)