Mootools放大时出现问题
我正在开发的网站中广泛使用 Mootools。但最近我注意到一个问题,当我放大(使用浏览器放大)网站时,动画速度会减慢很多。这个问题的可能原因是什么?或者这个问题是Mootools本身继承的。 Chrome 6.0.472 和 Firefox 3.6.8 中都会发生这种情况。
谢谢, 镍丁
I am using Mootools extensively for a site which I am developing. But recently I noticed a problem that the animations slow down alot when I zoom (using the browsers Zoom In) in into the site. What could be a possible reason for this problem ? Or is this problem inherit in Mootools itself. This happens in Chrome 6.0.472 as well as Firefox 3.6.8.
Thanks,
Nitin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于速度优化,这里有很多问题。
让我们看一下这个似乎变慢的鼠标悬停代码:
显然这会正常工作,但这是非常错误的,我不知道从哪里开始。
这个想法是抽象和设置重复性任务,以便一次性完成它们。
逐行考虑上面的代码:
this.childNodes.item(1).style.left="0px";
这对于 mootools 应用程序来说是错误的,它需要是
this .getFirst().setStyle("left", 0);
this.getFirst() 是一个查找,它应该被缓存 - 尽管这不是一个慢的过程。
然后是糟糕的部分。
您选择所有子 div 3 次,选择所有 span 两次,其中 NO SELECTION 应该适用。 非常昂贵
你在每次鼠标悬停事件没有变化的情况下重置Fx.morph选项(尽管你似乎对mouseenter和mouseleave有不同的持续时间 - 这是昂贵的)
考虑这个代码:
它将节省对事件进行大量处理。
同样,有很多地方您并没有真正使用 mootools api。
document.getElementById(curr).style.cursor="pointer";
$(this).removeEvents
->不需要$,这不是jquery。document.getElementById("lightbox").style.visibility="hidden";
m=setTimeout('gallery()',5000);
-->使用 mootoolsvar time = (function() { ... }).delay(5000);
,不要将字符串与 setTimeout/interval 一起使用,因为它会强制 eval 和回流,但要使用纯匿名函数等等等,
你确实可以花一天的时间来重构所有这些并使其变得“美好”,但这是值得的。
另外,了解链接,
这会调用选择器 3 次。相反,您可以:
$("ribbon").set("morph", {duration: 500}).morph({top:-10}).addEvents()
- mootools 元素方法倾向于返回原始元素因此您可以获取最后一个函数的响应并对其应用更多内容。1 的可读性更好,2 的执行速度更快。
还。你有太多的全局变量,这使得你的作用域链查找更加昂贵,这将影响许多调用和位置。尝试正确命名空间,如果您需要从函数和闭包访问真正的全局变量,请使用 window.varname 等。
这里的另一个可能的改进是使用事件委托(事件冒泡将导致事件将 dom 树激发到父母,mootools 有一个 api 来处理它,因此您可以向父元素添加单个事件,而不必将 nnn 事件附加到所有子元素) - 查找它。
PS,请不要以错误的方式理解这一点 - 这并不是要贬低您的工作,这只是一些建设性的(我希望)建议,可以帮助您将其提升到一个新的水平。祝你好运:)
many things are wrong here with regards to speed optimisations.
lets take a look at this mouseover code that seems to slow down:
obviously this will work as it does but it's so very wrong i don't know where to begin.
the idea is to abstract and setup the repetitive tasks so they are done as a one off.
consider line by line the code above:
this.childNodes.item(1).style.left="0px";
this is wrong for a mootools app anyway, it would need to be
this.getFirst().setStyle("left", 0);
the
this.getFirst()
is a lookup, it should be cached - although that's not a slow one.then comes the bad part.
you select all child divs 3 times and all spans twice, where NO SELECTION should be applicable. VERY EXPENSIVE
you reset the Fx.morph options every mouseover event where there are no changes (although you seem to have a different duration for mouseenter and mouseleave - this is expensive)
consider this code:
it will save a lot of processing on the events.
similarly, there are plenty of places where you are not really using the mootools api.
document.getElementById(curr).style.cursor="pointer";
$(this).removeEvents
-> no need for $, this is not jquery.document.getElementById("lightbox").style.visibility="hidden";
m=setTimeout('gallery()',5000);
--> use the mootoolsvar timer = (function() { ... }).delay(5000);
, don't use strings with setTimeout/interval as it forces eval and reflows but pure anon functionsetc etc.
you really can make a day out of refactoring all this and making it 'nice' but it's going to be worth it.
also, learn about chaining
this is calling up a selector 3 times. instead you can:
var ribbon = $("ribbon"); ribbon.set...
$("ribbon").set("morph", {duration: 500}).morph({top:-10}).addEvents()
- mootools element methods tend to return the original element so you can take the response of the last function and apply more to it.1 is better for readibility, 2 is faster to do.
also. you have way too many global variables which makes your scope chain lookups more expensive, this will affect many call ups and places. try to namespace properly, if you need to access real global vars from functions and closures, use window.varname etc etc.
Another possible improvement here would be the use of event delegation (event bubbling will cause events to fire up the dom tree to the parents, mootools has an api to deal with it so you can add singular events to parent elements and not have to attach nnn events to all children) - look it up.
P.S. please don't take this in the wrong way - it's not meant to rubbish your work and it's just some constructive (i hope) advice that can help you bring it to the next level. good luck :)
我没有在 MooTools 或任何其他库中看到任何特定代码来检查浏览器在动画期间是否缩放,因此我认为动画速度会减慢,因为浏览器使用更多 CPU 来计算缩放过程。
I haven't seen any specific code in MooTools or any other library that checks if browser is zooming during animation, so I think that animation slows down, since browser using more CPU for computing zooming process.