css 解决盒子移动鼠标丢失产生的抖动问题

例如:

一个div,鼠标悬浮希望div上移,鼠标离开div归位。

如果直接给div添加hover事件,div会上下抖动。

解决方法是给div添加一个父元素:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style> 
        * {
            padding: 0;
            margin: 0;
        }
        .box-in {
            background-color: blue;
            width: 100px;
            height: 100px;
            transition: 1s all;
            position: absolute;
            left: 0;
            top: 0;
        }
        .box:hover .box-in{
           top: -50px;
        }
        .box {
            width: 100px;
            height: 100px;
            margin: 200px auto;  
            position: relative; 
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="box-in"></div>
    </div>
    
</body>
</html>
原文地址:https://www.cnblogs.com/fyj-web-18/p/9579058.html