JQuery中心插件,扩展了功能

时间:2011-10-23 19:05:08

标签: jquery

我有一段代码用于通过jquery对任何元素进行居中。

(function($){
$.fn.centerIt = function(settings){

    var opts = $.extend({}, $.fn.centerIt.defaults, settings);

    return this.each(function(settings){
      var options = $.extend({}, opts, $(this).data());
      var $this = $(this);

      $this.css({
        position:options.position,
        top:'50%',
        left:'50%',
        width:options.width,                 // adjust width
        height:options.height,               // adjust height
        zIndex:1000,
        marginTop:parseInt((options.height / 2), 10) + 'px'  // half of height
        marginLeft:parseInt((options.width / 2), 10) + 'px'  // half of height
      });

    });
}

// plugin defaults - added as a property on our plugin function
$.fn.centerIt.defaults = {
  width: '600px',
  height: '600px',
  position:'absolute'
}

})(jQuery);

$('#elementId').centerIt({width:'400px', height:'200px'});

此代码无法正常运行。告诉我什么是错的,什么要纠正,结果它会正常工作....谢谢。

1 个答案:

答案 0 :(得分:2)

此行marginTop:parseInt((options.height / 2), 10) + 'px'最后缺少,

相关问题