在頁面中放置一個類名爲container的層做爲容器,在該層中放置5個字符區域,HTML代碼描述以下:html
<div class="container">瀏覽器
<span>H</span>ide
<span>E</span>flex
<span>L</span>動畫
<span>L</span>spa
<span>O</span>3d
</div>code
爲container和span定義CSS樣式規則,並定義實現5個字符翻轉的動畫關鍵幀。完整的HTML文件內容以下。orm
<!DOCTYPE html> <html> <head> <title>字符翻轉</title> <style> .container { margin: 0 auto; width: 500px; height: 300px; position: relative; overflow: hidden; display:flex; align-items: center; justify-content: center; border: 4px solid rgba(255, 0, 0, 0.9); background:#d8d8d8; border-radius: 10%; } .container>span { font-size: 130px; font-family: "Impact",sans-serif; display: inline-block; animation:flip 2s infinite linear; transform-origin:0 70%; transform-style:preserve-3d; } @keyframes flip { 50% { transform: rotateX(360deg); } 100% { transform: rotateX(360deg); } } .container>span:nth-child(1n+0) { color:var(--color); animation-delay: var(--delay); } </style> </head> <body> <div class="container"> <span style="--delay:0s;--color:#f00">H</span> <span style="--delay:0.4s;--color:#f0f">E</span> <span style="--delay:0.8s;--color:#ff0">L</span> <span style="--delay:1.2s;--color:#0ff">L</span> <span style="--delay:1.6s;--color:#800080">O</span> </div> </body> </html>
在瀏覽器中打開包含這段HTML代碼的html文件,能夠呈現出如圖1所示的動畫效果。htm
圖1 字符繞X軸翻轉
若將上面的關鍵幀設置中的兩個屬性值「rotateX」均改爲「rotateY」,則呈現如圖2所示的動畫效果。
圖2 字符繞Y軸翻轉
若將上面的關鍵幀設置中的兩個屬性值「rotateX」均改爲「rotateZ」,則呈現如圖3所示的動畫效果。
圖3 字符繞Y軸翻轉
咱們能夠經過修改字符區span的top屬性的屬性值讓字符上下移動,同時設置文本的陰影,造成字符的跳躍動畫效果。
編寫的完整HTML文件以下。
<!DOCTYPE HTML> <html> <head> <title>跳躍的字符</title> <style> .container { margin: 0 auto; width: 600px; height: 200px; position: relative; overflow: hidden; display:flex; align-items: center; justify-content: center; border: 4px solid rgba(255, 0, 0, 0.9); background:#f5f5f5; border-radius: 10%; } .container>span { position: relative; top: 20px; display: inline-block; animation: bounce .3s ease infinite alternate; font-family: "Impact",sans-serif; font-size: 80px; text-shadow: 0 1px 0 #CCC, 0 2px 0 #CCC, 0 3px 0 #CCC, 0 4px 0 #CCC, 0 5px 0 #CCC, 0 6px 0 transparent, 0 7px 0 transparent, 0 8px 0 transparent, 0 9px 0 transparent, 0 10px 10px rgba(0, 0, 0, .4); } .container>span:nth-child(1n+0) { color:var(--color); animation-delay: var(--delay); } @keyframes bounce { 100% { top: -20px; text-shadow: 0 1px 0 #CCC, 0 2px 0 #CCC, 0 3px 0 #CCC, 0 4px 0 #CCC, 0 5px 0 #CCC, 0 6px 0 #CCC, 0 7px 0 #CCC, 0 8px 0 #CCC, 0 9px 0 #CCC, 0 50px 25px rgba(0, 0, 0, .2); } } </style> </head> <body> <div class="container"> <span style="--delay:0s;--color:#f00">H</span> <span style="--delay:0.1s;--color:#f0f">a</span> <span style="--delay:0.2s;--color:#ff0">p</span> <span style="--delay:0.3s;--color:#0f0">p</span> <span style="--delay:0.4s;--color:#0ff">y</span> <span style="--delay:0.5s;--color:#00f">N</span> <span style="--delay:0.6s;--color:#800080">e</span> <span style="--delay:0.7s;--color:#008080">w</span> <span style="--delay:0.8s;--color:#ff6347">Y</span> <span style="--delay:0.9s;--color:#ee82ee">e</span> <span style="--delay:1.0s;--color:#8b4513">a</span> <span style="--delay:1.1s;--color:#fa8072">r</span> </div> </body> </html>
在瀏覽器中打開包含這段HTML代碼的html文件,能夠呈現出如圖4所示的動畫效果。
圖4 跳躍的字符
經過濾鏡的方式讓文字發光,造成霓虹文字效果。編寫HTML文件內容以下。
<!DOCTYPE html> <html> <head> <title>霓虹字符</title> <style> .container { margin: 0 auto; width: 500px; height: 300px; position: relative; overflow: hidden; display:flex; align-items: center; justify-content: center; border: 4px solid rgba(255, 0, 0, 0.9); background:#000000; border-radius: 10%; } .container>span { margin-right: 10px; display: inline-block; font-size: 100px; font-family: "Impact",sans-serif; color: white; text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #e60073, 0 0 40px #e60073, 0 0 50px #e60073, 0 0 60px #e60073, 0 0 70px #e60073; animation-name: anim; animation-timing-function: linear; animation-direction: alternate-reverse; animation-iteration-count: infinite; } .container>span:nth-child(1) { animation-delay: 0.2s; animation-duration: 0.5s; } .container>span:nth-child(2) { animation-delay: 0.4s; animation-duration: 1s; } .container>span:nth-child(3) { animation-delay: 0.6s; animation-duration: 1.5s; } .container>span:nth-child(4) { animation-delay: 0.8s; animation-duration: 2s; } .container>span:nth-child(5) { animation-delay: 1s; animation-duration: 2.5s; } @keyframes anim { 0% { opacity: .1; background-position: 0 0; filter: hue-rotate(0deg); } 10% { background-position: 5px 0; } 20% { background-position: -5px 0; } 30% { background-position: 15px 0; } 40% { background-position: -5px 0; } 50% { background-position: -25px 0; } 60% { background-position: -50px 0; } 70% { background-position: 0 -20px; } 80% { background-position: -60px -20px;} 81% { background-position: 0 0; } 100% { opacity: 1; background-position: 0 0; filter: hue-rotate(360deg); } } </style> </head> <body> <div class="container"> <span>W</span> <span>U</span> <span>H</span> <span>A</span> <span>N</span> </div> </body> </html>
在瀏覽器中打開包含這段HTML代碼的html文件,能夠呈現出如圖5所示的動畫效果。
圖5 霓虹文字