js定時(shí)器+簡單的動畫效果實(shí)例
1.向下滑動
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>向下滑動</title>
<style>
body {
margin: 0px;
}
#show {
width: 200px;
/* 高度為 0 */
height: 100px;
background-color: lightcoral;
margin: 0 auto;
/* 設(shè)置為隱藏 */
/*display: none;*/
}
</style>
</head>
<body>
<div id="show"></div>
<script>
var show = document.getElementById('show');
/*show.style.display = 'block';
var t = setInterval(function(){
var style = window.getComputedStyle(show,null);
var height = parseInt(style.height);
// 判斷當(dāng)前的高度是否為 400
if (height >= 400){
clearInterval(t);
} else {
height++;
show.style.height = height + 'px';
}
},50);*/
slideDown(show,400);
/*
將上述實(shí)現(xiàn)的向下滑動效果,封裝在一個固定的函數(shù)中
* 設(shè)計(jì)當(dāng)前實(shí)現(xiàn)向下滑動效果函數(shù)的形參
* elem - 表示實(shí)現(xiàn)向下滑動效果的元素
* maxHeight - 表示元素向下滑動的最大高度值
* 函數(shù)的邏輯與默認(rèn)設(shè)置CSS樣式屬性的值無關(guān)
*/
function slideDown(elem, maxHeight){
// 操作的元素默認(rèn)的display值為none
elem.style.display = 'block';
elem.style.height = '0px';
var t = setInterval(function(){
var style = window.getComputedStyle(elem,null);
var height = parseInt(style.height);
// 判斷當(dāng)前的高度是否為 400
if (height >= maxHeight){
clearInterval(t);
} else {
height++;
elem.style.height = height + 'px';
}
},50);
}
</script>
</body>
</html>
2.移動效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>移動效果</title>
<style>
body {
margin: 0px;
}
#box {
width: 100px;
height: 100px;
background-color: lightcoral;
position: absolute;
left: 100px;
top: 100px;
}
</style>
</head>
<body>
<div id="box"></div>
<script>
var box = document.getElementById('box');
box.onclick = function(){
clearInterval(t);
}
/*
* 向右移動
* 當(dāng)前元素移動到頁面的最右邊時(shí) -> 向左移動
* 向左移動
* 當(dāng)前元素移動到頁面的最左邊時(shí) -> 向右移動
*/
var flag = false;// 默認(rèn)表示向右
var speed = 1;// 表示每次變化的值
t = setInterval(function(){
//speed += 0.01;
// 獲取當(dāng)前頁面的寬度
var WIDTH = window.innerWidth;
var style = window.getComputedStyle(box,null);
var left = parseInt(style.left);
var width = parseInt(style.width);
// 判斷當(dāng)前元素移動的方向
if (flag){// 向左移動
left -= speed;
} else {// 向右移動
left += speed;
}
// 判斷什么情況下,向左移動;判斷什么情況下,向右移動
if ((left + width) >= WIDTH){// 向左移動
flag = true;
} else if (left <= 0){// 向右移動
flag = false;
}
box.style.left = left + 'px';
},10);
</script>
</body>
</html>
3.事件與動畫結(jié)合
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>事件與動畫結(jié)合</title>
<style>
body {
margin: 0px;
}
</style>
</head>
<body>
<script>
// 獲取<body>元素
var body = document.body;
// 當(dāng)頁面加載完畢后,設(shè)置當(dāng)前<body>元素的高度為當(dāng)前瀏覽器窗口的高度
window.onload = function(){
setHeight(body);
};
// 當(dāng)用戶改變?yōu)g覽器窗口的大小時(shí),重新設(shè)置<body>元素的高度(等于當(dāng)前窗口的高度)
window.onresize = function(){
setHeight(body);
};
// 定義函數(shù) - 設(shè)置<body>元素的高度等于當(dāng)前窗口的高度
function setHeight(elem){
elem.style.height = window.innerHeight + 'px';
}
var width = 100,height = 100;
// 為<body>元素綁定click事件
body.onclick = function(event){
var x = event.clientX;
var y = event.clientY;
// 創(chuàng)建<div>元素,顯示的位置在鼠標(biāo)當(dāng)前的坐標(biāo)值
var div = document.createElement('div');
div.setAttribute('class','circle');
body.appendChild(div);
// rgb(0,0,0)格式 -> 顏色隨機(jī)
var r = parseInt(Math.random()*255);
var g = parseInt(Math.random()*255);
var b = parseInt(Math.random()*255);
div.style.width = width + 'px';
div.style.height = height + 'px';
div.style.backgroundColor = 'rgb('+r+','+g+','+b+')';
div.style.borderRadius = '50%';
div.style.opacity = 1;
div.style.position = 'absolute';
div.style.left = x - width/2 + 'px';
div.style.top = y - height/2 + 'px';
animate(div);
}
// 定義函數(shù) -> 實(shí)現(xiàn)動畫效果
function animate(elem){
var style = window.getComputedStyle(elem,null);
/*var width = parseInt(style.width);
var height = parseInt(style.height);
var left = parseInt(style.left);
var top = parseInt(style.top);
width++;
height++;
elem.style.width = width + 'px';
elem.style.height = height + 'px';
elem.style.left = (left - 0.5) + 'px';
elem.style.top = (top - 0.5) +'px';*/
var opacity = style.opacity;
if (opacity <= 0){
clearTimeout(t);
// 刪除當(dāng)前元素
}
opacity -= 0.01;
elem.style.opacity = opacity;
// 設(shè)定定時(shí)器
var t = setTimeout(function(){
animate(elem);
},50);
}
</script>
</body>
</html>
以上這篇js定時(shí)器+簡單的動畫效果實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- js定時(shí)器實(shí)現(xiàn)倒計(jì)時(shí)效果
- JavaScript定時(shí)器詳解及實(shí)例
- JavaScript暫停和繼續(xù)定時(shí)器的實(shí)現(xiàn)方法
- JavaScript 定時(shí)器 SetTimeout之定時(shí)刷新窗口和關(guān)閉窗口(代碼超簡單)
- JavaScript定時(shí)器setTimeout()和setInterval()詳解
- Javascript 定時(shí)器調(diào)用傳遞參數(shù)的方法
- js 定時(shí)器setTimeout無法調(diào)用局部變量的解決辦法
- JS動畫定時(shí)器知識總結(jié)
- js 遞歸和定時(shí)器的實(shí)例解析
- JavaScript定時(shí)器設(shè)置、使用與倒計(jì)時(shí)案例詳解
相關(guān)文章
JavaScript?canvas?實(shí)現(xiàn)用代碼畫畫
這篇文章主要為大家介紹了JavaScript?canvas?實(shí)現(xiàn)用代碼畫畫示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Bootstrap告警框(alert)實(shí)現(xiàn)彈出效果和短暫顯示后上浮消失的示例代碼
這篇文章主要介紹了Bootstrap告警框(alert)實(shí)現(xiàn)彈出效果和短暫顯示后上浮消失,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
javascript基礎(chǔ)進(jìn)階_深入剖析執(zhí)行環(huán)境及作用域鏈
下面小編就為大家?guī)硪黄猨avascript基礎(chǔ)進(jìn)階_深入剖析執(zhí)行環(huán)境及作用域鏈。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
JavaScript實(shí)現(xiàn)星星等級評價(jià)功能
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)星星等級評價(jià)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
js獲取多個tagname的節(jié)點(diǎn)數(shù)組
寫了個獲取多個tagname節(jié)點(diǎn)集合的小方法。類似于jQuery的$(‘iput,select,textarea’,'#form’)的效果,返回是按節(jié)點(diǎn)在原有文檔流中的順序返回的2013-09-09
通過學(xué)習(xí)bootstrop導(dǎo)航條學(xué)會修改bootstrop顏色基調(diào)
這篇文章主要介紹了通過學(xué)習(xí)bootstrop導(dǎo)航條學(xué)會修改bootstrop顏色基調(diào),需要的朋友可以參考下2017-06-06
實(shí)例詳解JavaScript中setTimeout函數(shù)的執(zhí)行順序
關(guān)于javascript的運(yùn)行機(jī)制大家都應(yīng)該有所了解了吧,其實(shí)javascript是一個單線程的機(jī)制,但是因?yàn)殛?duì)列的關(guān)系它的表現(xiàn)會讓我們感覺是一個多線程的錯覺。下面這篇文章通過實(shí)例主要給大家介紹了關(guān)于JavaScript中setTimeout函數(shù)執(zhí)行順序的相關(guān)資料,需要的朋友可以參考下。2017-07-07

