网页笑脸(斜眼笑)

今天我们用HTML和CSS实现一个前端笑脸的页面效果,首先看一下效果图。
在这里插入图片描述
看到这个效果图之后,我们来分析一下此页面的布局。
首先最外层有一个最大的div(图中灰色背景盒子),然后在大div的正中间有一个核心的div盒子(图中淡黄色背景盒子以及其边框),在核心div里面又可以大致分成两个区域:上面的两个眼睛(包括眼眶)和下面的一个微笑。
微笑,比较简单,直接用盒子的边框表示(隐藏盒子的上边框)。
眼睛部分,有些难度,需要用定位以及伪类。
由于两只眼睛一模一样,所以我们只需单独看一只眼睛部分。
这个部分又可细分成三个部分:上眼眶,下眼眶以及眼珠。
上眼眶类似微笑,直接用盒子边框表示。
而下眼眶这里用的是定位和after伪类共同实现。(由于上眼眶和下眼眶必须完全相交且曲率又不能相同,所以此处只能用定位来完成,有妙招的读者欢迎评论区留言)
至于眼珠,为了便利,也是用定位实现。
清楚了实现原理,以下来看看具体实现代码:
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>leer</title>
    <link rel="stylesheet" href="leer.css">
</head>
<body>
    <div class="container">
        <div class="face">
            <div class="eyesbox">
                <div class="eyeAbl"></div>
                <div class="eyeLeft">
                    <div class="pupil"></div>
                </div>
                <div class="eyeAbr"></div>
                <div class="eyeRight">
                    <div class="pupil"></div>
                </div>
            </div>
            <div class="smile">
            </div>
        </div>
    </div>
</body>
</html>

CSS部分:

.container {
    width: 60%;
    min-height: 400px;
    background-color: #968f90;
    overflow: auto;
    margin: 0 auto;
    display: flex;
    justify-content: center;
    align-items: center;
}


@keyframes spin {
    to {
        transform: rotate(75deg);
    }
}

.container:hover .pupil {
    animation: spin 3s infinite linear alternate-reverse;
    transform-origin: 45px 60px;
}

.face {
    margin: 0 auto;
    height: 258px;
    width: 250px;
    background-color: rgb(231, 191, 14);
    border: 4px solid rgb(179, 118, 6);
    border-radius: 100%;
    position: relative;
    overflow: hidden;
}

.eyesbox {
    height: 125px;
    width: 250px;
}

.eyeLeft {
    height: 50px;
    width: 100px;
    margin-left: 11px;
    margin-top: 50px;
    border-radius: 100px 100px 0 0;
    border: 4px solid rgb(179, 118, 6);
    display: inline-block;
    background-color: white;
    position: relative;
}

.eyeRight {
    height: 50px;
    width: 100px;
    margin-left: 15px;
    margin-top: 50px;
    border-radius: 100px 100px 0 0;
    border: 4px solid rgb(179, 118, 6);
    display: inline-block;
    background-color: white;
    position: relative;
}

.eyeAbl {
    height: 25px;
    width: 100px;
    display: inline-block;
    margin-top: 75px;
    margin-left: 10px;
    position: absolute;
    overflow: hidden;
    z-index: 1;
}

.eyeAbr {
    height: 25px;
    width: 100px;
    display: inline-block;
    margin-top: 75px;
    margin-left: 15px;
    position: absolute;
    overflow: hidden;
    z-index: 1;
}

.eyeAbl::after,
.eyeAbr::after {
    content: '';
    width: 109px;
    height: 109px;
    border: 4px solid;
    border-color: rgb(179, 118, 6);
    border-radius: 90px;
    display: inline-block;
    margin-left: -8px;
    background-color: rgb(231, 191, 14);
}


.pupil {
    height: 10px;
    width: 10px;
    border-radius: 100%;
    background-color: black;
    margin-top: 26px;
    position: absolute;
}

.smile {
    margin-left: 20px;
    height: 100px;
    width: 200px;
    border-radius: 0 0 150px 150px;
    border: 8px solid rgb(179, 118, 6);
    border-top-style: hidden;
}

* {
    box-sizing: border-box;
}

注:此处为了好看,我还添加了CSS3里面的动画效果。(这里本应该用JS来实现动画效果:眼睛随着鼠标方位的移动而移动)。待到我后续学会了JS后再补上此效果,有兴趣的读者可以自己尝试做做。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值