javascript - jQuery 图像 slider - 性能问题

标签 javascript jquery performance

我目前正在构建一个简单的“jQuery Image Slider”,但它并没有像我希望的那样工作。它非常缓慢且 react 迟钝,最后一张图片什么也没做。

网址:http://fusionmedia.dk/jquery/

问题是什么?

提前致谢

最佳答案

指定更快的速度。它默认为较慢的速度。

$('#gallery').delegate('img', 'mouseover', function() {

                $this = $(this);

                for(var i = 0; i <= $this.siblings().size(); i++) {

                    if($this.index() > i) {

                        $this.siblings().eq(i).stop().animate({ left: (i * 50) + 'px' }, 300);

                    } else {

                        $this.siblings().eq(i).stop().animate({ left: ((i * 50) + 500) + 'px' }, 300);

                    }

                }

            });

编辑:

你有 2 个非常糟糕的速度问题。

1:每次他们悬停时,您都在运行一个耗时的循环。

2:你调用 $this.siblings() 的次数太多了。缓存那个。

这是一个如何更好地实现其中一些的示例,我仍然让你在悬停事件中循环,你应该尝试将其移出。

$(function(){

         $('#gallery').find('img').each(function(){

            $this = $(this);
            $this.css('left', $this.index() * 50 + 'px');

         });

         $('#gallery').delegate('img', 'mouseover', function(){

            $this = $(this);
            var $sibs = $this.siblings();
            for (var i = 0; i <= $sibs.size(); i++) {

               if ($this.index() > i) {

                  $sibs.eq(i).stop().animate({
                     left: (i * 50) + 'px'
                  });

               } else {

                  $sibs.eq(i).stop().animate({
                     left: ((i * 50) + 500) + 'px'
                  });
               }
            }
         });
      });

关于javascript - jQuery 图像 slider - 性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4025041/

相关文章:

javascript - 如何在 JSDOC 中记录对象数组

javascript - 引导日期时间选择器更改

c++ - 编译器是否优化 C++ 中静态值的 if 语句

linux - 如何获取服务器的点击数

java - 提高数据库访问性能

javascript - 将代码 html 导入 html5

javascript - 咕噜任务 : start mongod if not running

Javascript 模板系统 - PURE、EJS、jquery 插件?

javascript - 查找元素在 jQuery 对象中的位置

javascript - 替换窗口时 window.open() 的 window.opener 问题