我有以下代码jQuery插件变量
(function($) { // carousel var Carousel = { settings: { itemsPerPage: 1, itemsPerTransition: 1, noOfRows: 1, pagination: true, nextPrevLinks: true, speed: 'normal', easing: 'swing', loop: false, auto: true, autoTime: 4000, maxHeight: 300, maxWidth: 500 }, init: function(el, options) { if (!el.length) { return false; } this.options = $.extend({}, this.settings, options); this.container = el; this.panelHeight = 0; this.panelWidth = 0; this.numPanels = 0; // Find biggest panel in set this.container.find(".tinycarousel > li > div").each(function() { if($(this).outerHeight() > this.panelHeight) { this.panelHeight = $(this).outerHeight(); } if($(this).outerWidth() > this.panelWidth) { this.panelWidth = $(this).outerWidth(); } this.numPanels++; }); alert(this.numPanels); }, autoSwitch: function() { this.container.find(".tinycarousel").animate({marginLeft: -panelWidth}); } }; // bridge $.fn.tinycarousel = function(options) { return this.each(function() { var obj = Object.create(Carousel); obj.init($(this), options); $.data(this, 'carousel', obj); }); }; })(jQuery);
我在这里的问题是,this.numPanels
给出0的结果(和我知道应该是3)。它在我使用this.
之前有效,并且只有numPanels
作为一个普通变量,但现在它不是。对不起,我无法提供更多信息,但我的问题是:我如何在jQuery插件'可编辑'内部创建变量。
编辑 我对任何混淆道歉 - 我只是不想发布愚蠢的代码,并使其不可读。请看我上面的完整代码。
EDIT 2我感觉像吸了pillock ......看看在autoSwitch
功能 - 这是我得到一个错误:panelWidth is not defined
这就是为什么我试图使用this.
我对此感到抱歉 - 请参阅我的OP了解整个代码。我应该已经更清楚了。 – Bojangles 2011-03-09 20:46:36
更多编辑:-(对不起! – Bojangles 2011-03-09 20:52:19
@JamWaffles - 查看编辑 – 2011-03-09 21:08:54