很多网站我们看到在屏幕右下角有一个,返回顶部,始终在那儿,还有些网站顶部菜单栏永远也是固定的不动,就是通过今天学习的position来做的。

在style中加入 positon:fixed;top 0;left 0;right 0;就是固定在顶部

<body>
<div onclick="Top();" style="height: 30px;width: 30px;background: #396764;color: white;
position: fixed;bottom: 0;right: 0;
margin-bottom: 20px;margin-right: 20px;
">top</div>
<div style="height: 3000px;background: #dddddd;">body</div>
<script>
function Top() {
document.documentElement.scrollTop = 0;
}
</script>
</body>

  position 还有两个取值经常配合在一起用,relative,absolute.单独定义一个relative,是没有任何意义的,跟没定义一样。

  relative放在父标签中,它的子标签中才用absolute。作用是子标签的位置是相对于父标签来说的

<div style="width: 500px;height: 200px;border: 1px solid red;margin: 0 auto; position: relative;">
<div style="position: absolute;top: 0;left: 0;width: 30px;height: 30px;background: #000;"></div>
<div style="position: absolute;top: 0;right: 0;width: 30px;height: 30px;background: #000;"></div> <div style="width: 200px;height: 100px;border: 1px solid blue;margin: 0 auto;position: relative;">
<div style="position: absolute;top: 50px;left: 100px;width: 30px;height: 30px;background: #750e60;"></div>
</div>
</div> <div style="width: 500px;height: 200px;border: 1px solid red;margin: 0 auto; position: relative;">
<div style="position: absolute;bottom: -30px;right: -30px;width: 30px;height: 30px;background: #000;"></div>
</div>

  

position 分层固定在屏幕某位置的更多相关文章

  1. 将HTML的页脚固定在屏幕下方

    /********************************************************************* * 将HTML的页脚固定在屏幕下方 * 说明: * 处理的 ...

  2. 设置一个DIV块固定在屏幕中央(两种方法)

    设置一个DIV块固定在屏幕中央(两种方法) 方法一: 对一个div进行以下设置即可实现居中. <style> #a{ position: fixed; top: 0px; left: 0p ...

  3. 完美解决 IE6 position:fixed 固定定位问题

    关于 position:fixed; 属性 生成绝对定位的元素,相对于浏览器窗口进行定位. 元素的位置通过 “left”, “top”, “right” 以及 “bottom” 属性进行规定. pos ...

  4. Swift实现封装PopMenu菜单,可在屏幕任意位置弹出

    效果图: 说明: 代码现已支持 Swift3 语法 使用介绍: 1.初始化位置 //frame 为整个popview相对整个屏幕的位置 箭头距离右边位置,默认15 //popMenu = SwiftP ...

  5. WPF 获得鼠标相对于屏幕的位置,相对于控件的位置

    相对于屏幕的位置 第一步: /// <summary>    /// 用于获得鼠标相对于屏幕的位置    /// </summary>    public class Win3 ...

  6. android 屏幕单击位置测试

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  7. Android 利用代码在屏幕中间位置显示ProgressDialog和ProgressBar

    package cc.testprogressdialog; import android.os.Bundle; import android.view.Gravity; import android ...

  8. WPF中获取鼠标相对于屏幕的位置

    原文:WPF中获取鼠标相对于屏幕的位置 WPF中获取鼠标相对于屏幕的位置                                   周银辉WPF编程时,我们经常使用Mouse.GetPosi ...

  9. 【微信小程序】view顶部固定或底部固定 + scroll-view中的元素view也可以使用position:fixed;固定选中元素位置

    1.顶端固定核心代码如下: <view class="page__hd" style="position:fixed; top:0;width: 750rpx;&q ...

随机推荐

  1. 【Maven】Project configuration is not up-to-date with pom.xml错误解决方法

    导入一个Maven项目之后发现有一个如下的错误: Project configuration is not up-to-date with pom.xml. Run project configura ...

  2. shell统计当前文件夹下的文件个数、目录个数

    1. 统计当前文件夹下文件的个数 ls -l |grep "^-"|wc -l 2. 统计当前文件夹下目录的个数 ls -l |grep "^d"|wc -l ...

  3. 《网页文档/文字复制方法大全》 - imsoft.cnblogs

    <网页文档/文字复制方法大全> 一: 1.首先,找到自己要的文档. 2.文章题目复制,在搜索引擎的框框里输入:site:wenku.baidu.com "题目"/sit ...

  4. 基于hiredis,redis C客户端封装

    项目中需要用到redis就封装了一下,基于hiredis,只封装了string和哈希的部分方法.编译时加入-D__USER_LOCK__添加线程安全. suntelRedisCli.h #ifndef ...

  5. 2019寒假算法基础集训营1 - B 小a与"204"

    题目: 小a非常喜欢这个数字,因为. 现在他有一个长度为的序列,其中只含有这三种数字 设为序列中第个数,你需要重新排列这个数列,使得最大(公式的含义是:每个数与前一个数差的平方的和) 注意:我们默认 ...

  6. Docker第一个应用:Hello World

    Docker应用:Hello World 前言: 最近学习了Docker相关技术点,国内关于Docker的资料大多是基于Linux系统的,但是我对Linux又不熟(实际上没用过,掩面哭笑.Jpg). ...

  7. 【HDOJ3018】【一笔画问题】【欧拉回路+并查集】

    http://acm.hdu.edu.cn/showproblem.php?pid=3018 Ant Trip Time Limit: 2000/1000 MS (Java/Others)    Me ...

  8. D. The Fair Nut and the Best Path 树形dp (终于会了)

    #include<bits/stdc++.h> #define int long long using namespace std; ; int a[maxn]; int dp[maxn] ...

  9. javaweb防止表单重复提交

    参考孤傲苍狼 https://www.cnblogs.com/xdp-gacl/p/3859416.html 场景一:在网络延迟的情况下让用户有时间点击多次submit按钮导致表单重复提交 场景二:表 ...

  10. String的intern()方法详解

    https://blog.csdn.net/soonfly/article/details/70147205  :图解 https://blog.csdn.net/wjzhang5514/articl ...