web前端HTML/CSS_5(简易京东轮播图及position笔记)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./css/reset.css">
    <style>
        /* 设置图片的容器 */
        .img-list{
            width: 590px;
            height: 470px;
            margin: 100px auto;

/* 
            为ul开启相对定位,目的是使ul中的pointer可以相对于ul定位而不是相对于初始包含块(html)去定位
 */
            position: relative;

        }

        /* 设置li */
        .img-list li{
            position: absolute;
        }

        /* 通过修改元素的层级来显示指定的图片 */
        .img-list li:nth-child(4){
            z-index: 1;
        }



        /* 设置导航点的样式 */
        .pointer{
            position: absolute;
            z-index: 9999;
            bottom: 20px;
            left: 40px;
        }

        .pointer a{
            /* 设置元素向左浮动 */
            float: left;
            width: 10px;
            height: 10px;
            margin: 0px 2px;
            border-radius: 50%;
            background-color: rgba(255, 255, 255, .3);
            /* 将背景颜色值设置到内容区,边框和内边距不在有背景颜色 */
            background-clip: content-box;
            border: 2px solid transparent;
        }

        .pointer a.active,
        .pointer a:hover{
            background-color: #fff;
            border: 2px solid  rgba(255, 255, 255, .3);
        }


    </style>
</head>
<body>
    <ul class="img-list">
        <li><a href="javascript:;"><img src="./img/05/1.jpg"></a></li>
        <li><a href="javascript:;"><img src="./img/05/2.jpg"></a></li>
        <li><a href="javascript:;"><img src="./img/05/3.jpg"></a></li>
        <li><a href="javascript:;"><img src="./img/05/4.jpg"></a></li>
        <li><a href="javascript:;"><img src="./img/05/5.jpg"></a></li>
        <li><a href="javascript:;"><img src="./img/05/6.jpg"></a></li>
        <li><a href="javascript:;"><img src="./img/05/7.jpg"></a></li>
        <li><a href="javascript:;"><img src="./img/05/8.jpg"></a></li>

        <div class="pointer">
            <a class="active" href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
            <a href="javascript:;"></a>
        </div>
    </ul>
</body>
</html>

实现的效果为
在这里插入图片描述

1,position简介
定位(position)
- 定位是一种更加高级的布局手段
- 通过定位可以将元素摆放到页面的任意位置
- 使用position属性来设置定位
可选值:
static 默认值,元素是静止的没有开启定位
relative 开启元素的相对定位
absolute 开启元素的绝对定位
fixed 开启元素的固定定位
sticky 开启元素的粘滞定位

                - 相对定位:
                    - 当元素的position属性值设置为relative时则开启了元素的相对定位
                    - 相对定位的特点:
                        1.元素开启相对定位以后,如果不设置偏移量元素不会发生任何的变化
                        2.相对定位是参照于元素在文档流中的位置进行定位的
                        3.相对定位会提升元素的层级
                        4.相对定位不会使元素脱离文档流
                        5.相对定位不会改变元素的性质块还是块,行内还是行内

                - 偏移量(offset)
                    - 当元素开启了定位以后,可以通过偏移量来设置元素的位置
                        top
                            - 定位元素和定位位置上边的距离
                        bottom
                            - 定位元素和定位位置下边的距离

                            - 定位元素垂直方向的位置由top和bottom两个属性来控制
                                通常情况下我们只会使用其中一
                            - top值越大,定位元素越向下移动
                            - bottom值越大,定位元素越向上移动
                        left
                            - 定位元素和定位位置的左侧距离
                        right
                            - 定位元素和定位位置的右侧距离

                            - 定位元素水平方向的位置由left和right两个属性控制
                                通常情况下只会使用一个
                            - left越大元素越靠右
                            - right越大元素越靠左
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body{
            font-size: 60px;
        }
        .box1{
            width: 200px;
            height: 200px;
            background-color: #bfa;
        }

        .box2{
            width: 200px;
            height: 200px;
            background-color: orange;

          
            position: relative;

            left: 100px;
            top: -200px;

        }

        .box3{
            width: 200px;
            height: 200px;
            background-color: yellow;

        }
    </style>
</head>
<body>

    <div class="box1">1</div>
    <div class="box2">2</div>
    <div class="box3">3</div>
    
</body>
</html>

在这里插入图片描述

2,绝对定位

  • 当元素的position属性值设置为absolute时,则开启了元素的绝对定位
    - 绝对定位的特点:
    1.开启绝对定位后,如果不设置偏移量元素的位置不会发生变化
    2.开启绝对定位后,元素会从文档流中脱离
    3.绝对定位会改变元素的性质,行内变成块,块的宽高被内容撑开
    4.绝对定位会使元素提升一个层级
    5.绝对定位元素是相对于其包含块进行定位的

               包含块( containing block )
                   - 正常情况下:
                       包含块就是离当前元素最近的祖先块元素
                       <div> <div></div> </div>
                       <div><span><em>hello</em></span></div>
    
                   - 绝对定位的包含块:
                       包含块就是离它最近的开启了定位的祖先元素,
                           如果所有的祖先元素都没有开启定位则根元素就是它的包含块
    
                   - html(根元素、初始包含块)
    
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body{
            font-size: 60px;
        }
        .box1{
            width: 200px;
            height: 200px;
            background-color: #bfa;
        }

        .box2{
            width: 200px;
            height: 200px;
            background-color: orange;


            position: absolute;
            /* left: 0;
            top: 0; */
            bottom: 0;
            right: 0;
        }

        .box3{
            width: 200px;
            height: 200px;
            background-color: yellow;

        }

        .box4{
            width: 400px;
            height: 400px;
            background-color: tomato;
            position: relative;
        }
        
        .box5{
            width: 300px;
            height: 300px;
            background-color: aliceblue;
            /* position: relative; */
        }
    </style>
</head>
<body>

    <div class="box1">1</div>

    <div class="box4">
        4
        <div class="box5">
            5
            <div class="box2">2</div>
        </div>
    </div>
    <div class="box3">3</div>
    
</body>
</html>

在这里插入图片描述

3,固定定位
固定定位:
- 将元素的position属性设置为fixed则开启了元素的固定定位
- 固定定位也是一种绝对定位,所以固定定位的大部分特点都和绝对定位一样
唯一不同的是固定定位永远参照于浏览器的视口进行定位
固定定位的元素不会随网页的滚动条滚动

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body{
            font-size: 60px;
            height: 2000px;
        }
        .box1{
            width: 200px;
            height: 200px;
            background-color: #bfa;
        }

        .box2{
            width: 200px;
            height: 200px;
            background-color: orange;
          

            position: fixed;

            left: 0;
            top: 0;
        }

        .box3{
            width: 200px;
            height: 200px;
            background-color: yellow;

        }

        .box4{
            width: 400px;
            height: 400px;
            background-color: tomato;

        }
        
        .box5{
            width: 300px;
            height: 300px;
            background-color: aliceblue;
        }
    </style>
</head>
<body>

    <div class="box1">1</div>

    <div class="box4">
        4
        <div class="box5">
            5
            <div class="box2">2</div>

        </div>
    </div>

    <div class="box3">3</div>

    
</body>
</html>

在这里插入图片描述

4,粘滞定位
粘滞定位
- 当元素的position属性设置为sticky时则开启了元素的粘滞定位
- 粘滞定位和相对定位的特点基本一致,
不同的是粘滞定位可以在元素到达某个位置时将其固定

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>导航条</title>
    <link rel="stylesheet" href="./css/reset.css">
    <style>

        body{
            height: 3000px;
        }
        
        /* 设置nav的大小 */
        .nav{

            /* 设置宽度和高度 */
            width: 1210px;
            height: 48px;
            /* 设置背景颜色 */
            background-color: #E8E7E3;

            margin:100px auto;
             position: sticky;
             top: 10px;

        }

        /* 设置nav中li */
        .nav li{
            /* 设置li向左浮动,已使菜单横向排列 */
            float: left;
            /* 设置li的高度 */
            /* height: 48px; */
            /* 将文字在父元素中垂直居中 */
            line-height: 48px;

        }

        /* 设置a的样式 */
        .nav a{
            /* 将a转换为块元素 */
            display: block;
            /* 去除下划线 */
            text-decoration: none;
            /* 设置字体颜色 */
            color: #777777;
            /* 修改字体大小 */
            font-size: 18px;

            padding: 0 39px;
        }

        .nav li:last-child a{
            padding: 0 42px 0 41px;
        }

        /* 设置鼠标移入的效果 */
        .nav a:hover{
            background-color: #3F3F3F;
            color: #E8E7E3;
        }
    </style>
</head>

<body>
    <!-- 创建导航条的结构 -->
    <ul class="nav">
        <li>
            <a href="#">HTML/CSS</a>
        </li>
        <li>
            <a href="#">Browser Side</a>
        </li>
        <li>
            <a href="#">Server Side</a>
        </li>
        <li>
            <a href="#">Programming</a>
        </li>
        <li>
            <a href="#">XML</a>
        </li>
        <li>
            <a href="#">Web Building</a>
        </li>
        <li>
            <a href="#">Reference</a>
        </li>
    </ul>

</body>

</html>

注意看滚动条
在这里插入图片描述
在这里插入图片描述

当滚动条往下拉时,顶部导航栏一直不动
5,绝对定位元素的布局
水平布局
left + margin-left + border-left + padding-left + width + padding-right + border-right + margin-right + right = 包含块的内容区的宽度

            - 当我们开启了绝对定位后:
                水平方向的布局等式就需要添加left 和 right 两个值
                    此时规则和之前一样只是多添加了两个值:
                        当发生过度约束:
                            如果9个值中没有 auto 则自动调整right值以使等式满足
                            如果有auto,则自动调整auto的值以使等式满足

                    - 可设置auto的值
                        margin width left right

                    - 因为left 和 right的值默认是auto,所以如果不指定left和right
                        则等式不满足时,会自动调整这两个值

                垂直方向布局的等式的也必须要满足
                    top + margin-top/bottom + padding-top/bottom + border-top/bottom + height = 包含块的高度

6,元素的层级
对于开启了定位元素,可以通过z-index属性来指定元素的层级
z-index需要一个整数作为参数,值越大元素的层级越高
元素的层级越高越优先显示

                如果元素的层级一样,则优先显示靠下的元素

                祖先的元素的层级再高也不会盖住后代元素 
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body{
            font-size: 60px;
        }
        .box1{
            width: 200px;
            height: 200px;
            background-color: #bfa;
            position: absolute;
            /* 
                对于开启了定位元素,可以通过z-index属性来指定元素的层级
                    z-index需要一个整数作为参数,值越大元素的层级越高
                        元素的层级越高越优先显示

                    如果元素的层级一样,则优先显示靠下的元素

                    祖先的元素的层级再高也不会盖住后代元素
             */
             /* z-index: 3; */
        }

        .box2{
            width: 200px;
            height: 200px;
            background-color: rgba(255 , 0, 0, .3);
            position: absolute;
            top: 50px;
            left: 50px;
            /* z-index: 3; */

        }

        .box3{
            width: 200px;
            height: 200px;
            background-color: yellow;
            position: absolute;
            top: 100px;
            left: 100px;
            z-index: 3;

        }

        .box4{
            width: 100px;
            height: 100px;
            background-color: orange;
            position: absolute;
        }
    </style>
</head>
<body>

    <div class="box1">1</div>
    <div class="box2">2</div>
    <div class="box3">3
        <div class="box4">4</div>
    </div>

    
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值