JavaScript plugin compatible with jQuery [closed]
开发者_Go百科
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this questionI plan to write a JavaScript plugin. I'd like it to take advantage of jQuery features, but also degrade gracefully if jQuery is not present o the page.
For a slideshow for example, jQuery users would call:
$("#Placeholder").Slideshow();
Plain JavaScript pages could still use it, just with less features (e.g. no animations), or features that only work in some browsers:
Slideshow(Placeholder);
I'd be interested in examples, best practices, tutorials on how to do this.
I haven't written any non-jquery plugins, but if you want your plugin to work without jquery, I'd suggest you write your core implementation in plain javascript and write a way to access it via jquery, in a jquery-ish API.
Let me illustrate, if your plugin provides,
doAwesomeStuff(domElement);
then, if jQuery is available, also do this,
jQuery.fn.doAwesomeStuff = function (options) {
this.each(function () {
doAwesomeStuff(this[0]);
});
};
So, if jquery is available, the plugin could be used as a jquery plugin, with a jquery-ish API to access it. If not, the plain javascript API would anyway be available, and since the internal implementation should anyway be in plain javascript, should work without any problem.
精彩评论