简体   繁体   English

jQuery的。 在两个元素之间移动(一个动画)?

[英]JQuery. Moving between two elements (one animated) on hover?

I have an img that pops up (animated with css) and then I animated the image info to slide/fade in when I hovered over the image. 我有一个弹出的img(用css动画),然后当我将鼠标悬停在图像上时,将图像信息设置为动画/淡入淡出。 The problem is that I can't hover over to the image info (where there would normally be links) because it keeps moving (sliding in) when I enter it. 问题是我无法将鼠标悬停在图像信息上(通常会有链接),因为当我输入图像信息时,它会一直移动(滑入)。

Here's the fiddle: 这是小提琴:

http://jsfiddle.net/user100042/YLGGg/ http://jsfiddle.net/user100042/YLGGg/

jQuery: jQuery的:

$(document).ready(function () {
    $(function ($) {
        $('.imginfo').hide();
        $('.img, .imginfo').hover((function () {
            $(".imginfo").dequeue().fadeIn('slow');
            $(".imginfo").dequeue().animate({
                'left': '-=100px'
            });
        }), function () {
            $(".imginfo").dequeue().fadeOut('slow');
            $(".imginfo").dequeue().animate({
                'left': '+=100px'
            });
        });
    });
});

http://jsfiddle.net/isherwood/YLGGg/3 http://jsfiddle.net/isherwood/YLGGg/3

<div id="wrapper">
    <div class="img">Img</div>
    <div class="imginfo">ImgInfo</div>
</div>

$('#wrapper').hover((function () {...});

Heads-up: You have a bug where mousing out to the right causes a subsequent hover, throwing the position of .imginfo out of whack. 注意:您有一个错误,即向右移出鼠标会导致随后的悬停,从而使.imginfo的位置脱离混乱。

Okay, I figured it out on my own for my specific situation. 好的,我可以根据自己的具体情况自行解决。 Made a new fiddle to illustrate the basics. 做了一个新的小提琴来说明基本知识。

First, for moving between the two divs I tweaked this JSFiddle: http://jsfiddle.net/adeneo/LdDBn/ for my needs. 首先,为了在两个div之间移动,我对JSFiddle进行了调整: http : //jsfiddle.net/adeneo/LdDBn/以满足我的需要。

The only problem was since I was animating the images with css, when I hovered over img info, the image would scale back down to it's original size. 唯一的问题是,因为我正在使用CSS为图像设置动画,所以当我将鼠标悬停在img信息上时,图像会缩小到其原始大小。

To fix this, I deleted the css (only leaving the timing elements for .img otherwise it would animate too quickly) then deleted any effects on .img:hover, but these I moved to change instead with jquery .css(). 为了解决这个问题,我删除了css(只保留.img的时间元素,否则动画会太快),然后删除了对.img:hover的任何影响,但是我改用jquery .css()进行更改。

Here's the final fiddle: http://jsfiddle.net/user100042/YLGGg/6/ 这是最后的小提琴: http : //jsfiddle.net/user100042/YLGGg/6/

jQuery: jQuery的:

$(document).ready(function () {
    $(function ($) {
        var timer;
        $('.imginfo').hide(); //this wasn't necessary in my final code as I hid it off screen (right:-100%)
        $('.img').mouseenter(function () { //changed to just hover over .img, .imginfo wasn't neccesary
            //$(".img").dequeue().css({
            //css transforms would go here
            $(".imginfo").dequeue().fadeIn('fast'); //sped up all animations to fast to help get rid of problems with fast mouse movements
            $(".imginfo").dequeue().animate({
                'left': '100px' //stopped using += used fixed location instead
            }, 'fast');
        });
        $(".img, .imginfo").mouseleave(function () {
            timer = setTimeout(doSomething, 100); //set to 100 so accidental mouse movements don't stop imginfo animation
        }).mouseenter(function () {
            clearTimeout(timer);   
        });
        function doSomething() {
            //$(".img").dequeue().css({
            //css untransforms (so if previously you scaled to 1.5, here you'd scale back to 1.0)
            $(".imginfo").dequeue().fadeOut('fast');
            $(".imginfo").dequeue().animate({
                'left': '200px' //in the final code I set this to right:-100%
            }, 'fast');
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM