0

我正在尝试增强(非常好的)jQuery Spotlight 插件,以便我可以以编程方式调用“隐藏”行为。

我已将相关代码移动到一个hide()函数中,当从 Spotlight 本身中调用它时它工作正常。但是当我尝试从聚光灯之外调用它时,什么也没有发生。我已经检查过它spotlight.hide实际上定义为 type function,但调用它似乎什么也没做。

(function($) {

    $.fn.spotlight = function(options) {

        var hide = function() {
            alert('hiding...');   /* never gets invoked when called from outside spotlight */
            if(settings.animate){
                spotlight.animate({opacity: 0}, settings.speed, settings.easing, function(){
                    if(currentPos == 'static') element.css('position', 'static');
                    element.css('z-index', '1');
                    $(this).remove();
                    // Trigger the onHide callback
                    settings.onHide.call(this);
                });
            } else {
                spotlight.css('opacity', '0');
                if(currentPos == 'static') element.css('position', 'static');
                element.css('z-index', '1');
                $(this).remove();
                // Trigger the onHide callback
                settings.onHide.call(this);
            }
        };

        // Default settings
        settings = $.extend({}, {
            opacity: .5,
            speed: 400,
            color: '#333',
            animate: true,
            easing: '',
            exitEvent: 'click',
            onShow: function(){},
            onHide: function(){}
        }, options);

        // Do a compatibility check
        if(!jQuery.support.opacity) return false;

        if($('#spotlight').size() == 0){
            // Add the overlay div
            $('body').append('<div id="spotlight"></div>');

            // Get our elements
            var element = $(this);
            var spotlight = $('#spotlight');

            // Set the CSS styles
            spotlight.css({
                'position':'fixed', 
                'background':settings.color, 
                'opacity':'0', 
                'top':'0px', 
                'left':'0px', 
                'height':'100%', 
                'width':'100%', 
                'z-index':'9998'
            });

            // Set element CSS
            var currentPos = element.css('position');
            if(currentPos == 'static'){
                element.css({'position':'relative', 'z-index':'9999'});
            } else {
                element.css('z-index', '9999');
            }

            // Fade in the spotlight
            if(settings.animate){
                spotlight.animate({opacity: settings.opacity}, settings.speed, settings.easing, function(){
                    // Trigger the onShow callback
                    settings.onShow.call(this);
                });
            } else {
                spotlight.css('opacity', settings.opacity);
                // Trigger the onShow callback
                settings.onShow.call(this);
            }

            // Set up click to close
            spotlight.live(settings.exitEvent, hide);
        }

        // Returns the jQuery object to allow for chainability.  
        return this;
    };


})(jQuery);

我安装它:

      var spotlight = $('#media-fragment').spotlight({
          opacity: .5,
          speed: 400,
          color: '#333',
          animate: false,
          easing: '',
          exitEvent: 'click',
          onShow: function(){},
          onHide: function(){}
      });

然后隐藏它我做:

spotlight.hide();

我很确定这this涉及范围或问题。

更新: https ://gist.github.com/2910643 上的完整解决方案。

4

1 回答 1

1

尝试改变:

var hide = function() {

到:

this.hide = function() {

var在父范围内定义函数或变量的范围,即本质上是protected. this另一方面,将在父对象上显式设置它prototype,并使其可公开访问。

于 2012-06-11T14:56:17.253 回答