老师,请检查,谢谢

来源:4-3 编程练习

慕村6371425

2020-06-26 17:23:42

<!doctype html>

<html>

<head>

<meta charset="UTF-8">

<title>布局练习</title>

<style>

* {margin:0;padding:0;}

.background {

background: url(http://climg.mukewang.com/59c9f7ce0001839219034033.png) no-repeat center top;

width:100%;

height:4040px;

/* position:relative;*/


}

.left {

background: url(http://climg.mukewang.com/5a3383c70001f1b702240364.png) no-repeat;

width:224px;

height:364px;

position:fixed;

top:50%;

left:0;

margin-top:-182px;

}

.right {

background: url(http://climg.mukewang.com/5a3383d00001a3dd02240364.png) no-repeat;

width:224px;

height:364px;

position:fixed;

top:50%;

right:0;

margin-top:-182px;

}

</style>

</head>


<body>

<div class="background">

<div class="left"></div>

<div class="right"></div>

</div>

</body>

</html>

我想问问同样的效果用sticky怎么实现呢?

.left {

background: url(http://climg.mukewang.com/5a3383c70001f1b702240364.png) no-repeat;

width:224px;

height:364px;

position:sticky;

top:50%;

left:0;

margin-top:-182px;

}

不给上面的sticky在父元素添加scroll应该就是对视窗对齐呀,为什么像我这样设置垂直居中会无效呢?

写回答

1回答

好帮手慕夭夭

2020-06-27

同学你好,代码实现正确。另外,本题不能使用 position:sticky实现效果。虽然它类似于固定定位,但是并不是真正的固定定位。参考如下区分:

1.固定定位参照浏览器窗口定位,会脱离文档流。这里是要求元素固定在浏览器窗口的某一个位置,所以要使用固定定位。

2.而设置 position:sticky的元素,不会脱离文档流,它是相对离他最近的滚动祖先元素进行定位,例如body滚动,它就是参照body进行固定定位的。在实际开发中,它不常用,所以不建议使用它。

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

0

0 学习 · 40143 问题

查看课程