jQuery 插件简单模板
2014-03-21 12:08
344 查看
/*! * Copyright yunos.com All rights reserved. * jquery.scrollspy.js * @author v10258@qq.com * @version 0-0-1 */ (function ($) { // contructor function var ScrollSpy = function (element, options) { this.element = $(element); this.options = $.extend({}, ScrollSpy.defaults, options || {}); this.anchorArr = $(element).find('a'); this.init(); }; // default config ScrollSpy.defaults = { scrollEle: $(window) }; // prototype ScrollSpy.prototype = { constractor: ScrollSpy, init: function () { var self = this, element = this.element, scrollEle = this.options.scrollEle, anchorData, scrollTop; scrollEle.on('scroll', function () { // The realization of the bad dynamic acquisition anchorData = self.getAnchorData(); scrollTop = $(this).scrollTop(); for (var i = 0, len = anchorData.length; i < len; i++) { if (anchorData[i].min <= scrollTop && scrollTop < anchorData[i].max) { $(anchorData[i].author).parent().addClass('current').siblings().removeClass('current'); break; } } }); }, getAnchorData: function () { var anchorArr = this.anchorArr, active, activeDom, anchorTargetArr = []; anchorArr.each(function (i, n) { active = $($(this).attr('href')); activeDom = active[0]; anchorTargetArr.push({ ele: activeDom, author: this, min: active.offset().top, max: active.offset().top + active.height() }); }); return anchorTargetArr; }, distroy: function(){ this.element.removeData('scrollspy'); this.options.scrollEle.off('scroll'); } }; // bridging $.fn.scrollspy = function (option) { return this.each(function () { var $this = $(this), data = $this.data('scrollspy'), options = typeof option == 'object' && option; if (!data) $this.data('scrollspy', (data = new ScrollSpy(this, options))); if (typeof option == 'string') data[option](); }); }; })(jQuery);
个人感觉,写插件,插件的具体结构,能用原生结构,还是用原生结构的好,最后桥接在jquery上.
如果像下面这样,看起来是不是有点 蛋疼呢。
(function ($) { $.love.defaults = { // ...... }; $.love = function (element, o) { this.element = element; this.options = $.extend({}, $.love.defaults, o); // ...... }; var $love = $.love; $love.fn = $love.prototype = { verson: '0.0.1' // ...... }; $love.fn.extend = $love.extend = $.extend; $love.fn.extend({ metod: function () { } // ...... }); $.fn.ilove = function (o) { // ...... }; })(jQuery);
相关文章推荐
- 编写一个简单的 jQuery 插件(模板)
- 出位的template.js 基于jquery的模板渲染插件,简单、好用
- 基于jquery的lazy loader插件实现图片的延迟加载[简单使用]
- 简单的 jQuery 浮动层随窗口滚动滑动插件实例
- jquery中实现简单的tabs插件功能的代码
- 利用jQuery编写一个简单的插件
- jQuery的图片滑动翻牌效果插件,适合对图片的简单描述交互
- 基于jQuery简单实用的Tabs选项卡插件
- jquery插件chosen和select的简单应用
- jQuery的Form插件的简单入门
- 35 个高级但使用简单的 jQuery 插件
- jQuery插件模板
- 好用的jquery分页插件,功能齐全,简单
- jQuery插件实现的方法和原理简单说明
- Jquery操作Select 简单方便 一个js插件搞定
- Jquery 模板数据绑定插件 使用方法
- 简单的Jquery 插件编写
- 最简单的jquery插件开发示例
- 一个简单易懂且实用的JQuery分页插件(jquery.page)(详解)
- 制作一个超简单的全屏插件(基于JQuery)