[CSS]《CSS揭祕》第六章——用戶體驗

選用合適的鼠標光標

#testdiv{
    cursor:not-allowed;
    /*
    cursor:none;//例如視頻界面隱藏鼠標
    */
}

擴大可點擊區域

.testdiv{
    width: 100px;
    height: 40px;
    position: relative;
    cursor: move;
}
.testdiv::before{
    content: '';
    position: absolute;
    top: -10px;
    left: -10px;
    right: 10px;
    bottom: 10px;
    cursor: move;
}

利用僞元素。瀏覽器

自定義複選框

<div class="testdiv">
    <input type="checkbox" id="awesome">
    <label for="awesome">Awesome</label>
</div>

input[type="checkbox"]+label::before{
    content: '\a0';
    display: inline-block;
    vertical-align: .2em;  //vertical-align 屬性設置元素的垂直對齊方式
    width: .8em;
    height: .8em;
    margin-right: .2em;
    border-radius: .2em;
    background: silver;
    text-indent: .15em;  //text-indent 屬性規定文本塊中首行文本的縮進。
    line-height: .65;
}

input[type="checkbox"]:checked+label::before{
    content: '\2713';
    background: yellowgreen;
}

input[type="checkbox"]{
    position: absolute;
    clip: rect(0,0,0,0)   //clip 屬性剪裁絕對定位元素。
}

clipboard.png

定義和用法 <label> 標籤爲 input 元素定義標註(標記)。

label 元素不會向用戶呈現任何特殊效果。不過,它爲鼠標用戶改進了可用性。若是您在 label
元素內點擊文本,就會觸發此控件。就是說,當用戶選擇該標籤時,瀏覽器就會自動將焦點轉到和標籤相關的表單控件上。spa

<label> 標籤的 for 屬性應當與相關元素的 id 屬性相同。3d

經過陰影來弱化背景、經過模糊來弱化背景(遮罩層)

方案一:增長多一個HTML元素

.overlay{
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0, 0, 0, .8);
}
main{
    background: blueviolet;
}
.lightbox{
    position: absolute;
    z-index: 1;
    width: 200px;
    height: 100px;
    border: 1px solid red;
    background: yellowgreen;
}



<main><p>the backgroung</p></main>
<div class="overlay"></div>
<div class="lightbox">
    <p>the lightbox</p>
</div>

clipboard.png

方案二:經過增長僞元素來取代多餘的HTML元素

方案三:box-shadow方案

box-shadow: 0 0 0 50vmax rgba(0,0,0,.8);

*:有2個缺點:code

因爲遮罩層的尺寸是與視口相關,而不是與頁面相關的,當咱們滾動頁面時,遮罩層的邊緣就露出來了,除非給它加上position: fixed;
boxshadow 並無這種能力,所以它只能在視覺上起到引導注意力的做用,沒法阻止鼠標交互。

方案4:經過模糊來弱化背景

main.de-emphasized {
    filter: blur(5px);
    word-wrap: break-word;
}

clipboard.png

滾動提示

.testdiv{
    margin: 200px;
    overflow: auto;
    width: 10em;
    height: 8em;
    
    border: 1px solid silver;

    background: linear-gradient(white 15px,rgba(255,255,255,0)) ,
                radial-gradient(at top,rgba(0,0,0,.2),transparent 70%) ,
                linear-gradient(to top,white 15px,rgba(255,255,255,0)) ,
                radial-gradient(at bottom,rgba(0,0,0,.2),transparent 70%);
    background-position: 0 0,0 0,100% 100%,100% 100%;
    background-size: 100% 50px,100% 15px,100% 50px,100% 15px;

    
    background-repeat: no-repeat;
    
    background-attachment: local,scroll,local,scroll;
}

clipboard.png

交互式的圖片對比控件

CSS resize方案

.testdiv{
    position: relative;
    display: inline-block;
}
.testdiv>div{
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    width: 50%;
    overflow: hidden;
    resize: horizontal;
    max-width: 100%;
}
.testdiv>div::before{
    content: '';
    position: absolute;
    bottom: 0;
    right: 0;
    width: 12px;
    height: 12px;
    padding: 5px;
    background: linear-gradient(-45deg,white 50% ,transparent 0);
    background-clip: content-box;
    cursor: ew-resize;
}
.testdiv img{
    display: block;
    user-select: none;
}

<div class="testdiv">
    <div>
        <img src="./2.jpg" alt="before">
    </div>
    <img src="./2_1.jpg" alt="after">
</div>

clipboard.png

範圍輸入控件方案

function change() {
    var x=document.getElementById("range").value;
    document.getElementById("beImg").style.width=x+"%";
}


#testdiv{
    position: relative;
    display: inline-block;
}
#testdiv>div{
    position: absolute;
    top: 0;
    left: 0;
    width: 50%;
    overflow: hidden;
    margin: 0;
    
}

#testdiv img{
    display: block;
    user-select: none;
}
#range{
    position: absolute;
    left: 0;
    bottom: 10px;
    width: 50%;
    margin: 0;
    filter: contrast(.5);
    mix-blend-mode: luminosity;
    transform: scale(2);
    transform-origin: left bottom;
}

clipboard.png

相關文章
相關標籤/搜索