1

So I've got four circles next to each other, and when you move the mouse from one to the other, this code activates. But I only want it to activate when you move off/on any of the circles, not when you move from one .circle to another. Thanks folks!

 $('.circle').hover(
    function () {
        $(this).parent().animate({marginLeft: '-=25px'}, 1000);
    },
    function () {
        $(this).parent().stop(true, true).animate({marginLeft: '+=25px'}, 1000);
    }
 );
4
  • Register the event with the parent of all four circles and not the circles themselves? Commented Jun 29, 2012 at 17:52
  • but they are actually circles and I don't want it activating when you are outside the circles, but inside the parent. Commented Jun 29, 2012 at 17:54
  • Then use a boolean, true when already toggled and false when not. It determines when you animate and when you don't. Commented Jun 29, 2012 at 17:55
  • Could you refer me to somewhere that explains how to do that. It sounds like it would work, but I don' know how to implement it. Thanks. Commented Jun 29, 2012 at 18:01

2 Answers 2

1

check out this fiddle... http://jsfiddle.net/WxNdN/3/

e.relatedTarget should give you the element that you have entered or left. Check its class to see if it is a circle before firing your animation.

$('.circle').hover(
    function (e) {

        if(!$(e.relatedTarget).hasClass('circle'))
        {
            console.log('entering');
        }
    },
    function (e) {

        if(!$(e.relatedTarget).hasClass('circle'))
        {
            console.log('leaving');
        }
    }
 );​
Sign up to request clarification or add additional context in comments.

3 Comments

This is great, except there is space between my circles, so I think I need some delay() or something, to wait to check if the user will move over the next circle within a quarter second or something.
Do you have a public example of your circles? I may be able to whip something nifty together tomorrow.
Here it is So, yeah, it works pretty nice, but I don't like the latent left/right movement when you move from one circle to the next.
0

Without seeing the html, I'm guessing you'd need all of the circle class elements to be wrapped within a container, and then put the hover event on the container. So something like

<div id="container">
    <div class="circle"></div>
    <div class="circle"></div>
    <div class="circle"></div>
    <div class="circle"></div>
</div>

and then

$('#container').hover(functions...)

You'd then probably have to change $(this).parent() but it's hard to tell without seeing your current HTML structure.

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.