繁体   English   中英

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

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

我有一个弹出的img(用css动画),然后当我将鼠标悬停在图像上时,将图像信息设置为动画/淡入淡出。 问题是我无法将鼠标悬停在图像信息上(通常会有链接),因为当我输入图像信息时,它会一直移动(滑入)。

这是小提琴:

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

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

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

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

注意:您有一个错误,即向右移出鼠标会导致随后的悬停,从而使.imginfo的位置脱离混乱。

好的,我可以根据自己的具体情况自行解决。 做了一个新的小提琴来说明基本知识。

首先,为了在两个div之间移动,我对JSFiddle进行了调整: http : //jsfiddle.net/adeneo/LdDBn/以满足我的需要。

唯一的问题是,因为我正在使用CSS为图像设置动画,所以当我将鼠标悬停在img信息上时,图像会缩小到其原始大小。

为了解决这个问题,我删除了css(只保留.img的时间元素,否则动画会太快),然后删除了对.img:hover的任何影响,但是我改用jquery .css()进行更改。

这是最后的小提琴: http : //jsfiddle.net/user100042/YLGGg/6/

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