views:

74

answers:

4

I'm using the below code for a rollover effect, seems to be working okay!

What I'm interested in is taking this to the next step in a modular way. What I mean is that rather than stipulate every single possibility, how could I start to produce what I suppose would be a plugin?

Many thanks!

$(document).ready(function() {

    $('#damart_490').hide();
    $('#simply_490').hide();

    $('.damart_link').hover(
        function() {
            $('#damart_490').show('blind',250);
                    },
        function() {
            $('#damart_490').hide('blind',250);
    });

    $('.simply_link').hover(
        function() {
            $('#simply_490').show('blind',250);
                    },
        function() {
            $('#simply_490').hide('blind',250);
    });

});
+6  A: 

The next step would be to reduce repetition by writing a function:

function hover_link(link, element)
{
    $(element).hide();

    $(link).hover(
        function() {
            $(element).show('blind',250);
        },
        function() {
            $(element).hide('blind',250);
    });
}

hover_link('.damart_link', '#damart_490');
hover_link('.simply_link', '#simply_490');
Tobias Cohen
Exactly the kind of thing I was looking for, much appreciated!If I was to take it further would it be to turn the child elements of a tag into a set of triggers and the children of another tag into the targets? Still trying to get my head around it...
Cordial
Try not to overcomplicate things. The most important thing when writing good code is express your point as clearly as possible with minimum repetition. Only add more code as you find you need it, and when it doesn't look clear or concise any more, it's time to refactor.
Tobias Cohen
+1  A: 

You could try the button widget from JQuery UI

JQuery UI

George
Sorry I don't understand in which way I would use, and benefit from implementing, the button widget. Could you explain any further? Thank-you!
Cordial
I just had a better look at the code you submited . I think you can achieve the same results with JQuery UI tabs http://jqueryui.com/demos/tabs/#mouseover
George
Cordial is describing a really simple behavior. I wouldn't recommend bringing in something heavy like jQuery UI just for this.
Tobias Cohen
I'm using UI for some simple animations but from what I understand of jQuery UI Tabs, the order of the code is important and can't be rearranged - is this correct? These links and targets are all over the place and need to stay that way ideally.
Cordial
A: 

Sorry if this is very simple but how would I make this into a separate file plugin and reference it? At the moment I've got a file called rollover.js with this in...

function hover_link(link, element)
    {
        $(element).hide();

        $(link).hover(
            function() {
                $(element).show('blind',250);
            },
            function() {
                $(element).hide('blind',250);
        });
    }

And this in my page...

<script src="js/rollover.js" type="text/javascript"></script>
<script type="text/javascript">

$(document).ready(function() {

$hover_link('.damart_link', '#damart_490');
$hover_link('.simply_link', '#simply_490');

});

</script>

Am I just missing some syntax?! Thank-you!

Cordial
also, should i be answering my own question? new to SO and not sure of etiquette :s
Cordial
you should not, especially if you are asking :) Edit your original question and delete this answer
Marek
+1  A: 

There seems to be an issue with the way you're calling the jquery show() function. The documentation for show() says it will accept only a duration and an optional callback function to execute once the effect is complete.

.show( duration, [ callback ] )

I see no support for effect style 'blind' that you've used. Maybe I don't have all the code you're running. The example shown above crashes for me because 250 is an integer not a valid function.

As for the option to make the function a jQuery plugin I've built this:

$.fn.hover_link = function(target){

    //hide the target that will be toggled and store a reference to it
    $target = $(target).hide();     

    $(this).hover(
        function(){
            $(target).show(250);
        },

        function(){
            $(target).hide(250);
        }
    )

    //support jQuery chaining
    return this;
}

You can use it like this:

HTML

<a href="#" class="trigger">The trigger</a>
<div class="targetElement">The target</div>

JavaScript

$('.trigger').hover_link('.targetElement')

You can read a good tutorial about jquery plugins right on their website at: http://docs.jquery.com/Plugins/Authoring

Razvan Caliman
That's great many thanks!The 'blind' is part of jQuery UI, just using it for the animated effect.
Cordial