三個取整函數
這三個函數都是數學函數, Math
- Math.ceil() 向上取整 天花板
console.log(Math.ceil(1.01))
結果 是 2
console.log(Math.ceil(1.9))
結果 2
console.log(Math.ceil(-1.3))
結果 是 -1 - Math.floor() 向下取整 地板
console.log(Math.floor(1.01))
結果 是 1
console.log(Math.floor(1.9))
結果 1
console.log(Math.floor(-1.3))
結果 是 -2 - Math.round() 四舍五入函數
console.log(Math.round(1.01))
結果 是 1
console.log(Math.round(1.9))
結果 是 2
緩動動畫
勻速動畫的原理: 盒子本身的位置 + 步長
緩動動畫的原理: 盒子本身的位置 + 步長 (不斷變化的)
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
div{
width: 100px;
height: 100px;
position: absolute;
background-color: pink;
left: 0;
}
</style>
<script type="text/javascript">
function $(id){return document.getElementById(id);}
window.onload = function () {
var box = $("box");
$("btn400").onclick = function () {
animate(box,400);
}
$("btn800").onclick = function () {
animate(box,800);
}
}
function animate(obj,target){
clearInterval(obj.timer);
obj.timer = setInterval(function () {
var step = (target - obj.offsetLeft)/10;
step = step>0?Math.ceil(step):Math.floor(step);
obj.style.left = obj.offsetLeft+step+"px";
if(obj.offsetLeft == target){
clearInterval(obj.timer);
}
},20);
}
</script>
</head>
<body>
<button id="btn400">400</button>
<button id="btn800">800</button>
<div id="box"></div>
</body>
</html>
JS常用訪問 CSS 屬性
我們訪問得到css 屬性,比較常用的有兩種:
- 利用點語法
box.style.width
box.style.top
如上點語法可以得到width
屬性和top
屬性,帶有單位的,如:100px
但是這個語法有非常大的缺陷, 不變的。 后面的width
和top
沒有辦法傳遞參數的。
不能這樣寫:var w = width;
box.style.w
- 利用 [] 訪問屬性
box.style["left"]);
元素.style[“屬性”];
語法格式: box.style[“width”]
最大的優點 : 可以給屬性傳遞參數
得到css 樣式
我們想要獲得css 的樣式, box.style.left
、box.style.backgorundColor
但是它只能得到行內的樣式。 但是我們工作最多用的是內嵌式
或者外鏈式
。
怎么辦? 核心: 我們怎么才能得到內嵌或者外鏈的樣式呢?
-
obj.currentStyle
IE、opera 常用
外部(使用<link>)和內嵌(使用<style>)樣式表中的樣式(IE和opera常用) -
window.getComputedStyle("元素", "偽類")
w3c
兩個選項是必須的, 沒有偽類 用 null 替代
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
div{
width: 100px;
height: 100px;
background-color: pink;
position: absolute;
left: 10px;
top: 0;
}
</style>
</head>
<body>
<div id="box"></div>
<script type="text/javascript">
var box = document.getElementById("box");
//IE和Opera支持
//IE所有版本中:100px,chrome中報錯
console.log(box.currentStyle["width"]);
//W3C支持的
//chrome和IE9+中:10px,IE6、7、8中報錯
console.log(window.getComputedStyle(box,null)["left"]);
</script>
</body>
</html>
- 兼容寫法 :
我們這個元素里面的屬性很多, left top width ===
我們想要某個屬性, 就應該 返回改屬性,所有繼續封裝返回當前樣式的 函數。
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
div{
width: 100px;
height: 100px;
background-color: pink;
position: absolute;
left: 20px;
top: 10px;
z-index: 2;
opacity: 0.4;
}
</style>
</head>
<body>
<div id="box"></div>
<script type="text/javascript">
var box = document.getElementById("box");
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return window.getComputedStyle(obj,null)[attr];
}
}
console.log(getStyle(box,"width"));//100px
console.log(getStyle(box,"background-color"));//IE中:pink,chrome中:rgb(255, 192, 203)
console.log(getStyle(box,"position"));//absolute
console.log(getStyle(box,"opacity"));//0.4
console.log(getStyle(box,"left"));//20px
console.log(getStyle(box,"z-index"));2
</script>
</body>
</html>
例:運動框架的基本函數(單個屬性)的封裝
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
div{
width: 100px;
height: 100px;
position: absolute;
background-color: pink;
left: 0;
top: 60px;
}
</style>
<script type="text/javascript">
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return window.getComputedStyle(obj,null)[attr];
}
}
function $(id){return document.getElementById(id);}
window.onload = function () {
var box = $("box");
$("btn400").onclick = function () {
animate(box,"height",600);
}
$("btn800").onclick = function () {
animate(box,"left",400);
}
}
function animate(obj, attr, target){
clearInterval(obj.timer);
obj.timer = setInterval(function () {
var current = parseInt(getStyle(obj,attr));
var step = (target - current)/10;
step = step>0?Math.ceil(step):Math.floor(step);
obj.style[attr] = current+step+"px";
if(current == target){
clearInterval(obj.timer);
}
},20)
}
</script>
</head>
<body>
<button id="btn400">600</button>
<button id="btn800">400</button>
<div id="box"></div>
</body>
</html>
JSON遍歷:for in遍歷
for in 關鍵字
for (變量 in 對象)
{ 執行語句; }
例:運動框架的基本函數(多個屬性)的封裝
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
div{
width: 100px;
height: 100px;
position: absolute;
background-color: pink;
left: 0;
top: 60px;
}
</style>
<script type="text/javascript">
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return window.getComputedStyle(obj,null)[attr];
}
}
function $(id){return document.getElementById(id);}
window.onload = function () {
var box = $("box");
$("btn400").onclick = function () {
animate(box,{left:200,top:200});
}
$("btn800").onclick = function () {
animate(box,{width:200,left:200,top:200});
}
function animate(obj , json){
clearInterval(obj.timer);
obj.timer = setInterval(function () {
/*
關閉定時器的思路,不用判斷多個屬性都到達target,
只要判斷有一個屬性未到target,定時器就不能停止
*/
//用戶判斷是否可以關閉定時器
var flag = true;
//開始遍歷
for(var attr in json){
//當前位置
var current = parseInt(getStyle(obj,attr));
//計算步長
//target = json[attr]
var step = (json[attr] - current)/10;
step = step>0?Math.ceil(step):Math.floor(step);
obj.style[attr] = current+step+"px";
if(current != json[attr]){
flag = false;
}
}
if(flag){//如果flag=true,說明多個屬性都=target了
clearInterval(obj.timer);
alert("動畫完成");
}
},20);
}
}
</script>
</head>
<body>
<button id="btn400">600</button>
<button id="btn800">400</button>
<div id="box"></div>
</body>
</html>
回調函數
例:仿360開機彈窗的動畫
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.box{
position: fixed;
right: 0;
bottom: 0;
}
img{
display: block;
}
span{
position: absolute;
/*background-color: pink;*/
width: 30px;
height: 30px;
right: 0;
top: 0;
cursor: pointer;
}
</style>
<script type="text/javascript">
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return window.getComputedStyle(obj,null)[attr];
}
}
function animate(obj , json , fn){
clearInterval(obj.timer);
obj.timer = setInterval(function () {
/*
關閉定時器的思路,不用判斷多個屬性都到達target,
只要判斷有一個屬性未到target,定時器就不能停止
*/
//用戶判斷是否可以關閉定時器
var flag = true;
//開始遍歷
for(var attr in json){
//當前位置
var current = parseInt(getStyle(obj,attr));
//計算步長
//target = json[attr]
var step = (json[attr] - current)/10;
step = step>0?Math.ceil(step):Math.floor(step);
obj.style[attr] = current+step+"px";
if(current != json[attr]){
flag = false;
}
}
if(flag){//如果flag=true,說明多個屬性都=target了
clearInterval(obj.timer);
if(fn) fn();
}
},20);
}
window.onload = function () {
function $(id){return document.getElementById(id);}
var box = $("box");
var hd = box.getElementsByTagName("div")[0];
var bd = box.getElementsByTagName("div")[1];
var close = $("close");
close.onclick = function () {
animate(bd,{height:0}, function () {
animate(hd.parentNode,{width:0});
});
}
}
</script>
</head>
<body>
<div class="box" id="box">
<span id="close"></span>
<div class="hd">

</div>
<div class="bd">

</div>
</div>
</body>
</html>
in 運算符
in運算符也是一個二元運算符,但是對運算符左右兩個操作數的要求比較嚴格。in運算符要求第1個(左邊的)操作數必須是字符串類型或可以轉換為字符串類型的其他類型,而第2個(右邊的)操作數必須是數組或對象。只有第1個操作數的值是第2個操作數的屬性名,才會返回true,否則返回false
<script type="text/javascript">
var json = {name:"李德華",age:"45"};
if("name" in json){
alert("YES");
}else{
alert("NO");
}
</script>
例:運動框架的函數封裝(帶透明度的)
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
div{
width: 100px;
height: 100px;
position: absolute;
background-color: pink;
left: 0;
top: 60px;
}
</style>
<script type="text/javascript">
function getStyle(obj,attr){
if(obj.currentStyle){
return obj.currentStyle[attr];
}else{
return window.getComputedStyle(obj,null)[attr];
}
}
function $(id){return document.getElementById(id);}
window.onload = function () {
var box = $("box");
$("btn400").onclick = function () {
animate(box,{left:200,top:200,opacity:40});
}
$("btn800").onclick = function () {
animate(box,{width:200,left:200,top:200});
}
function animate(obj , json){
clearInterval(obj.timer);
obj.timer = setInterval(function () {
/*
關閉定時器的思路,不用判斷多個屬性都到達target,
只要判斷有一個屬性未到target,定時器就不能停止
*/
//用戶判斷是否可以關閉定時器
var flag = true;
//開始遍歷
for(var attr in json){
//當前位置
var current;
if(attr == "opacity"){
if("opacity" in obj.style){
current = parseInt(getStyle(obj,attr)*100);
}else{//IE6、7、8
var alphaStr = getStyle(obj,"filter");
var cs = alphaStr.substring(alphaStr.lastIndexOf("=")+1 , alphaStr.lastIndexOf(")"));
current = isNaN(parseInt(cs))?100:parseInt(cs);
}
}else{
current = parseInt(getStyle(obj,attr));
}
//計算步長
//target = json[attr]
var step = (json[attr] - current)/10;
step = step>0?Math.ceil(step):Math.floor(step);
if(attr == "opacity"){//判斷用戶是否要修改opacity屬性
if("opacity" in obj.style){//判斷瀏覽器是否支持opacity屬性
obj.style.opacity = (current+step)/100;
}else{
obj.style.filter = "alpha(opacity="+(current+step)+")";
}
}else{
obj.style[attr] = current+step+"px";
}
if(current != json[attr]){
flag = false;
}
}
if(flag){//如果flag=true,說明多個屬性都=target了
clearInterval(obj.timer);
alert("動畫完成");
}
},20);
}
}
</script>
</head>
<body>
<button id="btn400">600</button>
<button id="btn800">400</button>
<div id="box"></div>
</body>
</html>
例:手風琴效果
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
ul,li{
list-style: none;
}
.box{
width: 1200px;
height: 400px;
margin: 50px auto;
border: 1px solid red;
overflow: hidden;
}
.box ul{
width: 120%;
height: 100%;
}
.box li{
float: left;
width: 240px;
height: 400px;
/*border: 1px solid red;*/
}
</style>
<script type="text/javascript" src="animate.js"></script>
<script type="text/javascript">
function $(id){return document.getElementById(id);}
window.onload = function () {
var lis = $("ul").children;
for(var i = 0;i<lis.length ; i++){
lis[i].style.backgroundImage = "url(images/"+(i+1)+".jpg)";
lis[i].onmouseover = function () {
for(var j = 0;j<lis.length ;j++){
animate(lis[j],{width:75});
}
animate(this,{width:900});
}
}
$("ul").onmouseout = function () {
for(var i = 0;i<lis.length ; i++){
animate(lis[i],{width:240});
}
}
}
</script>
</head>
<body>
<div class="box">
<ul id="ul">
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
</div>
</body>
</html>