0x00 前言
今日在b站上看到一個hover動畫效果,感覺寫的非常好,所以復刻了一個
0x01 HTML+JS
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<title>Log-in</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="manifest" href="site.webmanifest">
<link rel="apple-touch-icon" href="icon.png">
<!-- Place favicon.ico in the root directory -->
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<meta name="theme-color" content="#fafafa">
</head>
<body>
<!--[if IE]>
<p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a href="https://browsehappy.com/">upgrade your browser</a> to improve your experience and security.</p>
<![endif]-->
<!-- Add your site or application content here -->
<img src="img/timg.gif" class="mouse-img">
<div class="container">
<h1>Sup</h1>
<form action="">
<input class="tbx" type="text" placeholder="用戶名"/>
<input class="tbx" type="password" placeholder="密碼"/>
<input class="sub" type="submit" value="登陸"/>
</form>
</div>
<script>
document.addEventListener('mousemove',(e) => {
let x = e.clientX;
let y = e.clientY;
let img = document.querySelector('img');
img.style.top = y + 'px';
img.style.left = x + 'px';
})
// 定義一個con綁定 .container
const con=document.querySelector('.container');
// 定義兩個函數開關(門)
let isIn=true; //鼠標進去的門,默認打開
let isOut=false; //鼠標出去的門,默認關閉
var span; //給未出生的元素取個名字
//監聽鼠標進去的事件+進去的方法
con.addEventListener('mouseenter',(e)=>{
if(isIn){//如果進去的門時打開的,就可以執行這個函數
//獲取進入的鼠標位置
//生成元素的位置=進入點距離窗口的距離-父盒子距離窗口的距離
let inx=e.clientX-e.target.offsetLeft;
let iny=e.clientY-e.target.offsetTop;
//創建一個span元素,並且給它對應的出生座標
let el=document.createElement('span');
el.style.left=inx+'px';
el.style.top=iny+'px';
con.appendChild(el);//添加到con對應的父元素,即container
$('.container span').removeClass('out');//除去出去的動畫 不知道這樣寫是不是不太好
$('.container span').addClass('in');//添加進入的動畫
span=document.querySelector('.container span');
isIn=false; //關閉進來的門(不能使用進入的方法)
isOut=true; //打開出去的門(可以使用出去的方法)
}
})
//監聽鼠標出來的事件+出來的方法
con.addEventListener('mouseleave',(e)=>{
if(isOut){
//獲取出去的鼠標位置
//生成元素的位置=進入點距離窗口的距離-父盒子距離窗口的距離
let inx=e.clientX-e.target.offsetLeft;
let iny=e.clientY-e.target.offsetTop;
//
$('.container span').removeClass('in');//移除進入的動畫
$('.container span').addClass('out');//添加出去的動畫
$('.out').css('left',inx+'px');//添加出去的座標
$('.out').css('top',iny+'px');
isOut=false;//關閉出去的大門
//當動畫結束後再刪除元素
setTimeout(()=>{
con.removeChild(span);//刪除元素
isIn=true;//打開進來的大門
},500)
}
})
</script>
<script src="js/vendor/modernizr-3.8.0.min.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script>window.jQuery || document.write('<script src="js/vendor/jquery-3.4.1.min.js"><\/script>')</script>
<script src="js/plugins.js"></script>
<script src="js/main.js"></script>
<!-- Google Analytics: change UA-XXXXX-Y to be your site's ID. -->
<script>
window.ga = function () { ga.q.push(arguments) }; ga.q = []; ga.l = +new Date;
ga('create', 'UA-XXXXX-Y', 'auto'); ga('set','transport','beacon'); ga('send', 'pageview')
</script>
<script src="https://www.google-analytics.com/analytics.js" async></script>
</body>
</html>
0x02 CSS
*{
margin: 0;
padding: 0;
}
body{
/* 設置窗口的高度爲100%的窗口高度 */
height: 100vh;
/* 伸縮盒子模型 */
display: flex;
/* 下面兩個屬性讓body裏的子類居中 */
justify-content: center;
align-items: center;
background-color: #2c3e50;
}
.container{
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
width: 300px;
height: 450px;
border-radius: 20px;
background-color: #34495e;
box-shadow: 15px 15px 10px rgba(33,45,58,0.3);
overflow: hidden;
position: relative;
}
.container form{
width: 300px;
height: 200px;
display: flex;
justify-content: space-around;
flex-direction: column;
align-items: center;
z-index: 1;
}
.container form .tbx{
width: 200px;
height: 40px;
outline: none;
border: none;
border-bottom: 1px solid #fff;
background:none;
color: #fff;
}
/* 設置placeholder */
.container form .tbx::placeholder{
color: #fff;
font-size: 15px;
}
.container form .sub{
width: 200px;
height: 40px;
outline: none;
border: 1px solid #fff;
border-radius: 20px;
letter-spacing: 5px;
color: #fff;
background: none;
cursor: pointer;
}
.container h1{
z-index: 1;
color: #ecf0f1;
letter-spacing: 5px;
padding-left: 5px;
font-size: 50px;
font-weight: 100;
text-shadow: 5px 5px 5px rgba(33,45,58,0.3);
}
/* 設置鼠標進入的樣式 */
.container .in{
position: absolute;
top: 0;
left: 0;
display: block;
width: 0;
height: 0;
border-radius: 50%;
background: #3498bd;
transform: translate(-50%,-50%);
/* 使用in動畫,持續0.5s,緩出的時間函數,停留在最後一幀 */
animation: in 0.5s ease-out forwards;
}
/* 設置鼠標離開的樣式 */
.container .out{
position: absolute;
top: 0;
left: 0;
display: block;
width: 1200px;
height: 1200px;
border-radius: 50%;
background: #3498bd;
transform: translate(-50%,-50%);
animation: out 0.5s ease-out forwards;
}
/* 設置鼠標進入時,元素的動畫 */
@keyframes in{
/* 初始關鍵幀 */
0%{
width: 0;
height: 0;
}
/* 結尾關鍵幀 */
100%{
width: 1200px;
height: 1200px;
}
}
/* 設置鼠標離開時,元素的動畫 */
@keyframes out{
/* 初始關鍵幀 */
0%{
width: 1200px;
height: 1200px;
}
/* 結尾關鍵幀 */
100%{
width: 0;
height: 0;
}
}
.mouse-img {
width:40px;
height:40px;
position: absolute;
}
0x03 效果