5

I am trying to attach an event to a separate onhover trigger. But I am having problems using multiple selectors since its dynamic.

Need help ::: Upon hovering on the yellow box named 'Rings', this should trigger the animated slide event for the green box above it.

$('.boxgrid1').hover(function(){  
    $(".cover", this).stop().animate({top:'0px'},{queue:false,duration:300});  
}, function() {  
    $(".cover", this).stop().animate({top:'247px'},{queue:false,duration:300});  
});
3
  • nice design, one question do you mean the same effect that appears on hover the green, and you also want the yellow region to activate it? Commented Dec 26, 2010 at 6:40
  • What exactly do you want answered here? Do you want help to bid events to your images "Rings", "earrings" etc and that should trigger an animate? Commented Dec 26, 2010 at 6:45
  • Given the structure of your HTML you may have to use some of jQuery's DOM traversal functions to accomplish that. You may also want to add a common class to each of your images so that you can select them all and animate them using a single hover() binding. Commented Dec 26, 2010 at 6:46

2 Answers 2

4

With a few markup tweaks we can greatly simplify your code, for example let's give those hover <div> elements a common class as well, like this:

<div class="boxgrid boxgrid1">

Then your code becomes much simpler, you can replace all that repeated code with this:

$('.boxgrid .cover').css({ top: '247px' });

$('.boxgrid').hover(function() {
    $(".cover", this).stop().animate({ top: '0px' }, 300);
}, function() {
    $(".cover", this).stop().animate({ top: '247px' }, 300);
});
$("#shop_buttons_table tr:nth-child(2)").delegate("td", "mouseenter mouseleave", function(e) {
    $("#shop_buttons_table tr:nth-child(1) .boxgrid").eq($(this).index()).trigger(e);
});

You can test it out here, all we're doing is taking the "hover" events from the lower cells and passing them onto the .boxgrid elements in the row before, the net effect (with the .stop() calls you already had) is a single hoverable area for the user.

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

1 Comment

Thanks! It is very helpful to see and understand the optimization.
0

Give a class name to your <a> tag outside <img>(RINGS) tag like <a class="boxgrid1" href="#">

Comments

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.