[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
元素内点击文本,就会触发此控件。就是说,当用户选择该标签时,浏览器就会自动将焦点转到和标签相关的表单控件上。

<label> 标签的 for 属性应当与相关元素的 id 属性相同。

通过阴影来弱化背景、通过模糊来弱化背景(遮罩层)

方案一:增加多一个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个缺点:

由于遮罩层的尺寸是与视口相关,而不是与页面相关的,当我们滚动页面时,遮罩层的边缘就露出来了,除非给它加上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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值