原生JS+CSS實現(xiàn)炫酷重力模擬彈跳系統(tǒng)的登錄頁面
今天小編把之前保存的js特效視頻看了一遍,跟著視頻敲了敲嘻嘻,用原生js實現(xiàn)一個炫酷的登錄頁面。怎么個炫酷法呢,看看下面的圖片大家就知道啦。
效果圖:

不過在看代碼之前呢,大家先和小穎看看css中的opacity、transition、box-shadow這三個屬性。
1.opacity
CSS3 opacity 屬性
實例
設置一個div元素的透明度級別:
div
{
opacity:0.5;
}
在此頁底部有更多的例子。
瀏覽器支持
Internet ExplorerFirefoxOperaGoogle ChromeSafari
所有主流瀏覽器都支持opacity屬性。.
注意:IE8和早期版本支持另一種過濾器屬性。像:filter:Alpha(opacity=50)
屬性定義及使用說明
Opacity屬性設置一個元素了透明度級別。
| 默認值: | 1 |
|---|---|
| 繼承: | no |
| 版本: | CSS3 |
| JavaScript 語法: | object.style.opacity=0.5 |
語法
opacity: value|inherit;
| 值 | 描述 |
|---|---|
| value | 指定不透明度。從0.0(完全透明)到1.0(完全不透明) |
| inherit | Opacity屬性的值應該從父元素繼承 |
2.transition
作用:將元素從一種樣式逐漸改變?yōu)榱硪环N的效果。
定義和用法
transition 屬性是一個簡寫屬性,用于設置四個過渡屬性:
- transition-property
- transition-duration
- transition-timing-function
- transition-delay
注釋:請始終設置 transition-duration 屬性,否則時長為 0,就不會產生過渡效果。
語法
transition: property duration timing-function delay;
| 值 | 描述 |
|---|---|
| transition-property | 規(guī)定設置過渡效果的 CSS 屬性的名稱。 |
| transition-duration | 規(guī)定完成過渡效果需要多少秒或毫秒。 |
| transition-timing-function | 規(guī)定速度效果的速度曲線。 |
| transition-delay | 定義過渡效果何時開始。 |
3.box-shadow
作用:給元素添加陰影效果。
定義和用法
box-shadow 屬性向框添加一個或多個陰影。
提示:請使用 border-image-* 屬性來構造漂亮的可伸縮按鈕!
| 默認值: | none |
|---|---|
| 繼承性: | no |
| 版本: | CSS3 |
| JavaScript 語法: | object.style.boxShadow="10px 10px 5px #888888" |
語法
box-shadow: h-shadow v-shadow blur spread color inset;
注釋:box-shadow 向框添加一個或多個陰影。該屬性是由逗號分隔的陰影列表,每個陰影由 2-4 個長度值、可選的顏色值以及可選的 inset 關鍵詞來規(guī)定。省略長度的值是 0。
| 值 | 描述 | 測試 |
|---|---|---|
| h-shadow | 必需。水平陰影的位置。允許負值。 | 測試 |
| v-shadow | 必需。垂直陰影的位置。允許負值。 | 測試 |
| blur | 可選。模糊距離。 | 測試 |
| spread | 可選。陰影的尺寸。 | 測試 |
| color | 可選。陰影的顏色。請參閱 CSS 顏色值。 | 測試 |
| inset | 可選。將外部陰影 (outset) 改為內部陰影。 | 測試 |
怎么實現(xiàn)的呢,哈哈哈,代碼看這里:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>夢幻登錄</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
list-style: none;
}
body {
overflow: hidden;
}
#bg_wrap {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
overflow: hidden;
}
#bg_wrap div {
width: 100%;
height: 100%;
position: absolute;
left: 0;
top: 0;
opacity: 0;
/* 設置透明度 */
transition: opacity 3s;
}
/* nth-of-type(1) *篩選選擇器選擇第一個*/
#bg_wrap div:nth-of-type(1) {
opacity: 1;
}
#Login {
width: 272px;
height: 300px;
margin: 200px auto;
}
#Login .move {
position: absolute;
top: -100px;
z-index: 999;
}
#Login h3 {
width: 270px;
font-size: 30px;
font-weight: 700;
color: #fff;
font-family: '微軟雅黑';
text-align: center;
margin-bottom: 30px;
cursor: move;
/* top: 100px; */
}
/* #username {
top: 170px;
}
#password {
top: 225px;
} */
#Login input.text {
width: 270px;
height: 42px;
color: #fff;
background: rgba(45, 45, 45, 0.15);
border-radius: 6px;
border: 1px solid rgba(255, 255, 255, 0.15);
box-shadow: 0 2px 3px 0 rgba(0, 0, 0, 1.0) inset;
text-indent: 10px;
}
#Login input.btn {
/* top: 280px; */
background: #ef4300;
width: 272px;
height: 44px;
border-radius: 6px;
color: #fff;
box-shadow: 0 15px 30px 0 rgba(255, 255, 255, 0.25) inset, 0 2px 7px 0 rgba(0, 0, 0, 0.2);
/* -webkit-box-shadow: 0 15px 30px 0 rgba(255, 255, 255, 0.25) inset, 0 2px 7px 0 rgba(0, 0, 0, 0.2);
-moz-box-shadow: 0 15px 30px 0 rgba(255, 255, 255, 0.25) inset, 0 2px 7px 0 rgba(0, 0, 0, 0.2); */
border: 0;
text-align: center;
}
/* #Login input.focus {
outline: none;
box-shadow: 0 2px 3px 0 rgba(0, 0, 0, 0.2) inset;
} */
input::-webkit-input-placeholder {
color: #fff;
}
</style>
</head>
<body>
<div id="bg_wrap">
<div><img src="images/1.jpg" width="100%" height="100%"></div>
<div><img src="images/2.jpg" width="100%" height="100%"></div>
<div><img src="images/3.jpg" width="100%" height="100%"></div>
</div>
<div id="Login">
<h3 id="title" class="move">User Login</h3>
<form action="#" method="post" target="_blank">
<input type="text" placeholder="UserName" name="username" id="username" class="text move">
<input type="password" placeholder="PassWord" name="password" id="password" class="text move">
<input type="submit" value="Sign in" class="btn move" id="submit">
</form>
</div>
<script type="text/javascript">
/*背景漸變*/
/*function(){} 匿名函數(shù)
()() IIFE匿名函數(shù)立刻執(zhí)行,函數(shù)自執(zhí)行體*/
(function() {
var timer = null; //聲明定時器
var oImg = document.querySelectorAll('#bg_wrap div') //h5最新元素獲取寫法獲取到的是一組元素
//querySelector獲取單個元素的 兼容ie8
var len = oImg.length; //3
var index = 0;
timer = setInterval(function() {
oImg[index].style.opacity = 0;
index++;
// if(index>=3){
// index=0;
// }
index %= len; //index=index%len求模取余 0%3=0; 1%3=1; 2%3=2; 3%3=0;
oImg[index].style.opacity = 1;
}, 2000);
})();
// 重力模擬彈跳系統(tǒng)
(function() {
/*
改變定位元素的top值
達到指定位置之后進行彈跳一次
多個元素一次運動
動畫序列*/
var oMove = document.querySelectorAll('.move');
var oLen = oMove.length;
var timer = null;
var timeout = null;
var speed = 3; //移動距離
move(oLen - 1);
function move(index) {
if (index < 0) {
clearInterval(timer); //清除循環(huán)定時器
clearTimeout(timeout); //清除延時定時器
return; //終止函數(shù)
}
var endTop = 150 + (index * 60); //根據(jù)下標計算endTop值
timer = setInterval(function() {
speed += 3;
var T = oMove[index].offsetTop + speed; //設置每一次的top值
if (T > endTop) {
T = endTop;
speed *= -1 //取反,讓移動距離變?yōu)樨摂?shù)
speed *= 0.4;
//慢慢停下來
}
oMove[index].style.top = T + 'px';
}, 20);
timeout = setTimeout(function() {
clearInterval(timer);
index--;
console.log(9);
move(index);
console.log(index);
}, 900) //過900毫秒之后再執(zhí)行方法里的代碼
}
})()
</script>
</body>
</html>
總結
以上所述是小編給大家介紹的原生JS+CSS實現(xiàn)炫酷重力模擬彈跳系統(tǒng)的登錄頁面,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
JavaScript實現(xiàn)Tab標簽頁切換的最簡便方式(4種)
這篇文章主要介紹了JavaScript實現(xiàn)Tab標簽頁切換的最簡便方式(4種),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
解決iframe的frameborder在chrome/ff/ie下的差異
最近的項目中使用了動態(tài)創(chuàng)建iframe的js方法,發(fā)現(xiàn)iframe.frameborder="0"在IE7下不管用,而chrome/ff都正常的,很是郁悶。2010-08-08

