0
        $("#theDiv").hover(function(){
            $(this).animate( {top: "300px", left: "400px", width: "50px" , height: "50px" },"Fast")
        }); 

        $("#theDiv").click(function(){
            $(this).stop();
        }); 

Code as above. I tried to stop the hover function when I click it, but it doesn't work. Or is it not possible at all ?

2 Answers 2

1
$("#theDiv").mouseenter(function(){
    $(this).animate( {
        top: "300px",
        left: "400px",
        width: "50px" ,
        height: "50px"
    } , "fast");
}).click(function(){
    $(this).stop(true, false);
});

.stop( [clearQueue] [, jumpToEnd] )

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

1 Comment

This won't work, the trigger will occur the second you move the mouse after clicking.
0
$("#theDiv").on({
    mouseenter: function() {
        $(this).animate( {top: "300px", left: "400px", width: "50px" , height: "50px" },"Fast");
    },
    click: function() {
        $(this).off('mouseenter').stop(true);
    }
});

FIDDLE

4 Comments

I would suggest a variation of this answer, with a setTimeout() function to re-enable the mouseenter, allowing the user enough time to move their mouse away from the element and avoid re-triggering the hover.
The question is "how to stop the hover function", are you sure the OP would like it to start again after a set time ?
the only two things I'm sure of in life are death and taxes. I was just thinking that it would be nice to be able to renable the animation because an element sitting in the middle of the viewport might become a bit of an intrusion. I meant nothing more than that, sorry.
Maybe an element in the middle of the viewport is exactly what the OP would like :-) -- Seriously though, it does'nt really say anything about it in the question, but if that is the wanted effect, it's easy to add.

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.