純css實(shí)現(xiàn)的顏色扇附圖
發(fā)布時(shí)間:2014-03-20 15:27:51 作者:佚名
我要評論

一個(gè)純css實(shí)現(xiàn)的顏色扇,效果還不錯(cuò),大家可以看到下面的圖片
今天我們實(shí)現(xiàn)一個(gè)純css實(shí)現(xiàn)的顏色扇,繼續(xù)學(xué)習(xí)sass的使用,效果見下圖所示。
html文件
<div id="container">
<div class="item it1" title="pick a color">
<div class="dot"></div>
</div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
然后是css文件,使用scss、prefire free和css reset。
/*
變量聲明
numOfItem,定義扇條數(shù)量
degreeOfFan,定義扇形角度
*/
$numOfItem:20;
$degreeOfFan:180deg;
body{
background-color: #000000;
}
/* 定義容器樣式,我們的hover事件將要添加在容器上,所以一定不能脫離標(biāo)準(zhǔn)流 */
#container{
width:580px;
height:300px;
position: relative;
}
/*
扇條的樣式
transform-origin非常重要,旋轉(zhuǎn)中心(應(yīng)該在.dot中心)
分別給不同的屬性定義不同的transition持續(xù)時(shí)間
*/
.item{
position: absolute;
left:50%;
top:100%;
width:300px;
height:40px;
border-radius:10px 10px 20px 10px;
transition:all .5s,transform 1s ease-in,;
transform-origin:22px 22px;
}
/* 扇條hover樣式 */
.item:hover{
width:336px;
border-radius:10px 10px 10px 10px;
cursor: pointer;
}
/*
設(shè)置扇條中的文字樣式,利用偽對象實(shí)現(xiàn)
*/
.item:after{
position: absolute;
right:10px;
top:0;
line-height: 40px;
color:#FFF;
}
.item:nth-child(1):before{
content:attr(title);
position: absolute;
right:90px;
top:0;
line-height: 40px;
color:#FFF;
}
/* 旋轉(zhuǎn)中心的樣式 */
.dot{
position: absolute;
left:15px;
top:15px;
border-radius:15px;
height:10px;
width:10px;
background-color:#333333;
border:4px #777777 solid;
z-index:100;
}
/*
關(guān)鍵代碼----
通過循環(huán)給不同的扇條添加樣式
*/
@for $i from 1 through $numOfItem{
//通過循環(huán)給不同的扇條增加樣式
//z-index,改變疊放次序
//bgc,設(shè)置不同的顏色
//通過:after偽對象來放置顏色文本
.item:nth-child(#{$i}){
z-index:100-$i;
background-color: hsl(360*($numOfItem - $i)/($numOfItem - 1),50%,50%);
&:after{
content:"#{hsl(360*($numOfItem - $i)/($numOfItem - 1),50%,50%)}";
}
}
//通過循環(huán)給不同的扇條增加樣式
//hover之后,旋轉(zhuǎn)扇條
//當(dāng)旋轉(zhuǎn)角度超過角度之后,旋轉(zhuǎn)文字
#container:hover .item:nth-child(#{$i}){
transform:rotate($degreeOfFan*($i - $numOfItem)/$numOfItem);
&:after,&:before{
@if($degreeOfFan * ($i - $numOfItem)/$numOfItem < -90deg){
transform:rotate(180deg);
}}
}
}
完工,注釋比較完善,原理不再贅述。

html文件
復(fù)制代碼
代碼如下:<div id="container">
<div class="item it1" title="pick a color">
<div class="dot"></div>
</div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
然后是css文件,使用scss、prefire free和css reset。
復(fù)制代碼
代碼如下:/*
變量聲明
numOfItem,定義扇條數(shù)量
degreeOfFan,定義扇形角度
*/
$numOfItem:20;
$degreeOfFan:180deg;
body{
background-color: #000000;
}
/* 定義容器樣式,我們的hover事件將要添加在容器上,所以一定不能脫離標(biāo)準(zhǔn)流 */
#container{
width:580px;
height:300px;
position: relative;
}
/*
扇條的樣式
transform-origin非常重要,旋轉(zhuǎn)中心(應(yīng)該在.dot中心)
分別給不同的屬性定義不同的transition持續(xù)時(shí)間
*/
.item{
position: absolute;
left:50%;
top:100%;
width:300px;
height:40px;
border-radius:10px 10px 20px 10px;
transition:all .5s,transform 1s ease-in,;
transform-origin:22px 22px;
}
/* 扇條hover樣式 */
.item:hover{
width:336px;
border-radius:10px 10px 10px 10px;
cursor: pointer;
}
/*
設(shè)置扇條中的文字樣式,利用偽對象實(shí)現(xiàn)
*/
.item:after{
position: absolute;
right:10px;
top:0;
line-height: 40px;
color:#FFF;
}
.item:nth-child(1):before{
content:attr(title);
position: absolute;
right:90px;
top:0;
line-height: 40px;
color:#FFF;
}
/* 旋轉(zhuǎn)中心的樣式 */
.dot{
position: absolute;
left:15px;
top:15px;
border-radius:15px;
height:10px;
width:10px;
background-color:#333333;
border:4px #777777 solid;
z-index:100;
}
/*
關(guān)鍵代碼----
通過循環(huán)給不同的扇條添加樣式
*/
@for $i from 1 through $numOfItem{
//通過循環(huán)給不同的扇條增加樣式
//z-index,改變疊放次序
//bgc,設(shè)置不同的顏色
//通過:after偽對象來放置顏色文本
.item:nth-child(#{$i}){
z-index:100-$i;
background-color: hsl(360*($numOfItem - $i)/($numOfItem - 1),50%,50%);
&:after{
content:"#{hsl(360*($numOfItem - $i)/($numOfItem - 1),50%,50%)}";
}
}
//通過循環(huán)給不同的扇條增加樣式
//hover之后,旋轉(zhuǎn)扇條
//當(dāng)旋轉(zhuǎn)角度超過角度之后,旋轉(zhuǎn)文字
#container:hover .item:nth-child(#{$i}){
transform:rotate($degreeOfFan*($i - $numOfItem)/$numOfItem);
&:after,&:before{
@if($degreeOfFan * ($i - $numOfItem)/$numOfItem < -90deg){
transform:rotate(180deg);
}}
}
}
完工,注釋比較完善,原理不再贅述。
相關(guān)文章
- CSS Grid 是一種二維布局系統(tǒng),可以同時(shí)控制行和列,相比 Flex(一維布局),更適合用在整體頁面布局或復(fù)雜模塊結(jié)構(gòu)中,這篇文章主要介紹了前端CSS Grid 布局詳解,需要的朋2025-04-16
CSS Padding 和 Margin 區(qū)別全解析
CSS 中的 padding 和 margin 是兩個(gè)非常基礎(chǔ)且重要的屬性,它們用于控制元素周圍的空白區(qū)域,本文將詳細(xì)介紹 padding 和 margin 的概念、區(qū)別以及如何在實(shí)際項(xiàng)目中使用它們2025-04-07- will-change 是一個(gè) CSS 屬性,用于告訴瀏覽器某個(gè)元素在未來可能會(huì)發(fā)生哪些變化,本文給大家介紹CSS will-change 屬性詳解,感興趣的朋友一起看看吧2025-04-07
- 本文給大家分享在 CSS 中,去除a標(biāo)簽(超鏈接)的下劃線的幾種方法,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-04-07
- 在前端開發(fā)中,CSS(層疊樣式表)不僅是用來控制網(wǎng)頁的外觀和布局,更是實(shí)現(xiàn)復(fù)雜交互和動(dòng)態(tài)效果的關(guān)鍵技術(shù)之一,隨著前端技術(shù)的不斷發(fā)展,CSS的用法也日益豐富和高級,本文將2025-04-07
css中的 vertical-align與line-height作用詳解
文章詳細(xì)介紹了CSS中的`vertical-align`和`line-height`屬性,包括它們的作用、適用元素、屬性值、常見使用場景、常見問題及解決方案,感興趣的朋友跟隨小編一起看看吧2025-03-26淺析CSS 中z - index屬性的作用及在什么情況下會(huì)失效
z-index屬性用于控制元素的堆疊順序,值越大,元素越顯示在上層,它需要元素具有定位屬性(如relative、absolute、fixed或sticky),本文給大家介紹CSS 中z - index屬性的作用2025-03-21- 文章詳細(xì)介紹了CSS中的打印媒體查詢@mediaprint包括基本語法、常見使用場景和代碼示例,如隱藏非必要元素、調(diào)整字體和顏色、處理鏈接的URL顯示、分頁控制、調(diào)整邊距和背景等2025-03-18
CSS模擬 html 的 title 屬性(鼠標(biāo)懸浮顯示提示文字效果)
本文介紹了如何使用CSS模擬HTML的title屬性,通過鼠標(biāo)懸浮顯示提示文字效果,通過設(shè)置`.tipBox`和`.tipBox.tipContent`的樣式,實(shí)現(xiàn)了提示內(nèi)容的隱藏和顯示,感興趣的朋友一起2025-03-10前端 CSS 動(dòng)態(tài)設(shè)置樣式::class、:style 等技巧(推薦)
本文介紹了Vue.js中動(dòng)態(tài)綁定類名和內(nèi)聯(lián)樣式的兩種方法:對象語法和數(shù)組語法,通過對象語法,可以根據(jù)條件動(dòng)態(tài)切換類名或樣式;通過數(shù)組語法,可以同時(shí)綁定多個(gè)類名或樣式,此外2025-02-26