0
$("#mydiv").hover(function() {
    $(".class").stop(true, true).fadeIn(100);
}, function() {
    $(".class").stop(true, true).fadeOut(100);
});

Suppose I have a snippet like something above. I have a lot similar hover functions need to be bound. How can I write a custom function to achieve this?

Something like:

$("#mydiv").myHover(".class", 100);

1 Answer 1

3

You would write what's known as a jQuery plugin:

$.fn.myHover = function(s, d){
    return this.each(function(){
        var $this = $(this), $selected = $(s);
        $this.hover(function(){
            $selected.stop(true, true).fadeIn(d);
        }, function(){
            $selected.stop(true, true).fadeOut(d);
        })
    });
}

JSFiddle

You can learn all about writing them yourself here: http://learn.jquery.com/plugins/

Sign up to request clarification or add additional context in comments.

2 Comments

yes, PLUGIN! I do not know how to ask this question. The keyword PLUGIN save me! Thank you!
I'm glad you're saved (:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.