offsetWidth和offsetHight 以及offsetLeft和offsetTop以及offsetParent
共同組成了offset家族逊朽。
offsetWidth和offsetHight(檢測盒子自身寬高+padding+border)
offsetWidth = width+padding+border赤惊;
offsetHeight = Height+padding+border长已;
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
div {
width: 100px;
height: 100px;
padding: 10px;
border: 10px solid #000;
margin: 100px;
background-color: pink;
}
</style>
</head>
<body>
<div class="box"></div>
<script>
var div = document.getElementsByTagName("div")[0];
//offsetHeight和offsetWidth: 可以檢測盒子的寬高技俐。
//包括寬高本身,padding吏口,border奄容。不包括margin
//offsetHeight = height+padding+border;(不加margin)
//offsetWidth = width+padding+border;(不加margin)
console.log(div.offsetHeight); 140
console.log(typeof div.offsetHeight); //number
</script>
</body>
</html>
offsetLeft和offsetTop(檢測距離父盒子有定位的左/上面的距離)
返回距離上級盒子(帶有定位)左邊s的位置
如果父級都沒有定位則以body為準
offsetLeft 從父親的padding 開始算,父親的border 不算。
在父盒子有定位的情況下产徊,offsetLeft == style.left(去掉px)
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.box1 {
width: 300px;
height: 300px;
padding: 100px;
margin: 100px;
position: relative;
border: 100px solid #000;
background-color: pink;
}
.box2 {
width: 100px;
height: 100px;
background-color: red;
/*position: absolute;*/
/*left: 10px;*/
/*top: 10px;*/
}
</style>
</head>
<body>
<div class="box1">
<div class="box3">
<div class="box2" style="left: 10px"></div>
</div>
</div>
<script>
var box2 = document.getElementsByClassName("box2")[0];
//offsetTop和offsetLeft
console.log(box2.offsetLeft); //100
console.log(box2.style.left); //10px
</script>
</body>
</html>
offsetParent 檢測父系盒子中帶有定位的父盒子節(jié)點
1昂勒、返回該對象的父級 (帶有定位)
如果當前元素的父級元素沒有進行CSS定位(position為absolute或 relative,fixed)舟铜, offsetParent為body戈盈。
2、如果當前元素的父級元素中有CSS定位 (position為absolute或 relative谆刨,fixed)塘娶,offsetParent取最近的那個父級元素归斤。
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div class="box1" style="position: absolute;">
<div class="box2" style="position: fixed;">
<div class="box3"></div>
</div>
</div>
<script>
//offsetParent:復習盒子中帶有定位的盒子
//復習盒子中都沒有定位,返回body
//如果有血柳,誰有返回最近哪個
var box3 = document.getElementsByClassName("box3")[0];
console.log(box3.offsetParent);
</script>
</body>
</html>
offsetLeft和style.left的區(qū)別
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.box1 {
width: 300px;
height: 300px;
padding: 100px;
margin: 100px;
position: relative;
border: 100px solid #000;
background-color: pink;
}
.box2 {
width: 100px;
height: 100px;
background-color: red;
position: absolute;
left: 10px;
top: 10px;
}
</style>
</head>
<body>
<div class="box1">
<div class="box3">
<div class="box2" style="left: 10px"></div>
</div>
</div>
<script>
var box2 = document.getElementsByClassName("box2")[0];
//offsetTop和offsetLeft
console.log(box2.offsetLeft);
console.log(box2.style.left);
// 一官册、最大區(qū)別在于offsetLeft可以返回沒有定位盒子的距離左側(cè)的位置生兆。
//如果父系盒子中都沒有定位难捌,以body為準。
//style.left只能獲取行內(nèi)式鸦难,如果沒有返回“”;
// 二根吁、offsetTop 返回的是數(shù)字,而 style.top 返回的是字符串合蔽,除了數(shù)字外還帶有單位:px击敌。
//div.offsetLeft = 100; div.style.left = "100px";
// 三、offsetTop 只讀拴事,而 style.top 可讀寫沃斤。(只讀是獲取值,可寫是賦值)
//style.left和style.top可以賦值
//offsetLeft和offsetTop只能獲取值
// 四刃宵、如果沒有給 HTML 元素指定過 top 樣式衡瓶,則style.top 返回的是空字符串。
//style.left只能獲取行內(nèi)式牲证,如果沒有返回“”;
</script>
</body>
</html>
動畫封裝勻速運動
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
.box1 {
margin: 0;
padding: 5px;
height: 300px;
background-color: #ddd;
position: relative;
}
button {
margin: 5px;
}
.box2 {
width: 100px;
height: 100px;
background-color: red;
position: absolute;
left: 0;
top: 40px;
}
.box3 {
width: 100px;
height: 100px;
background-color: yellow;
position: absolute;
left: 0;
top: 150px;
}
</style>
</head>
<body>
<div class="box1">
<button>運動到200</button>
<button>運動到400</button>
<div class="box2"></div>
<div class="box3"></div>
</div>
<script>
var btnArr = document.getElementsByTagName("button");
var box2 = document.getElementsByClassName("box2")[0];
var box3 = document.getElementsByClassName("box3")[0];
//綁定事件
btnArr[0].onclick = function () {
//如果有一天我們要傳遞另外一個盒子哮针,那么我們的方法就不好用了
//所以我們要增加第二個參數(shù),被移動的盒子本身坦袍。
animate(box2,200);
animate(box3,200);
}
btnArr[1].onclick = function () {
animate(box2,400);
animate(box3,400);
}
function animate(ele,target){
//要用定時器十厢,先清除定時器
//一個盒子只能有一個定時器,這樣兒的話捂齐,不會和其他盒子出現(xiàn)定時器沖突
//而定時器本身講成為盒子的一個屬性
clearInterval(ele.timer);
//我們要求盒子既能向前又能向后蛮放,那么我們的步長就得有正有負
//目標值如果大于當前值取正,目標值如果小于當前值取負
var speed = target>ele.offsetLeft?10:-10;
ele.timer = setInterval(function () {
//在執(zhí)行之前就獲取當前值和目標值之差
var val = target - ele.offsetLeft;
ele.style.left = ele.offsetLeft + speed + "px";
//目標值和當前值只差如果小于步長奠宜,那么就不能在前進了
//因為步長有正有負筛武,所有轉(zhuǎn)換成絕對值來比較
if(Math.abs(val)<Math.abs(speed)){
ele.style.left = target + "px";
clearInterval(ele.timer);
}
},30)
}
</script>
</body>
</html>
滑動焦點圖
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 490px;
height: 170px;
padding: 5px;
border: 1px solid #ccc;
margin: 100px auto;
}
.inner {
width: 490px;
height: 170px;
position: relative;
overflow: hidden;
}
ul {
width: 500%;
list-style: none;
position: absolute;
left: 0;
}
li {
float: left;
}
.square {
position: absolute;
bottom: 10px;
right: 10px;
}
span {
display: inline-block;
width: 16px;
height: 16px;
background-color: #fff;
text-align: center;
margin: 3px;
border: 1px solid #ccc;
line-height: 16px;
cursor: pointer;
}
.current {
background-color: darkorange;
color: #fff;
}
</style>
<script>
window.onload = function () {
//需求:鼠標放到小方塊上面,上面的圖片(ul向左移動指定的個數(shù)個圖片的寬)
//思路:綁定事件挎塌,點亮指定的盒子徘六,移動ul。
//步驟:
//1.獲取事件源和相關元素
//2.綁定事件和書寫事件驅(qū)動程序(for循環(huán)綁定)
//3.點亮盒子
//4.移動ul(向左移動榴都,給定目標為-值待锈,放到第n個小方塊上向左移動n-1個圖片的寬)
//1.獲取事件源和相關元素
var inner = document.getElementById("inner");
var imgWidth = inner.offsetWidth;
var ul = inner.children[0];
var spanArr = inner.children[1].children;
//2.綁定事件和書寫事件驅(qū)動程序(for循環(huán)綁定)
for(var i=0;i<spanArr.length;i++){
//屬性綁定(自定義屬性參數(shù)盒子的索引值)
//用span的innerHTML屬性也可以,但是為了代碼的健壯性嘴高,我們選擇使用自己的屬性
spanArr[i].index = i; //綁定的是索引值竿音,所以移動盒子的時候不用-1
spanArr[i].onmouseover = function () {
//3.點亮盒子
//排他思想
for(var j=0;j<spanArr.length;j++){
spanArr[j].className = "";
}
this.className = "current";
//4.移動ul(向左移動和屎,給定目標為-值,放到第n個小方塊上向左移動n-1個圖片的寬)
//利用框架移動盒子春瞬。(兩個參數(shù):1.元素柴信。 2.目標位置)
animate(ul,-this.index*imgWidth);
}
}
function animate(ele,target){
clearInterval(ele.timer);
var speed = target>ele.offsetLeft?10:-10;
ele.timer = setInterval(function () {
var val = target - ele.offsetLeft;
ele.style.left = ele.offsetLeft + speed + "px";
if(Math.abs(val)<Math.abs(speed)){
ele.style.left = target + "px";
clearInterval(ele.timer);
}
},10)
}
}
</script>
</head>
<body>
<div class="box">
<div class="inner" id="inner">
<ul>
<li><img src="images/01.jpg" alt=""/></li>
<li><img src="images/02.jpg" alt=""/></li>
<li><img src="images/03.jpg" alt=""/></li>
<li><img src="images/04.jpg" alt=""/></li>
<li><img src="images/05.jpg" alt=""/></li>
</ul>
<div class="square">
<span class="current">1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</div>
</div>
</div>
</body>
</html>
左右焦點圖
<!doctype html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<style type="text/css">
body,ul,ol,li,img{margin:0;padding:0;list-style:none;}
#box{width:490px;height:170px;padding:5px;
position: relative;border:1px solid #ccc;margin:100px auto 0;overflow:hidden;}
.ad{width:490px;height:170px;overflow:hidden;position:relative;}
#box img{width:490px;}
.ad ol{position:absolute;right:10px;bottom:10px;}
.ad ol li{width:20px;height:20px;line-height:20px;border:1px solid #ccc;text-align:center;background:#fff;float:left;margin-right:10px;cursor:pointer;_display:inline;}
.ad ol li.current{background:yellow;}
.ad ul li{float:left;}
.ad ul{ position:absolute; top:0; width:2940px;}
.ad ul li.current{display:block;}
#arr {display: none;}
#arr span{ width:40px; height:40px; position:absolute; left:5px; top:50%; margin-top:-20px; background:#000; cursor:pointer; line-height:40px; text-align:center; font-weight:bold; font-family:'黑體'; font-size:30px; color:#fff; opacity:0.3; border:1px solid #fff;}
#arr #right{right:5px; left:auto;}
</style>
</head>
<body>
<div id="box" class="all">
<div class="ad">
<ul id="imgs">
<li><img src="images/1.jpg" /></li>
<li><img src="images/2.jpg" /></li>
<li><img src="images/3.jpg" /></li>
<li><img src="images/4.jpg" /></li>
<li><img src="images/5.jpg" /></li>
</ul>
</div>
<div id="arr">
<span id="left"><</span>
<span id="right">></span>
</div>
</div>
<script>
//需求:鼠標放到盒子上,顯示左右切換的圖片宽气。點擊左則按鈕圖片(ul)向右移動随常,相反的按鈕向左移動。
//思路:獲取兩個按鈕萄涯,點擊左側(cè)按鈕移動ul向右走(每次只走一張)(計數(shù)器從0開始)绪氛。
//如何移動盒子。利用計數(shù)器模擬index值涝影,點右側(cè)自增枣察,點左側(cè)自減。
//步驟:
//1.鼠標放上去顯示移開以藏
//2.點擊右側(cè)盒子圖片向做移動并用計數(shù)器模擬index值燃逻。
//3序目,點擊左側(cè)盒子,同理伯襟。
//1.鼠標放上去顯示移開以藏
var box = document.getElementById("box");
var imgWidth = box.children[0].offsetWidth;
var ul = box.children[0].children[0];
var boxLeftRight = box.children[1];
var btnArr = boxLeftRight.children;
//鼠標放上去顯示猿涨,移開隱藏
box.onmouseover = function () {
boxLeftRight.style.display = "block";
}
box.onmouseout = function () {
boxLeftRight.style.display = "none";
}
//2.點擊右側(cè)盒子圖片向做移動并用計數(shù)器模擬index值。
//定義計數(shù)器
var index = 0;
btnArr[1].onclick = function () {
index++;
//我們要對index的值進行約束逗旁。index的值必須在[0,4]
if(index>ul.children.length-1){
index = ul.children.length-1;
alert("到頭了嘿辟!");
}
//點擊盒子以后移動圖片(ul,和目標位置)
animate(ul,-index*imgWidth);
}
//3片效,點擊左側(cè)盒子红伦,同理。
btnArr[0].onclick = function () {
index--;
if(index<0){
index = 0;
alert("第一張淀衣!");
}
//點擊盒子以后移動圖片(ul昙读,和目標位置)
animate(ul,-index*imgWidth);
}
function animate(ele,target){
clearInterval(ele.timer);
var speed = target>ele.offsetLeft?10:-10;
ele.timer = setInterval(function () {
var val = target - ele.offsetLeft;
ele.style.left = ele.offsetLeft + speed + "px";
if(Math.abs(val)<Math.abs(speed)){
ele.style.left = target + "px";
clearInterval(ele.timer);
}
},10)
}
</script>
</body>
</html>
12-帶有定時器的無縫輪播圖
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
<style type="text/css">
*{ padding:0; margin:0; list-style:none; border:0;}
.all{
width:500px;
height:200px;
padding:7px;
border:1px solid #ccc;
margin:100px auto;
position:relative;
}
.screen{
width:500px;
height:200px;
overflow:hidden;
position:relative;
}
.screen li{ width:500px; height:200px; overflow:hidden; float:left;}
.screen ul{ position:absolute; left:0; top:0px; width:3000px;}
.all ol{ position:absolute; right:10px; bottom:10px; line-height:20px; text-align:center;}
.all ol li{ float:left; width:20px; height:20px; background:#fff; border:1px solid #ccc; margin-left:10px; cursor:pointer;}
.all ol li.current{ background:yellow;}
#arr {display: none;}
#arr span{ width:40px; height:40px; position:absolute; left:5px; top:50%; margin-top:-20px; background:#000; cursor:pointer; line-height:40px; text-align:center; font-weight:bold; font-family:'黑體'; font-size:30px; color:#fff; opacity:0.3; border:1px solid #fff;}
#arr #right{right:5px; left:auto;}
</style>
<script>
window.onload = function () {
//需求:無縫滾動。
//思路:賦值第一張圖片放到ul的最后膨桥,然后當圖片切換到第五張的時候
// 直接切換第六章蛮浑,再次從第一張切換到第二張的時候先瞬間切換到
// 第一張圖片,然后滑動到第二張
//步驟:
//1.獲取事件源及相關元素只嚣。(老三步)
//2.復制第一張圖片所在的li,添加到ul的最后面沮稚。
//3.給ol中添加li,ul中的個數(shù)-1個册舞,并點亮第一個按鈕蕴掏。
//4.鼠標放到ol的li上切換圖片
//5.添加定時器
//6.左右切換圖片(鼠標放上去隱藏,移開顯示)
//1.獲取事件源及相關元素。(老三步)
var all = document.getElementById("all");
var screen = all.firstElementChild || all.firstChild;
var imgWidth = screen.offsetWidth;
var ul = screen.firstElementChild || screen.firstChild;
var ol = screen.children[1];
var div = screen.lastElementChild || screen.lastChild;
var spanArr = div.children;
//2.復制第一張圖片所在的li,添加到ul的最后面盛杰。
var ulNewLi = ul.children[0].cloneNode(true);
ul.appendChild(ulNewLi);
//3.給ol中添加li挽荡,ul中的個數(shù)-1個,并點亮第一個按鈕即供。
for(var i=0;i<ul.children.length-1;i++){
var olNewLi = document.createElement("li");
olNewLi.innerHTML = i+1;
ol.appendChild(olNewLi)
}
var olLiArr = ol.children;
olLiArr[0].className = "current";
//4.鼠標放到ol的li上切換圖片
for(var i=0;i<olLiArr.length;i++){
//自定義屬性定拟,把索引值綁定到元素的index屬性上
olLiArr[i].index = i;
olLiArr[i].onmouseover = function () {
//排他思想
for(var j=0;j<olLiArr.length;j++){
olLiArr[j].className = "";
}
this.className = "current";
//鼠標放到小的方塊上的時候索引值和key以及square同步
// key = this.index;
// square = this.index;
key = square = this.index;
//移動盒子
animate(ul,-this.index*imgWidth);
}
}
//5.添加定時器
var timer = setInterval(autoPlay,1000);
//固定向右切換圖片
//兩個定時器(一個記錄圖片,一個記錄小方塊)
var key = 0;
var square = 0;
function autoPlay(){
//通過控制key的自增來模擬圖片的索引值逗嫡,然后移動ul
key++;
if(key>olLiArr.length){
//圖片已經(jīng)滑動到最后一張青自,接下來,跳轉(zhuǎn)到第一張祸穷,然后在滑動到第二張
ul.style.left = 0;
key = 1;
}
animate(ul,-key*imgWidth);
//通過控制square的自增來模擬小方塊的索引值性穿,然后點亮盒子
//排他思想做小方塊
square++;
if(square>olLiArr.length-1){//索引值不能大于等于5勺三,如果等于5雷滚,立刻變?yōu)?;
square = 0;
}
for(var i=0;i<olLiArr.length;i++){
olLiArr[i].className = "";
}
olLiArr[square].className = "current";
}
//鼠標放上去清除定時器吗坚,移開后在開啟定時器
all.onmouseover = function () {
div.style.display = "block";
clearInterval(timer);
}
all.onmouseout = function () {
div.style.display = "none";
timer = setInterval(autoPlay,1000);
}
//6.左右切換圖片(鼠標放上去顯示祈远,移開隱藏)
spanArr[0].onclick = function () {
//通過控制key的自增來模擬圖片的索引值,然后移動ul
key--;
if(key<0){
//先移動到最后一張商源,然后key的值取之前一張的索引值车份,然后在向前移動
ul.style.left = -imgWidth*(olLiArr.length)+"px";
key = olLiArr.length-1;
}
animate(ul,-key*imgWidth);
//通過控制square的自增來模擬小方塊的索引值,然后點亮盒子
//排他思想做小方塊
square--;
if(square<0){//索引值不能大于等于5牡彻,如果等于5扫沼,立刻變?yōu)?;
square = olLiArr.length-1;
}
for(var i=0;i<olLiArr.length;i++){
olLiArr[i].className = "";
}
olLiArr[square].className = "current";
}
spanArr[1].onclick = function () {
//右側(cè)的和定時器一模一樣
autoPlay();
}
function animate(ele,target){
clearInterval(ele.timer);
var speed = target>ele.offsetLeft?10:-10;
ele.timer = setInterval(function () {
var val = target - ele.offsetLeft;
ele.style.left = ele.offsetLeft + speed + "px";
if(Math.abs(val)<Math.abs(speed)){
ele.style.left = target + "px";
clearInterval(ele.timer);
}
},10)
}
}
</script>
</head>
<body>
<div class="all" id='all'>
<div class="screen" id="screen">
<ul id="ul">
<li><img src="images/1.jpg" width="500" height="200" /></li>
<li><img src="images/2.jpg" width="500" height="200" /></li>
<li><img src="images/3.jpg" width="500" height="200" /></li>
<li><img src="images/4.jpg" width="500" height="200" /></li>
<li><img src="images/5.jpg" width="500" height="200" /></li>
</ul>
<ol>
</ol>
<div id="arr">
<span id="left"><</span>
<span id="right">></span>
</div>
</div>
</div>
</body>
</html>