分享Css3設計—純Css實現超酷的iphone玻璃氣泡效果

來源:互聯網
上載者:User

demo地址:http://xueduany.github.com/KitJs/KitJs/index.html#bubble

號外:kitjs官方討論QQ群建立了,QQ群號88093625,歡迎大家加入,討論前端相關話題

最近做手機項目時候,需要實現一個類似iphone SMS效果的氣泡效果。

這裡分享下實現心得,

 

首先分析下iphone的氣泡效果有一下特點

1. 圓角

2. 向下的外陰影

3. 上邊和下邊的內陰影

4. 上邊內的一個內嵌的玻璃氣泡的反光效果

 

首先定義一個容器,盒模型為display: inline-block,方便自適應文字大小

.bubble {
    position: relative;
    display: inline-block;
    min-width: 30px;
    max-width: 200px;
    word-break: break-all;
    word-wrap: break-word;
    min-height: 22px;
    background: #d2d2d2;
    border-radius: 15px;
    margin-bottom: 20px;
    padding: 6px 8px;
    -webkit-box-shadow: 0px 1px 2px #000, inset 0px 4px 4px rgba(0,0,0,.3), inset 0px -4px 4px rgba(255,255,255,.5);
    -moz-shadow: 0px 1px 2px #000, inset 0px 4px 4px rgba(0,0,0,.3), inset 0px -4px 4px rgba(255,255,255,.5);
    box-shadow: 0px 1px 2px #000, inset 0px 4px 4px rgba(0,0,0,.3), inset 0px -4px 4px rgba(255,255,255,.5);
}

設定斷詞,避免文字過長,撐開容器,同時設定最小寬度,最大寬度

設定圓角,border-radius

設定box-shadow: 0px 1px 2px #000實現氣泡的外陰影

inset 0px 4px 4px rgba(0,0,0,.3)為上邊框內陰影

inset 0px -4px 4px rgba(255,255,255,.5)為下邊框的內陰影

 

接下來,我們需要實現最後一個效果內嵌玻璃氣泡的反光效果

.bubble .content {
    position: relative;
    padding: 0 4px;
}
.bubble .content:before {
    content: '';
    position: absolute;
    margin: auto;
    top: -5px;
    left: 0;
    width: 100%;
    height: 12px;
    background-image: -webkit-linear-gradient(top, rgba(255,255,255,1) 0%, rgba(255,255,255,0.2) 90%, rgba(255,255,255,0) 90% );
    background-image: -moz-linear-gradient(top, rgba(255,255,255,1) 0%, rgba(255,255,255,0.2) 90%, rgba(255,255,255,0) 90% );
    border-radius: 10px
}

 

在氣泡內嵌一個顯示內容的block,使用block的before虛擬元素,實現一個圓角的漸層氣泡

background-image: -webkit-linear-gradient(top, rgba(255,255,255,1) 0%, rgba(255,255,255,0.2) 90%, rgba(255,255,255,0) 90% );

最後,通過氣泡的before和after虛擬元素,實現三角

.bubble:before {
    content: '';
    display: block;
    font-size: 0;
    width: 0;
    height: 0;
    border-width: 6px;
    position: absolute;
    bottom: -12px;
    left: 12px;
    border-color: #4a4c50 transparent transparent #4a4c50;
    border-style: solid dashed dashed solid;
}
.bubble:after {
    content: '';
    display: block;
    font-size: 0;
    position: absolute;
    bottom: -9px;
    left: 13px;
    width: 0;
    height: 0;
    border-width: 5px;
    border-color: #e8e8e8 transparent transparent #e8e8e8;
    border-style: solid dashed dashed solid;
}

 

最終:

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.