欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

CSS3實(shí)現(xiàn)多背景模擬動態(tài)邊框的效果

  發(fā)布時間:2016-11-08 17:02:19   作者:佚名   我要評論
前幾天在工作的時候遇到一個問題,項(xiàng)目需要實(shí)現(xiàn)一個效果,當(dāng)鼠標(biāo)移入一個元素的時候,元素出現(xiàn)一個動態(tài)的邊框,通過網(wǎng)上查找資料看到有人推薦可以使用Css3來實(shí)現(xiàn),后來試了試成功了,現(xiàn)在將實(shí)現(xiàn)的方法分享給大家,有需要的朋友們可以參考借鑒。

首先來看看要實(shí)現(xiàn)的效果圖

實(shí)現(xiàn)方法如下

我首先想到的是border屬性,可是border屬性不能設(shè)置長度。如果用border實(shí)現(xiàn),需要用其他元素來模擬,比較麻煩。后來突然想起以前在網(wǎng)上看到有人用CSS3的多背景來模擬邊框,就試了一下。

css3 背景

CSS3對于background做了一些修改,最明顯的一個就是采用設(shè)置多背景,不但添加了4個新屬性,并且還對目前的屬性進(jìn)行了調(diào)整增強(qiáng)。

1、 多個背景圖片

在css3里面,你可以再一個標(biāo)簽元素里應(yīng)用多個背景圖片。代碼類似與css2.0版本的寫法,但引用圖片之間需用“,”逗號隔開。第一個圖片是定位在元素最上面的背景,后面的背景圖片依次在它下面顯示,如下:

background-image: url(top-image.jpg), url(middle-image.jpg), url(bottom-image.jpg);

2、新屬性:Background Clip

此討論讓我們回到文章開始提到的關(guān)于背景被border邊框遮擋的問題。background-clip的添加讓我們完全能夠控制背景顯示的位置。

屬性值如下:

     background-clip: border; 背景在border邊框下開始顯示

     background-clip: padding; 背景在padding下開始顯示,而不是border邊框下開始

     background-clip: content; 背景在內(nèi)容區(qū)域下開始顯示,而不是border邊框下開始或padding下開始。

     background-clip: no-clip; 默認(rèn)屬性值,類似與background-clip: border;

3、新屬性: Background Origin

此屬性需要與background-position配合使用。你可以用background-position計(jì)算定位是從border,padding或content boxes內(nèi)容區(qū)域算起。(類似background-clip)

     background-origin:border; 從border邊框位置算起

     background-origin:padding; 從padding位置算起

     background-origin:content; 從content-box內(nèi)容區(qū)域位置算起;

     background-clip和background-origin的不同之處www.CSS3.info網(wǎng)站給做了很好的分析講解。

4、新屬性:Background Size

Background Size屬性用來重設(shè)你的背景圖片。

有幾個屬性值:

     background-size: contain; 縮小背景圖片使其適應(yīng)標(biāo)簽元素(主要是像素方面的比率)

     background-size: cover; 讓背景圖片放大延伸到整個標(biāo)簽元素大?。ㄖ饕窍袼胤矫娴谋嚷剩?br />

     background-size: 100px 100px; 標(biāo)明背景圖片縮放的尺寸大小

     background-size: 50% 100%; 百分比是根據(jù)內(nèi)容標(biāo)簽元素大小,來縮放圖片的尺寸大小

你可以去CSS 3 specifications站點(diǎn)看一下簡單的案例說明。

5、新屬性:Background Break

css3里標(biāo)簽元素能被分在不同區(qū)域(如:讓內(nèi)聯(lián)元素span跨多行),background-break屬性能夠控制背景在不同區(qū)域顯示。

屬性值:

     Background-break: continuous; 此屬性是默認(rèn)值,忽視區(qū)域之間的間隔空隙(給它們應(yīng)用圖片就好像把它們看成一個區(qū)域一樣)

     Background-break: bounding-box; 重新考慮區(qū)域之間的間隔

     Background-break: each-box; 對每一個獨(dú)立的標(biāo)簽區(qū)域進(jìn)行背景的重新劃分。

6、背景顏色的調(diào)整

     background-color屬性在css3版本里面稍微做了增強(qiáng),除了指定background color背景顏色之外,還可以對不使用的標(biāo)簽元素背景圖片進(jìn)行去色處理。

     background-color: green / blue;此例子里,這背景顏色可能是綠色,然而,如果底部背景圖片無效的話,藍(lán)色將代替綠色來顯示。如果你沒有指定某個顏色的話,它將其視為透明。

7、背景重復(fù)的調(diào)整

css2里當(dāng)設(shè)置背景的時候,它經(jīng)常被標(biāo)簽元素截取而顯示不全,css3介紹了2個新屬性來修復(fù)此問題。 space:圖片以相同的間距平鋪且填充整個標(biāo)簽元素 round:圖片自動縮放直到適應(yīng)且填充整個標(biāo)簽元素

CSS 3 specifications網(wǎng)站對background-repeat: space的使用就是一個現(xiàn)成的例子。

8、Background Attachment 的調(diào)整

Background Attachment有了一個新屬性值:local,當(dāng)標(biāo)簽元素滾動時它才有效(如設(shè)置overflow:scroll;),當(dāng)background-attachment設(shè)置為scroll時,背景圖片是不隨內(nèi)容滾條滾動的?,F(xiàn)在,有了background-attachment:local,就可以做到讓背景隨元素內(nèi)容滾動而滾動了。

css3 多背景模擬元素邊框

我們這里主要使用了background-img、background-size 和 background-position 三個屬性。

background-image: [background-image], [background-image], [background-image]; 
background-position: [background-position], [background-position], [background-position]; 
background-repeat: [background-repeat], [background-repeat], [background-repeat];

簡寫形式如下:

background: [background-image] [background-position] [background-repeat], 
[background-image] [background-position] [background-repeat], 
[background-image] [background-position] [background-repeat];

現(xiàn)在我們用多背景來模擬一個元素的邊框

/*CSS*/
.exammple {
    background: linear-gradient(0, #108b96 2px, #108b96 2px) no-repeat, 
                linear-gradient(-90deg, #108b96 2px, #108b96 2px) no-repeat, 
                linear-gradient(-180deg, #108b96 2px, #108b96 2px) no-repeat, 
                linear-gradient(-270deg, #108b96 2px, #108b96 2px) no-repeat;
    background-size: 100% 2px, 2px 100%, 100% 2px, 2px 100%;
    background-position: left top, right top, right bottom, left bottom;
}
<div class="exammple"></div>

我們用四個漸變的背景來模擬四個邊框(為什么我們要用漸變而不是直接的Color呢?這是由于Css的多背景只能是background-image, background-color不支持多個值,所有即便是純色的邊框,我們也只能使用漸變)。

接下來我們讓邊框動起來

/*CSS*/
.exammple {
    transition: ease-in .3s;
    background: linear-gradient(0, #108b96 2px, #108b96 2px) no-repeat, 
                linear-gradient(-90deg, #108b96 2px, #108b96 2px) no-repeat, 
                linear-gradient(-180deg, #108b96 2px, #108b96 2px) no-repeat, 
                linear-gradient(-270deg, #108b96 2px, #108b96 2px) no-repeat;
    background-size: 0 2px, 2px 0, 0 2px, 2px 0;
    background-position: left top, right top, right bottom, left bottom;
}
.exammple:hover {
    background-size: 100% 2px,  2px 100%, 100% 2px, 2px 100%;
}

相比border屬性,用background的模擬邊框的優(yōu)勢在于可以控制邊框的寬高,運(yùn)動方向等,實(shí)現(xiàn)很多border不能實(shí)現(xiàn)的效果,劣勢在于不能實(shí)現(xiàn)border的圓角。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家學(xué)習(xí)或者使用Css3能有所幫助,如果有疑問大家可以留言交流。

相關(guān)文章

最新評論