两个问题,动画颤抖和元素定位

来源:2-18 编程练习

慕工程8451887

2019-08-18 06:56:25

问题一:动画运行时会有一个左右颤抖的效果,如何能避免呢?

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>2-5</title>
    <style type="text/css">
        div {
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            box-sizing: border-box;
            width: 400px;
            height: 400px;
            margin: auto;
            border: 1px solid red;
            border-bottom: 200px solid red;
            border-radius: 50%;
            transform-origin: 50% 50%;
            animation-name: rotate;
            animation-duration: 5s;
            animation-timing-function: linear;
            /*此处写代码*/
            animation-iteration-count: infinite;
        }
        
        div::before {
            position: absolute;
            content: "";
            top: 50%;
            margin: auto;
            box-sizing: border-box;
            width: 200px;
            height: 200px;
            background-color: red;
            border:80px solid white;
            border-radius: 50%;
        }
        
        div::after {
            position: absolute;
            content: "";
            top: 50%;
            left: 200px;
            margin: auto;
            box-sizing: border-box;
            width: 200px;
            height: 200px;
            background-color: white;
            border:80px solid red;
            border-radius: 50%;
        }
        
        @keyframes rotate {
            from {transform: rotate(0deg);}
            to   {transform: rotate(360deg);
    </style>
</head>
<body>
    <div></div>
</body>
</html>

问题二:在用 after 和 before 伪元素的时候,设置 top:0; bottom:0; margin: auto; 无法实现伪元素高度居中,而使用 top:50%可以实现高度居中。这是为什么呢??下面是一个示例代码

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>2-5</title>
    <style type="text/css">
        div {
            position: absolute;
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            box-sizing: border-box;
            width: 400px;
            height: 400px;
            margin: auto;
            border: 1px solid red;
            border-bottom: 200px solid red;
            border-radius: 50%;
        }
        
        div::after {
            position: absolute;
            content: "";
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            box-sizing: border-box;
            width: 200px;
            height: 200px;
            margin: auto;
            border: 1px solid green;
            border-bottom: 200px solid green;
            border-radius: 50%;
        }
        
    </style>
</head>
<body>
    <div></div>
</body>
</html>


写回答

1回答

好帮手慕码

2019-08-18

同学你好!
(1)因为动画运动的时候,宽高可能会超出body宽高,导致出现滚动条,可以给body设置overflow: hidden;

http://img.mukewang.com/climg/5d58f8180001413f03070075.jpg

(2)因为受到了父级的有效高度仅为200px的影响,其实是实现了垂直居中的:

http://img.mukewang.com/climg/5d58fc5c0001a56916700794.jpg

可以删除底边框查看:

http://img.mukewang.com/climg/5d58fc780001edca12310785.jpg

top:50%;是从top:0开始布局的,设置了绝对定位脱离文档流不受影响,推荐使用top:50%

如果帮助到了你,欢迎采纳,祝学习愉快~

0

0 学习 · 5760 问题

查看课程