固定元素,不随页面改变位置position:fixed

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LzzMandy/article/details/84871437

当页面滑动到第2个页面时,黄色固定元素一直处于初始化的位置不变,实现这个功能:

1、使用position:fixed;还需要指定位置,比如top、right值等。

2、注意【需要固定的元素在html中放置的位置】

下面的代码中,“第2个固定” 显示在页面上,而 "第1个固定" 没有显示,因为“第2个固定”放在后面,这样会覆盖前面元素的z-index等级,如果想让 "第1个固定"显示,则需要添加 " z-index:999; ",使其等级高于其他元素即可。

<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<title>练习的地方</title>
	<style>
	.divStyle{width: 1000px;height: 1000px;margin:20px auto;border: 1px solid red;}
	.fixed{position:fixed;top:30%;right:30%;width: 100px;height: 100px;border-radius: 50px;background: yellow;text-align: center;line-height: 100px;}
</style>
</head>
<body>
	<div class="fixed">第1个固定</div>
	<div class="divStyle">第1个页面</div>
	<div class="divStyle">第2个页面</div>
	<div class="fixed">第2个固定</div>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/LzzMandy/article/details/84871437