Building simple jQuery plugin, need assistance

Posted by kirisu_kun on Stack Overflow See other posts from Stack Overflow or by kirisu_kun
Published on 2011-01-13T15:50:49Z Indexed on 2011/01/13 15:53 UTC
Read the original article Hit count: 193

Hi there,

I'm building my first ever jQuery plugin (it's just a simple experiment). Here's what I have so far:

(function($){
$.fn.extend({
    auchieFader: function(options) {
        var defaults = {
            mask: '',
            topImg : '',
        }
        var options = $.extend(defaults, options);
        return this.each(function() {
            var o = options;
            var obj = $(this);
            var masker = $(o.mask, obj);
        masker.hover(function () {
        $(o.topImg).stop().animate({
            "opacity": "0"
        }, "slow");
    }, function () {
        $(o.topImg).stop().animate({
            "opacity": "1"
        }, "slow");
    });    
        });
    }
});   
})(jQuery);

I'm then calling the plugin using:

$('.fader').auchieFader({mask: ".mask", topImg: ".top"});

If I then add another request say:

$('.fader2').auchieFader({mask: ".mask", topImg: ".top"});

Then no matter what instance of my 2 faders I hover both of them will trigger. I know this is because my mask mask and topImg options have the same class - but how can I modify the plugin to allow for these items to have the same class? I know it's probably something really simple, but I'm still finding my way with jQuery and Javascript in general. Any other tips on improving my code would also be greatly appreciated!

Cheers, Chris

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery