文章出處

前面的話

  之前的博客文章已經詳細介紹過絕對定位的基礎知識,由于它的用途實在廣泛,于是單獨為其寫這篇關于其應用的博客關于絕對定位的基礎知識移步至此

 

靜態位置

  當元素絕對定位后,若該元素的格式化屬性不發生變化,則該元素處于靜態位置。關于絕對定位元素格式化的相關內容移步至此。元素的靜態位置是指元素在正常流中原本的位置,更確切的講,頂端的靜態位置是從包含塊的上邊界到假想框的上外邊距邊界之間的距離。假想框是假設元素position屬性為static時元素的第一個框。

應用

  以下是基于絕對定位靜態位置的應用

跟隨圖標

  圖標使用不依賴定位父級的absolute和margin屬性進行定位,這樣,當文本的字符個數改變時,圖標的位置可以自適應

div{
    height: 20px;
    width: 500px;
    line-height: 20px;
    margin-bottom: 30px;
}    
i{
    position: absolute;
    width: 28px;
    height: 11px;
    margin: -6px 0 0 2px;
    background: url('http://sandbox.runjs.cn/uploads/rs/26/ddzmgynp/hot.gif');
}
<div>長度可變文字<i></i></div>

視頻提示

  一般在視頻圖片上的邊角上都會有"自制"、"最新"、"1080p"等諸如此類的提示。使用不依賴的絕對定位屬性,可以讓父級元素不設置relative,拓展性更強

i{
    position: absolute;
    width:40px;
    text-align: center;
    height: 18px;
    line-height: 18px;
    font-style: normal;
    background-color: orange;
    color: white;
    padding: 2px;
}    
.box{
    height: 200px;
    width: 200px;
    border: 2px solid gray;
}
.in{
    width: 100%;
    height: 100%;
    line-height: 100px;
    background-color: pink;
    display:inline-block;
}
.rt{
    margin-left: -44px;
}
.lb{
    margin-top: -22px;
}
.rb{
    float: right;
    margin-top: -22px;
    margin-left: -44px;
}
<div class="box">
    <i class="lt">自制</i>
    <div class="in">測試內容</div><!--
    --><i class="rt">獨家</i>
    <i class="lb">1080p</i>
    <span style="width: 100%;display:inline-block"></span><!--
    --><i class="rb">最新</i>
</div>

下拉菜單

  一般地,下拉菜單作為一個組件需要使用在各種場景中,如果給組件添加relative屬性,則降低了其利用率

body{
    margin: 0;
}    
ul{
    margin: 0;
    padding: 0;
    list-style: none;
}
input{
    padding: 0;
    border: 0;
}
.box{
    width: 200px;
    height: 38px;
    border: 2px solid gray;
}
.con{
    overflow: hidden;
}
.input{
    float: left;
    width: 160px;
    height: 38px;
}
.search{
    width: 38px;
    height: 38px;
    float: right;
    background: url('http://sandbox.runjs.cn/uploads/rs/26/ddzmgynp/search.png') 0 -38px;
}
.list{
    display:none;
    position: absolute;
    width: 158px;
    border: 1px solid #e6e8e9; 
    overflow: hidden;
}
.in{
    line-height: 30px;
    border-bottom: 1px solid lightblue;
    cursor:pointer;
    text-indent: 1em;
}
.in:hover{
    background-color: #f9f9f9;
}
<div class="box">
    <div class="con">
        <input class="input" id="input">
        <a href="javascript:;" class="search"></a>
    </div>
    <ul class="list" id="list">
        <li class="in">選項一</li>
        <li class="in">選項二</li>
        <li class="in" style="margin-bottom: -1px">選項三</li>
    </ul>        
</div>
input.onfocus = function(){
    list.style.display = 'block';
}
input.onblur = function(){
    list.style.display = 'none';
}

邊緣對齊

  很多網站都使用了邊緣對齊,但好多都是用頁面寬度計算出來的,當寬度變化時需要重新計算。而無依賴的絕對定位利用靜態位置,無需計算就可將其位置確定,且拓展性好

body{
    margin: 0;
}
ul{
    margin: 0;
    padding: 0;
    list-style: none;
}
.box{
    width: 200px;
    height: 100px;
    border: 2px solid black;
    background-color: lightgreen;
}    
.out{
    text-align: right;
}
.list{
    position: absolute;
    margin: 10px 0 0 2px;
    display: inline-block;
}
.in{
    text-align: center;
    width: 20px;
    line-height: 20px;
    margin-top: 4px;
    background-color: pink;
    border-radius: 50%;
}
<div class="box">
    <div class="out">
        <!-- 對于safari瀏覽器需要添加空格&nbsp;來觸發右對齊,其他瀏覽器則不需要-->
        &nbsp;
        <ul class="list">
            <li class="in"></li>
            <li class="in"></li>
            <li class="in"></li>
        </ul>        
    </div>
</div>

星號

  在很多注冊或登錄頁面中,存在用*表示的必填項。*和*號對齊,文字和文字對齊。這種情況使用靜態位置的絕對定位比較合適

body{
    margin: 0;
}
ul{
    margin: 0;
    padding: 0;
    list-style: none;
}
i{
    font-style: normal;
    color: red;
    position:absolute;
    margin-left: -10px;
}
.list{
    width: 100px;
    padding-left: 20px;
    border: 2px solid black;
    line-height: 2;
}
<ul class="list">
    <li class="in">
        <i>*</i><span>手機號</span>
    </li>
    <li class="in">
        <span>用戶名</span>
    </li>
    <li class="in">
        <i>*</i><span>密碼</span>
    </li>
</ul>

 

偏移屬性

  當使用偏移屬性時,絕對定位元素將相對于包含塊進行定位。一般地,我們僅僅使用偏移屬性中的兩個,且這兩個屬性不對立。但實際上,對立的偏移屬性如left和right可以同時使用,甚至4個偏移屬性都可以同時使用,并且可以達到一些意想不到的效果。以下基于絕對定位偏移屬性的應用

應用

全屏自適應

  實現一個距離屏幕右側200px的全屏自適應的容器層

.box{
    position: absolute;
    top: 0;
    left: 0;
    right: 200px;
    bottom: 0;
    background-color: pink;
}
<div class="box"></div>

左右半區翻圖

  一些選項卡中存在左右半區的翻圖效果,點擊左覆蓋區切換到上一張圖片,點擊右覆蓋區切換到下一張圖片

ul{
    margin: 0;
    padding: 0;
    list-style: none;
}
.box{
    position: relative;
    width: 300px;
    height: 200px;
    border: 2px solid lightgray;
    text-align: center;
    font: 40px/200px '宋體';
    color: white;
    overflow: hidden;
}
.list{
    position: absolute;
    width: 400%;
    left: 0;
    top: 0;
    bottom: 0;
    transition: left 1s;
}
.in{
    float: left;
    width: 25%;
    background-color: lightgreen;
}
.l,.r{
    position: absolute;
    opacity: 0;
    top: 0;
    bottom: 0;
    background-color: rgba(0,0,0,0.1);
    cursor: pointer;
}
.l{
    left: 0;
    right: 50%;
}
.r{
    left: 50%;
    right: 0;
}
.l:hover,.r:hover{
    opacity: 1;
    transition: 1s;
}
<div class="box">
    <ul class="list" id="list">
        <li class="in">第1個</li>
        <li class="in">第2個</li>
        <li class="in">第3個</li>
        <li class="in">第4個</li>
    </ul>
    <div class="l" id="l">&lt;</div>
    <div class="r" id="r">&gt;</div>
</div>
var index = 0;
var children = list.children;
l.onclick = function(){
    if(index > 0){
        index --;
        move(index);
    }
}
r.onclick = function(){
    if(index < children.length -1){
        index++;
        move(index);
    }
}
function move(index){
    list.style.left = '-' + index*100 + '%';
}

九宮格

  利用絕對定位的偏移屬性可以制作寬高自適應的九宮格效果

ul{
    margin: 0;
    padding: 0;
    list-style: none;
}    
.list{
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
}
.in{
    position: relative;
    float: left;
    height: 33.3%;
    width: 33.3%;
    background-color: pink;
}
.in:before{
    content: '';
    position: absolute;
    left: 10px;
    right: 10px;
    top: 10px;
    bottom: 10px;
    background-color: lightblue;
    border-radius: 10px;
}
.in:after{
    content: attr(data-value);
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    height: 30px;
    margin: auto;
    text-align: center;
    font:bold 24px/30px  '宋體';
}
<ul class="list">
    <li class="in" data-value='1'></li>
    <li class="in" data-value='2'></li>
    <li class="in" data-value='3'></li>
    <li class="in" data-value='4'></li>
    <li class="in" data-value='5'></li>
    <li class="in" data-value='6'></li>
    <li class="in" data-value='7'></li>
    <li class="in" data-value='8'></li>
    <li class="in" data-value='9'></li>
</ul>

等高布局

  利用overflow清除浮動的BFC的包裹性,形成一個看似等高的布局,再利用絕對定位模擬出背景和間隔線

.box{
    width: 80%;
    margin: auto;
    border: 1px solid gray;
    overflow: hidden;
    position: relative;
    background-color: lightgreen;
}
.l{
    box-sizing:border-box;
    float: left;
    width: 25%;
    position: relative;
}
.r{
    box-sizing:border-box;
    float: right;
    width: 75%;
    padding: 10px;
    height: 100%;
}
.con{
    position: absolute;
    background-color: lightblue;
    border-right: 1px solid #ccc;
    height: 9999px;
    width: 100%;
}
.show{
    padding: 10px;
    position: relative;
}
<div class="box">
    <div class="l">
        <div class="con"></div>
        <div class="show">測試文字<br>測試文字<br></div>
    </div>
    <div class="r">測試文字<br>測試文字<br>測試文字<br></div>
</div>

整體布局

  整體布局的思路就是利用絕對定位元素的偏移屬性來替代固定定位,首先讓<page>元素滿屏起到<body>元素的作用,然后各個模塊各居其位。如果有其他的一些整體的頁面遮罩,則與<page>元素平級

html,body{
    height: 100%;
}
body{
    margin: 0;
}
.page{
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}
header,footer{
    position: absolute;
    left: 0;
    right: 0;
    height: 50px;
}
header{
    top: 0;
    background-color: lightgreen;
}
footer{
    bottom: 0;
    background-color: lightcoral;
}
aside{
    position: absolute;
    left: 0;
    top: 50px;
    bottom: 50px;
    width: 250px;
    background-color: lightblue;
}
.content{
    position: absolute;
    top: 50px;
    bottom: 50px;
    left: 250px;
    right: 0;
    overflow: auto;
    background-color: pink;
}
<div class="page">
    <div class="content">
        <div style="height: 1000px">內容區</div>
    </div>
    <aside>側邊欄</aside>
    <header>頭部</header>
    <footer>底部</footer>
</div>


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()