javascript - jQuery中在两个拥有相同mouseover的元素之间移动的问题

查看:111
本文介绍了javascript - jQuery中在两个拥有相同mouseover的元素之间移动的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

$('#d11,#d12').on('mouseover',function(){
                $('#d2').animate({opacity:'100'});
            });

如代码所示,在d11,d12之间移动,animate会执行照成物体一闪一闪的,怎么解决。
详细代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <style>
        #d1{overflow:hidden;}
        #d1 div{height:50px;width:50px;float:left;}
        #d11{background:#F11;}
        #d12{background:#ff2;}
        #d2{height:50px;width:100px;background:#000;opacity:0;filter:alpha(opacity=0);}
    </style>
    <script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.12.4/jquery-1.12.4.min.js"></script>
</head>
<body>
    <div id="d1">
        <div id="d11"></div>
        <div id="d12"></div>
    </div>
    <div id="d2"></div>
    <script>
        $(document).ready(function(){
            $('#d11,#d12').on('mouseover',function(){
                $('#d2').animate({opacity:'100'});
            });
            $('#d11,#d12').on('mouseout',function(){
                $('#d2').animate({opacity:'0'});
            });
        });
    </script>
</body>
</html>

解决方案

// 先终断之前的动画
$(document).ready(function(){
    $('#d11,#d12').on('mouseover',function(){
        $('#d2').stop(true).animate({opacity:'100'});
    });
    $('#d11,#d12').on('mouseout',function(){
        $('#d2').stop(true).animate({opacity:'0'});
    });
});

这篇关于javascript - jQuery中在两个拥有相同mouseover的元素之间移动的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆