天天看點

2021-07-25(html&css&js 3D載入動畫,js控制樣式、随機動态顔色)

(html&css&js 3D載入動畫,js控制樣式、随機動态顔色)

若有疑問可評論留言或私信

index.html

<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/index.css">
</head>
<body>
//載入動畫部分
<div>
<div>
        <ul>
            <li>載</li>
            <li>載</li>
            <li>載</li>
            <li>載</li>
        </ul>
        <ul>
            <li>入</li>
            <li>入</li>
            <li>入</li>
            <li>入</li>
        </ul>
        <ul>
            <li>中</li>
            <li>中</li>
            <li>中</li>
            <li>中</li>
        </ul>
        </div>
</div>
//載入動畫部分end
//js改變樣式部分
<input type="text">
//js改變樣式部分end
</body>
<script src="js/index.js"></script>
</html>
           

index.css

*{
margin:0;
padding:0;
border:0;
}
body>div:first-of-type{
    width:100%;
    height:1000px;
    padding-top:300px;
    background:white;
    position:fixed;
    top:0px;
    left:0px;
    text-align:center;
}
body div:first-of-type>div{
    width: 180px;
    margin: 0 auto;
    background: green;
}
ul{
    position: relative;
    width: 60px;
    height: 60px;
    float: left;
    transform-style: preserve-3d;
    animation: move 1.5s infinite linear;
}
@keyframes move{
    0%{ 
    transform: rotateX(0deg);
    }
    100%{ 
    transform: rotateX(360deg);
    }
}
ul:nth-of-type(2){
    animation: moveT 1.5s infinite linear;
}
@keyframes moveT{
    0%{ 
    transform: rotateX(0deg);
    }
    100%{ 
    transform: rotateX(-360deg);
    }
}
li{
//css實作載入動畫,後js控制動畫結束
    position: absolute;
    width: 50px;
    height: 50px;
    line-height: 50px;
    left: 0;
    top: 0;
}
ul:first-of-type li{
    border:5px dashed lime;
    background: white;
}
ul:nth-of-type(2) li{
    border:5px dashed red;
    background: white;
}
ul:nth-of-type(3) li{
    border:5px dashed aqua;
    background: white;
}
ul li:nth-of-type(1){
    transform: translateZ(30px);
}
ul li:nth-of-type(2){
    transform: translateZ(-30px) rotateZ(180deg) rotateY(180deg);
}
ul li:nth-of-type(3){
    transform: translateY(30px) rotateX(-90deg);
}
ul li:nth-of-type(4){
    transform: translateY(-30px) rotateX(90deg);
}


           

index.js

//随機顔色函數,每次調用生成不同顔色,透明度
function randomColor(){
    return ('rgba('+parseInt(Math.random()*256)
    +','+parseInt(Math.random()*256)
    +','+parseInt(Math.random()*256)
    +',0.'+parseInt(Math.random()*11)+')');
}
//接收參數:html标簽對象、邊框樣式
function labels(labelSelect,borderStyle){
    labelSelect.style.cssText='font-size:20px;font-weight:bold;text-align:center;word-break:break-all;transition:0.3s;color:'+randomColor()+';background:'+randomColor()+';border:5px '+randomColor()+' '+borderStyle+';';
}
function inputStyle(){
var inputLabel=document.querySelector('input');
//引用labels函數,傳入參數
labels(inputLabel,'solid');
}
//每300毫秒随機配置設定給input顔色樣式
setInterval(inputStyle,300);
//載入頁面1.5秒結束載入動畫(隐藏包含動畫的div标簽)
setTimeout(function(){
document.querySelestor('div').style.cssText='display:none;';
},1500);
           

END