jQuery闭包和事件函数(mouseover,mouseout,...)

时间:2011-04-05 12:48:33

标签: javascript jquery events closures

我正试图了解如何使用与jQuery事件函数相关的闭包。

我目前的问题是在屏幕上圆形并使它们停止并在鼠标悬停时淡出并淡出并在鼠标移出时重新启动。我将使用图像映射创建一个圆形鼠标悬停敏感区域。虽然动画工作正常,但我很难在鼠标悬停功能上使用闭包,就像我希望的那样。

鉴于此设置:

(function($){
  $.fn.xyz = function( option ) {
    // override defaults with specified option
    option = $.extend( {}, $.fn.xyz.option, option );

  return this.each(function(index, element) {
            // run works fine.
            function run(index) {
                $(".ball:eq(" + index + ")").css({top: 500).startAnimation({ top: -500}, 1000, "linear", (function (i) {
                    return function() {
                        run(i);
                }})(index));
            }

            //1 this version works great but I don't like the .parent().parent() especially as the animation requires 
            // just the ball I hover over gets the opacity assigned
            $("area").mouseover(
                function () {$(this).parent().parent().css('opacity', 0.5);}
            );

            //2 this version makes all balls transparent on page load
            $("area").mouseover(
                (function (activeElement) {
                    $(activeElement).css('opacity', 0.5);
                })(this)
            );

            //3 this version makes all balls transparent on the first mouse over event 
            $("area").mouseover(
                (function (activeElement) {
                    return function() {
                        $(activeElement).css('opacity', 0.5);
                    }
                })(this)
            );

            //4 also this version affecs all balls and not just the one that is mouse overed
            var activeBall = $(this);
            $("area").mouseover(function () {
                $(activeBall).css('opacity', 0.5);
            }).mouseout(function () {
                $(activeBall).css('opacity', 1);
            });

            run(index);
        });
    },

    $.fn.xyz.option = {};
})(jQuery);

为什么版本2,3和4定位所有元素而不仅仅是主动盘旋的元素。我如何利用闭包来避免使用索引或类似的解决方法?

非常感谢!

1 个答案:

答案 0 :(得分:1)

你使它成为一个自我调用匿名函数。基本上,使用jQuery对象自动调用它。你还将函数包装在函数中......这是我没有得到的。这应该有效:

(function($){
  $.fn.xyz = function( option ) {
    // override defaults with specified option
    option = $.extend( {}, $.fn.xyz.option, option );

  return this.each(function(index, element) {
            // run works fine.
            function run(index) {
                $(".ball:eq(" + index + ")").css({top: 500).startAnimation({ top: -500}, 1000, "linear", (function (i) {
                    return function() {
                        run(i);
                }})(index));
            }

            //1 this version works great but I don't like the .parent().parent() especially as the animation requires 
            // just the ball I hover over gets the opacity assigned
            $("area").mouseover(
                function () {$(this).parent().parent().css('opacity', 0.5);}
            );

            //2 this version makes all balls transparent on page load
            $("area").mouseover(
                (function (activeElement) {
                    $(activeElement).css('opacity', 0.5);
                })
            );

            //3 this version makes all balls transparent on the first mouse over event 
            $("area").mouseover(
                (function (activeElement) {
                    return function() {
                        $(activeElement).css('opacity', 0.5);
                    }
                })
            );

            //4 also this version affecs all balls and not just the one that is mouse overed
            var activeBall = $(this);
            $("area").mouseover(function () {
                $(activeBall).css('opacity', 0.5);
            }).mouseout(function () {
                $(activeBall).css('opacity', 1);
            });

            run(index);
        });
    },

    $.fn.xyz.option = {};
})(jQuery);

基本上,SIAF正在做这样的事情:

(function(txt) { alert(txt); })('Hello world!');

你声明一个匿名函数(它没有名字),它接受一个参数,然后用末尾的括号,你调用它,并且parens中的函数是函数的参数。

所以,当你说

        (function (activeElement) {
            return function() {
                $(activeElement).css('opacity', 0.5);
            }
        })(this)

编译器看到“使用此对象作为参数激活函数”。看到这将如何在您声明的函数之外引用jQuery对象,jQuery将其视为“使用.css函数更改所有元素”。