关于溢出隐藏

关于溢出隐藏

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="css1.css" type="text/css">
</head>
<body>
    <div class="sky">
        <div class="cloud cloud1"></div>
        <div class="cloud cloud2"></div>
        <div class="cloud cloud3"></div>
        <div class="cloud cloud4"></div>
        <div class="cloud cloud5"></div>
    </div>
    <div class="grassland">
        <img src="rabbit.png"/>
    </div>
</body>
</html>
*{
    padding:0;
    margin:0;
}
html,body{
    height:100%;

}

/*天空*/
.sky{
    /*position:relative;  !*天空设置定位时,溢出隐藏才有效,如果去除定位属性,溢出隐藏无效*!*/
    width:100%;
    height:60%;
    background:rgb(196,228,253);
    background: linear-gradient(to bottom,rgba(196,228,253,1),rgba(196,228,253,0));
    overflow:hidden;
}
/*5朵云的公共属性*/
.sky .cloud{
    position:absolute;  /*对.sky .cloud设置相对定位时,溢出隐藏才有效,如果设置绝对定位或者去除定位属性,溢出隐藏无效*/
    width:100px;
    height:100px;
    border-radius:50px;
    background:white;
}
.sky .cloud::before{
    display:block;
    content:'';
    width:50px;
    height:70px;
    border-radius:30px;
    background:white;
    transform:rotate(-30deg) translate(-40px,10px);
}
.sky .cloud::after{
    display:block;
    content:'';
    width:160px;
    height:50px;
    border-radius:30px;
    background:white;
    transform:translate(-50px,-30px);
}

/*分别设置5朵云的位移、缩放和透明度*/
.sky .cloud1{
    transform:translate(0px,0px) scale(1);
    opacity:1;
    animation:movement 10s linear 1s 3;
}
.sky .cloud2{
    transform:translate(300px,60px)  scale(0.6);
    opacity:0.6;
    animation:movement 7s linear 1s 3;
}
.sky .cloud3{
    transform:translate(400px,20px)  scale(0.7);
    opacity:0.7;
    animation:movement 7s linear 1s 3;
}
.sky .cloud4{
    transform:translate(700px,120px)  scale(0.6);
    opacity:0.6;
    animation:movement 8s linear 1s 3;
}
.sky .cloud5{
    transform:translate(1000px,70px)  scale(0.9);
    opacity:0.9;
    animation:movement 9s linear 1s 3;
}

/*定义5朵云的动画*/
@keyframes movement {
     from{
         margin-left:100%;
     }
     to{
         margin-left:-100%;
     }
 }

/*草地*/
.grassland{
    position:relative;
    width:100%;
    height:40%;
    background:rgb(148,190,89);
    background: linear-gradient(to top,rgba(148,190,89,1),rgba(148,190,89,0));
}

.grassland img{
    position:absolute;
    width:150px;
    height:150px;
    right:200px;
    bottom:50px;
}

我看老师在作业批复中是将html和body设置了overflow:hidden,但如果我对天空 .sky 设置overflow:hidden,发现设置定位时,溢出隐藏才有效,如果去除定位属性,溢出隐藏为什么就无效了?

http://img1.sycdn.imooc.com//climg/5e27fcc009ec996408570339.jpg

或者,如果我对 .sky .cloud 设置相对定位时,溢出隐藏才有效,如果设置绝对定位或者去除定位属性,溢出隐藏就无效?

正在回答

登陆购买课程后可参与讨论,去登陆

3回答

同学你好,首先,相对定位没有脱离文档流,因此不会去参照谁去定位的。

其次,给.sky设置超出隐藏,并给.cloud设置相对定位实际上和不给sky和.sky .cloud添加定位,只给.sky 添加超出隐藏的效果是一致的,可以实现清除滚动条。

如果我的回答帮到了你,欢迎采纳,祝学习愉快~

提问者 慕函数1351374 2020-01-22 17:28:00

http://img1.sycdn.imooc.com//climg/5e2814bb09f6819408670342.jpg

那如图,如果给.cloud设置了相对定位呢?应该不是根据最近定位祖先元素了,而是以.cloud自身作为参考的吧?为什么这时父元素设置了溢出隐藏有效了呢?

好帮手慕码 2020-01-22 16:32:55

同学你好,本练习中不需要给sky和.sky .cloud添加定位。可以测试一下,不给sky和.sky .cloud添加定位,给.sky 添加超出隐藏就可以实现超出隐藏:

http://img1.sycdn.imooc.com/climg/5e28072009e9287508560360.jpg

另,如果直接给.sky .cloud 设置绝对定位,不给sky设置相对定位的话,.sky .cloud是按照body作为参照物的,那么在body中.sky .cloud的宽度在运动过程中还是会出现宽度超出的现象,因此无法给.sky设置超出隐藏无法实现清除滚动条的效果。

所以最好的方法就是给body,html添加超出隐藏,无需给sky和.sky .cloud添加定位

如果我的回答帮到了你,欢迎采纳,祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师