CSS絕對定位的應用

来源:http://www.cnblogs.com/xiaohuochai/archive/2016/03/25/5315942.html
-Advertisement-
Play Games

× 目錄 [1]跟隨圖標 [2]視頻提示 [3]下拉菜單[4]邊緣對齊[5]星號 [6]全屏適應[7]半區翻圖[8]九宮格[9]等高佈局[10]整體佈局 前面的話 之前的博客文章已經詳細介紹過絕對定位的基礎知識,由於它的用途實在廣泛,於是單獨為其寫這篇關於其應用的博客。關於絕對定位的基礎知識移步至此 ...


×
目錄
[1]跟隨圖標 [2]視頻提示 [3]下拉菜單[4]邊緣對齊[5]星號 [6]全屏適應[7]半區翻圖[8]九宮格[9]等高佈局[10]整體佈局

前面的話

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

 

靜態位置

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

應用

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

【1】自適應位置的跟隨圖標

  圖標使用不依賴定位父級的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>

【2】視頻圖片上的小圖標提示

  一般在視頻圖片上的邊角上都會有"自製"、"最新"、"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>

【3】下拉菜單

  一般地,下拉菜單作為一個組件需要使用在各種場景中,如果給組件添加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';
}

【4】邊緣對齊

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

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">
        <ul class="list">
            <li class="in"></li>
            <li class="in"></li>
            <li class="in"></li>
        </ul>        
    </div>
</div>

【5】星號

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

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個偏移屬性都可以同時使用,並且可以達到一些意想不到的效果。以下基於絕對定位偏移屬性的應用

應用

【1】全屏自適應

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

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

【2】左右半區翻圖

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

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 + '%';
}

【3】九宮格

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

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>

【4】等高佈局

  利用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>

【5】整體佈局

  整體佈局的思路就是利用絕對定位元素的偏移屬性來替代固定定位,首先讓<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>


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • join() 函數 join()函數是將兩個列表連接合併成一個列表。 >>join(10px 20px, 30px 40px) (10px 20px 20px 40px) >>join((blue,red),(#abc,#def)) (#0000ff,#ff0000,#aabbcc,#ddeeff) ...
  • Merge是一個非常有用的功能,類似於Mysql里的insert into on duplicate key. Oracle在9i引入了merge命令, 通過這個merge你能夠在一個SQL語句中對一個表同時執行inserts和updates操作. 當然是update還是insert是依據於你的指定 ...
  • js中不允許出現“ - ” 頁面中改變文字大小-案例: class 點擊按鈕變成覆選框checkbox 改變DIV的浮動 判斷註意事項 所有的相對路徑都別拿來做判斷。。。 img src href="css.css" 絕對路徑可以: img src="http://........jpg" 顏色值不 ...
  • 出處:http://blog.csdn.net/dyllove98/article/details/8957232 CSS3中和動畫有關的屬性有三個 transform、 transition 和 animation。下麵來一一說明: transform 從字面來看transform的釋義為改變,使 ...
  • 1:基本雛形 <html><head> <meta charset="UTF-8"> <title></title></head><body><!--標題標簽--> <h1>11111111111111111</h1> <h2>11111111111111111</h2> <h3>111111111 ...
  • 今天編碼時遇到一個問題,通過後臺查詢的數據設置前端checkbox的選中狀態,設置選中狀態為.attr('checked','true');沒有問題,但是當數據重新載入時,checkbox應清空即所有checkbox為未選中狀態,使用.attr('checked','false');無效果,且全部為 ...
  • 一.冒泡排序 二.選擇排序 三.插入排序 四.希爾排序 五.歸併排序 六.快速排序 ...
  • 搜集了一下網上的資源和自己看過的一些書,小小總結了一波HTTP,現在也只是很膚淺的瞭解,期望以後深入理解後能寫出更有營養的筆記。 HTTP協議的主要特點 + 支持客戶/伺服器模式。+ 簡單快速:客戶向伺服器請求服務時,只需傳送請求方法和路徑。請求方法常用的有GET、HEAD、POST。每種方法規定了 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...