position:fix相对父元素定位

时间:2022-10-02 22:19:42

大家都知道,当position的值为fix时,生成绝对定位的元素,相对于浏览器窗口进行定位。

它常常应用的场合是,当下拉滚动条时固定导航栏到顶部,将广告固定在页面两侧或浏览器中间。

如果需要将导航栏div固定到浏览器顶部,只需要将top设置为0即可。

如果要将广告div固定在特定位置,只需要用js计算出div应显示的位置,再设置top和left属性。

当我们想要设置一个div相对于其父元素定位,大家一定会想,将父元素position设置为relative,子元素为absolute不就可以了吗?

但有些时候,我们想要这个div相对父元素定位,要想保留fix定位的一些特征。比如,一个父容器下有一个div,我们想将这个div固定在父容器的顶部(而不是整个浏览器的顶部),当拉动滚动条时它的位置不发生变化,这时应该怎么做呢

position:fix相对父元素定位

如上图,我们想实现的效果是,红色部分固定在content的顶部位置,实现代码为

 <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title> 页面名称 </title>
<style type="text/css">
html, body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="par" style='margin-top:150px;background-color:blue; position: relative;height:1500px'>
<div id="fix" style="position: absolute;top:0px;left:60px;width:180px;background-color:red;">
这个是固定的DIV
</div> </div>
<script type="text/javascript">
window.onscroll = function () {
var fix = document.getElementById("fix");
var par = document.getElementById("par");
var st = document.documentElement.scrollTop || document.body.scrollTop;
var pt = par.offsetTop;
fix.style.position = st>pt ? "fixed" : "absolute";
}
</script>
</body>
</html>

还有一种存在左侧导航栏的情况

position:fix相对父元素定位

实现代码为

 <!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title> 页面名称 </title>
<script src="https://cdn.bootcss.com/jquery/1.11.1/jquery.js"></script>
<style type="text/css">
html, body {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div>
<div style="width:100%;height: 100px; background-color: #ccc;">
</div>
<div>
<div style="width:20%;height:100%;background-color: #bbb;float: left">333333333333</div>
<div id="par" style=' float: left; width:80%;background-color:blue; position: relative;height: 1500px'>
<div id="fix" style="position: absolute;top:0px;left:0px;width:100%;background-color:red;height:100px">
这个是固定的DIV
</div> <div id='ct' style="margin-top: 120px">
</div> </div> </div> </div> <script type="text/javascript">
window.onscroll = function () {
var fix = document.getElementById("fix");
var par = document.getElementById("par");
//var sp = document.getElementById("sp");
var ct = document.getElementById("ct");
var st = document.documentElement.scrollTop || document.body.scrollTop;
var pt = par.offsetTop;
if (st>pt) {
fix.style.left = par.offsetLeft + "px";
fix.style.width = par.offsetWidth + "px";
fix.style.position = "fixed";
var top = (document.documentElement.scrollTop - 200 + 120) + 'px';
console.log(top);
$(ct).css('margin-top','120px');
} else {
fix.style.left = "0px";
fix.style.width = "100%";
fix.style.position = "absolute";
}
}
</script>
</body>
</html>

position:fix相对父元素定位的更多相关文章

  1. position&colon;fixed 相对父元素定位

    position:fixed是对于浏览器窗口定位的,要实现相当于父元素定位,可以这样: 不设置fixed元素的top,bottom,left,right,只设置margin来实现. 这种方法本质上fi ...

  2. 解决IE6&comma;IE7下子元素使用position&colon;relative、父元素使用overflow&colon;auto后,子元素不随着滚动条滚动的问题

    解决IE6,IE7下子元素使用position:relative.父元素使用overflow:auto后,子元素不随着滚动条滚动的问题   在IE6,IE7下,子元素使用position:relati ...

  3. 关于元素设置margin-top能够改变body位置的原因及解决(子元素设置margin-top改变父元素定位)

    关于元素设置margin-top能够改变body位置的原因及解决(子元素设置margin-top改变父元素定位) 起因:在进行bootstrap的.navbar-brand内文字设置垂直居中时采用li ...

  4. 【随笔】关于绝对定位absolute相对于父元素定位的问题

    绝对定位absolute的官方定义: 设置为绝对定位的元素框从文档流完全删除,并相对于其包含块定位,包含块可能是文档中的另一个元素或者是初始包含块.元素原先在正常文档流中所占的空间会关闭,就好像该元素 ...

  5. 内层元素设置position&colon;relative后父元素overflow&colon;hidden overflow&colon;scroll失效 解决方法

    内层元素设置position:relative后父元素overflow:hidden overflow:scroll 都失效 解决方法:在position:relative的外层父容器加positio ...

  6. margin-top影响父元素定位

    写样式时无意中发现margin-top会影响到父元素的定位,下面是示例: HTML代码: <div class="demo"> <div class=" ...

  7. jquery的相对父元素和相对文档定位示例代码

    在开发jquery时候经常需要用到定位,有相对父元素定位和相对文档定位,本文为此总结下,有需要的朋友可以参考下 在开发jquery时候经常需要用到定位,这里概括两种定位: 1.相对父元素定位: $(& ...

  8. CSS 实现:父元素包含子元素,子元素垂直居中布局

    ☊[实现要求]:父元素包含子元素,子元素垂直居中布局 <div class="demo5"> <div class="child">A& ...

  9. 父元素高度设置为min-height,子元素高度设置为100&percnt;,但实际上子元素高度你知道是多少吗?

    前言 给父元素一个min-height,子元素设置height:100%. 代码 <!DOCTYPE html> <html> <head> <title&g ...

随机推荐

  1. C&num;创建、安装、卸载、调试Windows Service&lpar;Windows 服务&rpar;的简单教程

    前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...

  2. ThinkPHP3&period;2&period;3扩展之生成PDF文件(MPDF)

    目前是PHP生成PDF文件最好的插件了,今天介绍下在ThinkPHP3.2.3里如何使用. 先安照路径放好如图. 下面是使用方法 public function pdf(){ //引入类库 Vendo ...

  3. 前端学习笔记汇总(之merge方法)

    学习笔记 关于Jquery的merge方法 话不多说,先上图 使用jquery时,其智能提示如上,大概意思就是合并first和second两个数组,得到的结果是first+(second去重后的结果) ...

  4. 【python之路6】pycharm的使用

    1.pycharm简介 PyCharm 是我众多python编辑器中比较好的一个.而且可以跨平台,在macos和windows下面都可以用,这点比较好. PyCharm是一种Python IDE,带有 ...

  5. hdu 2104

    #include <iostream> using namespace std; int gcd(int a,int b) { return (b?gcd(b,a%b):a); } int ...

  6. access数据库的连接字符串以及数据库操作类

    <!--access数据库连接方式--> <add name="QYTangConnectionString" connectionString="Pr ...

  7. 你为什么还坚持&period;NET

    C#换什么比较合适? 从TIOBE来看,Java.C++.C.Python都好,对了,还不能忘了JS. Sql Server换什么比较合适? MySql挺好,Oracle也不错,也还有不少选择. 都挺 ...

  8. React Hooks &lpar;React v16&period;7&period;0-alpha&rpar;

    :first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...

  9. PHP性能调优---php-fpm - 启动参数及重要配置详解

    约定几个目录/usr/local/php/sbin/php-fpm/usr/local/php/etc/php-fpm.conf/usr/local/php/etc/php.ini 一,php-fpm ...

  10. Java 8学习之Stream API

    一个Stream表面上看与一个集合很类似,允许你改变和获取数据.但是实际上他与集合是有很大区别的: Stream自己不会存储元素.元素可能被存储在底层的集合中,或者根据需要产生出来. Stream操作 ...