美文网首页
鼠标移入和移出

鼠标移入和移出

作者: WANGLIN_HZ | 来源:发表于2018-07-24 19:10 被阅读0次

鼠标移入和移出

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>鼠标移入移出</title>
    <style type="text/css">
        .box{
            width: 200px;
            height: 200px;
            background-color: gold;
            margin: 100px auto 0;
        }
        .son{
            width: 100px;
            height: 100px;
            background-color: green;
        }
    </style>
    <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
        $(function(){
            /*进入子元素也触发*/
            /*$('#div1').mouseover(function() {
                $(this).animate({marginTop: 50});//.stop()
            });
            $('#div1').mouseout(function() {
                $(this).animate({marginTop: 100});//.stop()
            });*/
            /*进入子元素不触发*/
            /*$('#div1').mouseenter(function() {
                $(this).stop().animate({marginTop: 50});//
            });
            $('#div1').mouseleave(function() {
                $(this).stop().animate({marginTop: 100});//
            });*/
            /*通过hover(mouseenter+mouseleave)实现简写*/
            $('#div1').hover(function() {
                $(this).stop().animate({marginTop: 50});
            }, function() {
                $(this).stop().animate({marginTop: 100});
            });
        })
    </script>
</head>
<body>
    <div id="div1" class="box">
        <div class="son"></div>
    </div>
</body>
</html>

元素绝对定位

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>元素绝对位置</title>
    <style type="text/css">
        .con{
            width: 600px;
            height: 600px;
            margin: 50px auto 0;
        }
        .box{
            width: 100px;
            height: 100px;
            background-color: gold;
            margin-bottom: 10px;
        }
        .pos{
            margin-left: 500px;
        }
        .pop{
            width: 100px;
            height: 100px;
            background-color: red;
            position: fixed;
            left:0;
            top: 0;
            display: none;
        }
    </style>
    <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script>
    <script type="text/javascript">
        $(function(){
            var $pos = $('.pos');
            //offset()是获取相对于页面左上角的绝对位置,即使外面再包一层con居中层,也不影响效果
            var pos = $pos.offset();
            // console.log(pos);
            // alert(pos.left + "," + pos.top);
            var w = $pos.outerWidth();
            var h = $pos.outerHeight();
            // alert(w);
            $('.pop').css({left:pos.left + w,top:pos.top});
            $pos.mouseover(function() {
                $('.pop').show();
            });
            $pos.mouseout(function() {
                $('.pop').hide();
            });
        })
    </script>
</head>
<body>
    <div class="con">
        <div class="box"></div>
        <div class="box"></div>
        <div class="box pos"></div>
        <div class="box"></div>
    </div>

    <div class="pop">提示信息!</div>
</body>
</html>

相关文章

网友评论

      本文标题:鼠标移入和移出

      本文链接:https://www.haomeiwen.com/subject/wlvymftx.html