css 解決盒子移動鼠標丟失產生的抖動問題

例如:html

一個div,鼠標懸浮但願div上移,鼠標離開div歸位。ui

若是直接給div添加hover事件,div會上下抖動。spa

解決方法是給div添加一個父元素:code

<!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>
相關文章
相關標籤/搜索