很多网站我们看到在屏幕右下角有一个,返回顶部,始终在那儿,还有些网站顶部菜单栏永远也是固定的不动,就是通过今天学习的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. c++ 中:和::理解

    1.冒号(:)用法 (1)表示机构内位域的定义(即该变量占几个bit空间) typedef struct _XXX{ unsigned char a:4; unsigned char c; } ; X ...

  2. Power BI与Tableau基于Google搜索上的比较

    在数据分析领域里,不少的数据爱好者都会关心什么数据分析产品最好用?最重要的是,很多的企业也特别希望员工能真正知道如何使用这些BI平台以确保公司的投资是值得.同类的文章,小悦也曾发布过,可参考最近< ...

  3. arm svc

    隐藏exit,ptrace etc. C示例 #include <sys/ptrace.h> #include <stdio.h> int main() { int r; as ...

  4. SDM(Supervised Descent Method and its Applications to Face Alignment )

    sdm SDM 人脸对齐的核心内容很简单,就是特征到偏移量的映射:                                           Ix = R I 是特征,x是映射矩阵,R是偏移 ...

  5. JavaBasic_10

    JVM中的对一个对象的所有初始化动作,是javac在字节码文件中帮我们生成的 1.成员变量的显式初始化 2.初始化代码块(构造代码块) 3.构造方法 初始化静态的东西 在Java中,一个没有方法体的方 ...

  6. 给电脑换源 npm 国内镜像 cnpm

    (1)通过 config 配置指向国内镜像源 npm config set registry http://registry.cnpmjs.org //配置指向源 npm info express   ...

  7. 数据库设计画图工具powerdesigner

    powerdesigner 教程:http://jingyan.baidu.com/article/bea41d43684fa4b4c51be6cf.html

  8. Nginx + php-fpm

    www.example.com | | Nginx | | 路由到www.example.com/index.php | | 加载nginx的fast-cgi模块 | | fast-cgi监听127. ...

  9. C++学习(三十)(C语言部分)之 栈和队列

    数据结构1.保存数据 2.处理数据数组+操作增查删改 栈和队列是一种操作受限的线性表 栈 是先进后出 是在一端进行插入删除的操作--->栈顶 另一端叫做栈底(栈和栈区是两个概念)(是一种数据结构 ...

  10. python------面向对象介绍之经典类与新式类的继承顺序

    一. 经典类与新式类的继承顺序 1 class A: def __init__(self): print("A") class B(A): def __init__(self): ...