果如圖:
index.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>純CSS3實現發光開關切換按鈕DEMO演示</title> <link rel="stylesheet" href="style.css" media="screen" type="text/css" /> </head> <body style="background-color: #1c1d1f;"> <div style="text-align:center;clear:both"> <script src="/gg_bd_ad_720x90.js" type="text/javascript"></script> <script src="/follow.js" type="text/javascript"></script> </div> <iframe frameborder="0" scrolling="no" src="index2.html" width="100%" height="500px"></iframe> </body> </html>
index2.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Switch Button #3 - CodePen</title> <link rel="stylesheet" href="style.css" media="screen" type="text/css" /> </head> <body> <label class="button"><input type="checkbox"><span></span><span></span></input></label> <script src="js/index.js"></script> </body> </html>
style.css
*, *:before, *:after { -moz-box-sizing: border-box; box-sizing: border-box; } body { background-color: #1c1d1f; } .button { display: block; width: 400px; height: 120px; position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); background-color: #000000; box-shadow: 0 -1px 0 rgba(255, 255, 255, 0.2) inset; border-radius: 20px; overflow: hidden; cursor: pointer; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); } .button span { display: block; position: absolute; top: 6px; width: 194px; height: 108px; background-color: #1c1d1f; -webkit-transition: -webkit-transform 300ms ease, box-shadow 300ms ease; transition: transform 300ms ease, box-shadow 300ms ease; } .button span:before { position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); font-family: "Open Sans"; font-weight: 800; font-size: 48px; -webkit-transition: text-shadow 800ms ease 100ms, color 800ms ease 100ms; transition: text-shadow 800ms ease 100ms, color 800ms ease 100ms; } .button span:after { content: ""; width: 4px; height: 108px; position: absolute; top: 0; background: -webkit-radial-gradient(center, ellipse, rgba(255, 255, 255, 0.5) 0%, transparent 50%); background: radial-gradient(ellipse at center, rgba(255, 255, 255, 0.5) 0%, transparent 50%); -webkit-transition: opacity 300ms ease; transition: opacity 300ms ease; } .button span:first-of-type { left: 6px; border-top-left-radius: 18px; border-bottom-left-radius: 18px; -webkit-transform-style: preserve3d; transform-style: preserve3d; -webkit-transform-origin: right center; -ms-transform-origin: right center; transform-origin: right center; -webkit-transform: perspective(2000px) rotateY(40deg); transform: perspective(2000px) rotateY(40deg); box-shadow: -1px 0 1px rgba(255, 255, 255, 0.1) inset, 4px 0 8px rgba(255, 255, 255, 0.1) inset, 1px 0 0 rgba(255, 255, 255, 0.1) inset, -10px 0 8px rgba(40, 42, 44, 0.9), -20px 0 8px rgba(28, 29, 31, 0.7), -30px 0 8px rgba(28, 29, 31, 0.4); } .button span:first-of-type:before { content: "ON"; color: rgba(0, 0, 0, 0.5); text-shadow: 1px 4px 6px #1c1d1f, 0 0 0 #000000, 1px 4px 6px #1c1d1f; } .button span:first-of-type:after { left: -1px; } .button span:last-of-type { right: 6px; border-top-right-radius: 18px; border-bottom-right-radius: 18px; -webkit-transform-origin: left center; -ms-transform-origin: left center; transform-origin: left center; box-shadow: -1px 1px 1px rgba(255, 255, 255, 0.1) inset, 2px 0 2px rgba(255, 255, 255, 0.05) inset; } .button span:last-of-type:before { content: "OFF"; color: #ff4847; text-shadow: 0 0 24px rgba(255, 72, 71, 0.6); } .button span:last-of-type:after { right: -1px; opacity: 0; } .button input[type="checkbox"] { display: none; } .button input[type="checkbox"]:checked ~ span:first-of-type { -webkit-transform: none; -ms-transform: none; transform: none; box-shadow: 1px 1px 1px rgba(255, 255, 255, 0.1) inset, -2px 0 2px rgba(255, 255, 255, 0.05) inset; } .button input[type="checkbox"]:checked ~ span:first-of-type:before { color: #93c913; text-shadow: 0 0 24px rgba(147, 201, 19, 0.6); } .button input[type="checkbox"]:checked ~ span:first-of-type:after { opacity: 0; } .button input[type="checkbox"]:checked ~ span:last-of-type { -webkit-transform: perspective(2000px) rotateY(-40deg); transform: perspective(2000px) rotateY(-40deg); box-shadow: 1px 0 1px rgba(255, 255, 255, 0.1) inset, -4px 0 8px rgba(255, 255, 255, 0.1) inset, -1px 0 0 rgba(255, 255, 255, 0.1) inset, 10px 0 8px rgba(40, 42, 44, 0.9), 20px 0 8px rgba(28, 29, 31, 0.7), 30px 0 8px rgba(28, 29, 31, 0.4); } .button input[type="checkbox"]:checked ~ span:last-of-type:before { color: rgba(0, 0, 0, 0.5); text-shadow: 1px 4px 6px #1c1d1f, 0 0 0 #000000, 1px 4px 6px #1c1d1f; } .button input[type="checkbox"]:checked ~ span:last-of-type:after { opacity: 1; }
style.less
@background: #1C1D1F; @button: #1C1D1F; @light-on: #93C913; @light-off: #FF4847; @import url(http://fonts.googleapis.com/css?family=Open+Sans:800); *, *:before, *:after { box-sizing: border-box } body { background-color: @background; } .button { display: block; width: 400px; height: 120px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); background-color: darken(@button, 50%); box-shadow: 0 -1px 0 fade(white, 20%) inset; border-radius: 20px; overflow: hidden; cursor: pointer; -webkit-tap-highlight-color: fade(black, 0%); span { display: block; position: absolute; top: 6px; width: 194px; height: 108px; background-color: @button; transition: transform 300ms ease, box-shadow 300ms ease; &:before { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-family: "Open Sans"; font-weight: 800; font-size: 48px; transition: text-shadow 800ms ease 100ms, color 800ms ease 100ms; } &:after { content: ""; width: 4px; height: 108px; position: absolute; top: 0; background: radial-gradient(ellipse at center, fade(white, 50%) 0%, transparent 50%); transition: opacity 300ms ease; } &:first-of-type { left: 6px; border-top-left-radius: 18px; border-bottom-left-radius: 18px; transform-style: preserve3d; transform-origin: right center; transform: perspective(2000px) rotateY(40deg); box-shadow: -1px 0 1px fade(white, 10%) inset, 4px 0 8px fade(white, 10%) inset, 1px 0 0 fade(white, 10%) inset, -10px 0 8px fade(lighten(@button, 5%), 90%), -20px 0 8px fade(@button, 70%), -30px 0 8px fade(@button, 40%); &:before { content: "ON"; color: fade(black, 50%); text-shadow: 1px 4px 6px @button, 0 0 0 black, 1px 4px 6px @button; } &:after { left: -1px; } } &:last-of-type { right: 6px; border-top-right-radius: 18px; border-bottom-right-radius: 18px; transform-origin: left center; box-shadow: -1px 1px 1px fade(white, 10%) inset, 2px 0 2px fade(white, 5%) inset; &:before { content: "OFF"; color: @light-off; text-shadow: 0 0 24px fade(@light-off, 60%); } &:after { right: -1px; opacity: 0; } } } input[type="checkbox"] { display: none; &:checked ~ span { &:first-of-type { transform: none; box-shadow: 1px 1px 1px fade(white, 10%) inset, -2px 0 2px fade(white, 5%) inset; &:before { color: @light-on; text-shadow: 0 0 24px fade(@light-on, 60%); } &:after { opacity: 0; } } &:last-of-type { transform: perspective(2000px) rotateY(-40deg); box-shadow: 1px 0 1px fade(white, 10%) inset, -4px 0 8px fade(white, 10%) inset, -1px 0 0 fade(white, 10%) inset, 10px 0 8px fade(lighten(@button, 5%), 90%), 20px 0 8px fade(@button, 70%), 30px 0 8px fade(@button, 40%); &:before { color: fade(black, 50%); text-shadow: 1px 4px 6px @button, 0 0 0 #000, 1px 4px 6px @button; } &:after { opacity: 1; } } } } }
內容轉至網絡,若侵權,聯系刪除
載說明:原創不易,未經授權,謝絕任何形式的轉載
俗話說得好,興趣是最好的老師。本篇文章搜集了幾個漂亮的宇宙風格按鈕動畫效果的代碼,希望它們能夠激發你對 CSS 的熱愛。作為前端開發者,我們不僅僅是制作寫頁面和與 后端 API 打交道,偶爾也需要調味劑。廢話不多說,讓我們一起來解讀這些 CSS 代碼吧。
HTML部分
<button class="btn" type="button">
<strong>SPACE</strong>
<div id="container-stars">
<div id="stars"></div>
</div>
<div id="glow">
<div class="circle"></div>
<div class="circle"></div>
</div>
</button>
CSS部分
.btn {
display: flex;
justify-content: center;
align-items: center;
width: 13rem;
height: 3rem;
background-size: 300% 300%;
backdrop-filter: blur(1rem);
border-radius: 5rem;
transition: 0.5s;
animation: gradient_301 5s ease infinite;
border: double 4px transparent;
background-image: linear-gradient(#212121, #212121), linear-gradient(137.48deg, #ffdb3b 10%,#FE53BB 45%, #8F51EA 67%, #0044ff 87%);
background-origin: border-box;
background-clip: content-box, border-box;
}
#container-stars {
position: fixed;
z-index: -1;
width: 100%;
height: 100%;
overflow: hidden;
transition: 0.5s;
backdrop-filter: blur(1rem);
border-radius: 5rem;
}
strong {
z-index: 2;
font-family: 'Avalors Personal Use';
font-size: 12px;
letter-spacing: 5px;
color: #FFFFFF;
text-shadow: 0 0 4px white;
}
#glow {
position: absolute;
display: flex;
width: 12rem;
}
.circle {
width: 100%;
height: 30px;
filter: blur(2rem);
animation: pulse_3011 4s infinite;
z-index: -1;
}
.circle:nth-of-type(1) {
background: rgba(254, 83, 186, 0.636);
}
.circle:nth-of-type(2) {
background: rgba(142, 81, 234, 0.704);
}
.btn:hover #container-stars {
z-index: 1;
background-color: #212121;
}
.btn:hover {
transform: scale(1.1)
}
.btn:active {
border: double 4px #FE53BB;
background-origin: border-box;
background-clip: content-box, border-box;
animation: none;
}
.btn:active .circle {
background: #FE53BB;
}
#stars {
position: relative;
background: transparent;
width: 200rem;
height: 200rem;
}
#stars::after {
content: "";
position: absolute;
top: -10rem;
left: -100rem;
width: 100%;
height: 100%;
animation: animStarRotate 90s linear infinite;
}
#stars::after {
background-image: radial-gradient(#ffffff 1px, transparent 1%);
background-size: 50px 50px;
}
#stars::before {
content: "";
position: absolute;
top: 0;
left: -50%;
width: 170%;
height: 500%;
animation: animStar 60s linear infinite;
}
#stars::before {
background-image: radial-gradient(#ffffff 1px, transparent 1%);
background-size: 50px 50px;
opacity: 0.5;
}
@keyframes animStar {
from {
transform: translateY(0);
}
to {
transform: translateY(-135rem);
}
}
@keyframes animStarRotate {
from {
transform: rotate(360deg);
}
to {
transform: rotate(0);
}
}
@keyframes gradient_301 {
0% {
background-position: 0% 50%;
}
50% {
background-position: 100% 50%;
}
100% {
background-position: 0% 50%;
}
}
@keyframes pulse_3011 {
0% {
transform: scale(0.75);
box-shadow: 0 0 0 0 rgba(0, 0, 0, 0.7);
}
70% {
transform: scale(1);
box-shadow: 0 0 0 10px rgba(0, 0, 0, 0);
}
100% {
transform: scale(0.75);
box-shadow: 0 0 0 0 rgba(0, 0, 0, 0);
}
}
代碼解釋
@keyframes pulse_3011
這個動畫定義了一個名為“pulse_3011”的關鍵幀動畫。在該動畫中,元素從開始狀態縮小到0.75倍,同時有一個0.7不透明度的黑色陰影。在動畫執行的70%處,元素擴大到原始大小,同時黑色陰影消失。最終,元素再次縮小到0.75倍,而黑色陰影再次消失。該動畫無限重復。
stars::before 和 stars::after 相關的代碼是實現星星循環動畫的關鍵,設置了背景為一個由白色和透明顏色交替組成的徑向漸變色,這樣就形成了一些閃閃發光的小星星。通過 animation 屬性設置了動畫效果,其中 animStar 是一個移動動畫,設置了 60s 的動畫時間,并且使用 linear 動畫函數,使得小星星以勻速向上移動的方式出現在屏幕上,并且使用 infinite 讓其循環播放。這樣就形成了一些往上移動的小星星,從而實現了整個星空的動態效果。
HTML部分
<button>
<div id="moon" class="state"> Moony</div>
<div id="sun" class="state">Sunny</div>
<span class="border border1"></span>
<span class="border border2"></span>
</button>
CSS部分
button {
--sunGradient: linear-gradient(to right, #fa709a 0%, #fee140 100%);
--moonGradient: linear-gradient(to right, #6a11cb 0%, #2575fc 100%);
display: flex;
justify-content: center;
align-items: center;
position: relative;
width: 85px;
height: 85px;
color: white;
font-size: 0.8em;
font-weight: bold;
text-transform: uppercase;
border-radius: 70px;
background-color: transparent;
transition: 0.09s;
box-shadow: 0 20px 30px black;
overflow: hidden;
}
button:hover {
transform: rotate(360deg);
}
button:active {
transform: rotate(360deg) scale(1.2);
box-shadow: -15px -15px 500px white,inset 0 0 5px black, 0px -15px 20px white;
}
.state {
position: absolute;
display: flex;
justify-content: center;
align-items: center;
border-radius: inherit;
width: 90%;
height: 90%;
border: 4px groove black;
}
#sun {
display: none;
transition: 2s;
background: var(--sunGradient);
}
#moon {
background: var(--moonGradient);
}
button:hover #sun {
display: flex;
}
button:active #moon {
display: none;
}
.border {
position: absolute;
transition: 0.4s;
z-index: -1;
border-radius: inherit;
background: var(--moonGradient);
}
button:hover .border {
background: var(--sunGradient);
}
.border1 {
width: 45px;
height: 100px;
animation: 1s linear reverse infinite rotation_91;
}
.border2 {
width: 200px;
height: 35px;
animation: 3s linear reverse infinite rotation_91;
}
@keyframes rotation_91 {
from {
transform: rotate(0deg)
}
to {
transform: rotate(360deg)
}
}
代碼解釋:
這段代碼實現了一個狀態切換的動畫按鈕效果,它包含兩個狀態:一個是太陽狀態,另一個是月亮狀態。當鼠標懸停在按鈕上時,太陽狀態會出現,月亮狀態會消失。當用戶點擊按鈕時,按鈕會放大并旋轉,并且太陽狀態會替換成月亮狀態,月亮狀態則替換成太陽狀態。
具體實現的邏輯如下:
HTML部分:
CSS部分:
HTML部分
<button>
Button
</button>
CSS部分:
button {
position: relative;
padding: 18px 55px;
font-size: 15px;
font-weight: bold;
color: #fff;
background: linear-gradient(87deg, #6d67e4, #7743db, #4e31aa, #6d67e4, #6d67e4);
background-size: 600% 600%;
border: 3px solid #7743db;
border-radius: 8px;
/* filter: drop-shadow(0 0 4px #9F73AB); */
box-shadow: inset 0 0 10px 1px #7743db, 0 0 10px 5px #7743db;
cursor: pointer;
transition: all 0.5s;
animation: GradientAnimation 25s ease infinite;
}
button::after {
content: "";
position: absolute;
width: 202px;
height: 54px;
top: 0;
left: -17px;
border: 5px solid #453c67;
border-width: 0 2px 4px 0;
border-radius: 100%;
filter: drop-shadow(0px 0px 6px rgba(78, 49, 180, 1));
transform: rotate(-30deg);
animation: LinerAfter 15s ease infinite;
}
button::before {
content: "";
position: absolute;
width: 200px;
height: 50px;
top: 0;
left: -20px;
border: 5px solid #6d67e4;
border-width: 0 12px 8px 4px;
border-radius: 100%;
filter: drop-shadow(0px 0px 6px rgba(109, 103, 228, 1));
/* box-shadow: inset 0 0 10px 1px #4E31AA, 0 0 10px 5px #4E31AA; */
transform: rotate(-30deg);
transition: all 0.5s;
animation: LinerBefore 15s ease infinite;
}
@keyframes GradientAnimation {
0% {
background-position: 0% 50%;
}
50% {
background-position: 100% 50%;
}
100% {
background-position: 0% 50%;
}
}
@keyframes LinerAfter {
0% {
left: -27px;
height: 54px;
}
50% {
left: -7px;
height: 37px;
}
100% {
left: -27px;
height: 54px;
}
}
@keyframes LinerBefore {
0% {
left: -30px;
height: 50px;
}
50% {
left: -10px;
height: 33px;
}
100% {
left: -30px;
height: 50px;
}
}
代碼解釋:
CSS代碼設置了按鈕的樣式,包括位置、內邊距、字體大小、加粗程度、顏色、背景、邊框、圓角、光標形狀、過渡效果、動畫等。其中background屬性使用了線性漸變,border屬性設置了3px寬度的實線邊框,border-radius屬性設置了8px的圓角。box-shadow屬性設置了兩個陰影效果,一個是內陰影,另一個是外陰影。button::after和button::before是button的偽元素,分別表示按鈕前面和后面的圓圈。這兩個圓圈都是絕對定位,大小、位置、邊框、圓角等屬性不同,但都使用了drop-shadow濾鏡和rotate旋轉變換效果。動畫效果分別由GradientAnimation、LinerAfter和LinerBefore三個關鍵幀動畫控制,分別設置了不同的屬性變化規則,實現了按鈕和圓圈的不同的動態效果。
今天的內容就分享到這里,太多了就不利于我們去理解,雖然就三個案例,但是需要我們去慢慢消化和理解,才能成為自己的知識,建議大家還是親自實現下上述的按鈕動畫效果,這些案例雖然我們在平常的工作中很少用到,但是如果你接到酷炫的項目,比如某品牌的宣傳站點時,我相信這些效果還是能派上用場的。
如果你喜歡這樣的文章,請點贊支持下,后續我會持續分享這類的內容,如果太少,我會覺得大家不太喜歡這樣的文章,我只能自己私藏了,最后別忘記關注「前端達人」,你的支持將是我分享最大的動力,后續我會持續輸出更多內容,敬請期待。
網頁設計工作中,我們可以給很多元素添加發光的動畫效果,舉幾個例子,“輸入框”、“文字”、“進度條”等。給這些元素加上的發光特效會讓整個網頁變得更加炫酷。今天陜西優就業小編就為大家搜集整理了8款炫酷的HTML發光動畫,這些都可以用作Web前端開發工作中,可以為你節省很多開發時間。
1.HTML5 Canvas五彩繽紛的3D發光水晶球動畫
這是一款基于HTML5 Canvas的超絢麗發光水晶球動畫,就像是酒吧里的那種水晶球射燈一樣,不停地隨機變換射出光線的顏色,給人一種非常動感的視覺效果。整個水晶球是在Canvas畫布上繪制而成,利用CSS3的相關特性使其能夠出現五彩繽紛的發光動畫,而且可以通過鼠標滾輪來放大縮小這個水晶球,不得不說HTML5真的是非常強大。
2.jQuery/CSS3實現漂亮字體發光特效
今天我們要來分享一款很酷的jQuery/CSS3文字發光特效,首先是加載了谷歌的公共字體庫,因此字體非常特別和漂亮,另外利用了jQuery和CSS3的相關特性,當鼠標滑過文字時,文字將會出現發光的動畫特效,非常漂亮。
3.超絢麗CSS3多色彩發光立方體旋轉動畫
之前我們分享過幾個不錯的CSS3立方體動畫,比如這款HTML5 3D立方體旋轉動畫和HTML5 3D立方體圖片切換動畫。今天要分享的也是一款基于CSS3的3D立方體旋轉動畫,不同的是,這款立方體的每一個面都有不同的色彩,并且會帶有絢麗的發光特效。
4.純CSS3實現發光開關切換按鈕
前段時間我們向大家分享過一款牛奶般剔透的CSS3 3D開關按鈕,效果相當贊。今天我們要來分享一款類似的純CSS3發光開關切換按鈕,它的外觀就像一個電燈的開關,可以左右切換。另外開關上的文字還有發光的特效,整體看上去很有立體感。
5.純CSS3和SVG鼠標滑過燈泡發光特效
這次要分享一款利用純CSS3和SVG實現的燈泡發光效果,我們只需要將鼠標滑過燈泡,整個燈泡就會出現發光發亮的動畫特效,效果相當逼真。CSS3的運用,讓燈泡外圍有一層淡淡的光暈。并且在燈光點亮和熄滅的時候有淡入淡出的效果。
6.HTML5 Canvas發光Loading動畫
之前我們分享過很多基于CSS3的Loading動畫效果,相信大家都很喜歡。今天我們要來分享一款基于HTML5 Canvas的發光Loading加載動畫特效。Loading旋轉圖標是在canvas畫布上繪制的,整個loading動畫是發光3D的視覺效果,HTML5非常強大。
7.CSS3發光Loading加載動畫
今天我們要分享一款與眾不同的CSS3 Loading動畫效果,它是由幾個不同的動畫效果組合而成的,像文字打印效果、發光效果、飛入飛出效果,這幾種效果都非???。另外我們可以再回顧一下其他的CSS3 Loading效果:HTML5 Canvas實現超酷Loading動畫、很有個性的CSS3彈跳Loading動畫。
8.純CSS3實現發光動畫按鈕特效
這是一款基于純CSS3的發光按鈕,按鈕發光的顏色是隨機的,這樣一排按鈕就顯得五彩繽紛,非常絢麗。之前我們也介紹過不少樣式很獨特的CSS3按鈕,比如純CSS3 3D開關按鈕、純CSS3實現動感彈性按鈕等,都非常不錯,也有HTML5動畫按鈕,大家可以自己看看。
陜西優就業:http://www.ujiuye.com/shaanxi/
IT學習交流:468910291
*請認真填寫需求信息,我們會在24小時內與您取得聯系。