Turning my script into a plugin
Easy All
As I've been learning jQuery lately, I decided to write my own modal window to play videos in, when the video thumbnail is clicked. It all works fine, but I need some help turning it into a plugin, so I can use it on different pages with different parameters. I read the documentation, and a few tutorials, and I've got it 99% working, just now my 'window close' function doesn't work.
- <script type="text/javascript">
- (function($){
- $.fn.extend({
- mynewmodal: function(options) {
- var defaults = {
- container: "#modalcontainer"
- };
- var options = $.extend(defaults, options);
- return this.each(function() {
- var o = options;
-
- $(this).click(function (e) {
- e.preventDefault();
-
- $('body').append('<div id="overlay" />');
-
- $('#overlay').css({"display":"block","opacity":"0"}).animate({"opacity":"0.2"}, 300),
-
- $(o.container).css({"top":"43%", "opacity":"0"}).show().animate({"top": "50%", "opacity": "1"}, 550),
-
- $(o.container).append('<div id="modal-vid-close" title="Close window" />');
-
- $('#overlay', '#modal-vid-close').click(function () {
-
- $(o.container).animate({"top": "55%", "opacity": "0"}, 350).fadeOut(200),
-
- $('#overlay').fadeOut(200, function () { $(this).remove(); $('#modal-vid-close').remove()} )
- });
- });
- });
- }
- });
- })(jQuery);
- </script>
And then I fire it on my page using:
- <script type="text/javascript">
- var $j = jQuery.noConflict();
- $j(document).ready( function() {
- $j('a.video_container').mynewmodal({ container: "#video" });
- });
- </script>
I have an example up on jsbin if that'll help -
http://jsbin.com/adereI'm sure I'm just overlooking something simple, but for the life of me I can't figure out what it is. Any help much appreciated!