很多网站我们看到在屏幕右下角有一个,返回顶部,始终在那儿,还有些网站顶部菜单栏永远也是固定的不动,就是通过今天学习的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. swift3.0 简单直播和简单网络音乐播放器

    本项目采用swift3.0所写,适配iOS9.0+,所有界面均采用代码布局. 第一个tab写的是简单直播,传统MVC模式,第二个tab写的是简单网络音乐播放器.传说MVVM模式(至于血统是否纯正我就不 ...

  2. 【Python】数据库练习-1

    三十四 数据库 1.     查看数据库命令 2.     使用某个数据库 3.     查看当前在哪个库 4.     查看当前数据库中有哪些表 5.     查询表中数据 6.     建库:cr ...

  3. 正则表达式的lastIndex属性

    js中正则表达式的使用方式有两种,一种是正则表达式对象的方法,一种是字符串对象的方法,前者有exec(str).test(str)两个方法,后者有match(regexp).replace(regex ...

  4. react-navigation 使用详解(转载)

    上篇博客和大家分享了关于React Native jsBundle预加载,界面启动优化的内容,详情可点击: 基于最新版本React Native实现JsBundle预加载,界面秒开优化 一.开源库介绍 ...

  5. 高级数据结构及应用 —— 使用 bitmap 进行字符串去重

    bitmap 即为由单个元素为 boolean(0/1, 0 表示未出现,1 表示已经出现过)的数组. 如果C/C++ 没有原生的 boolean 类型,可以用 int 或 char 来作为 bitm ...

  6. [LeetCode&Python] Problem 100. Same Tree

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  7. centos7 firewalld基本使用

    firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status firewalld 停止: systemctl disable ...

  8. 本地ip变化,自定义IP地址

    1.打开网络和internet 设置 2.右键属性 3.修改

  9. javascript json 判断项目 是否存在不存在插入foreach 组合 输出

    var a = []; var i; a.push({ key: "key1", value: 23 }); a.push({ key: "key2", val ...

  10. 《DSP using MATLAB》Problem 6.5

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...