JS三大系列
- 三大家族(offset/scroll/client)
- 事件對象 (event)
(事件被觸動時,鼠標和鍵盤的狀態)
(通過屬性控制)
offset家族
目的: js中有一套方便的獲取元素尺寸的辦法就是offset家族;
offsetWidth、offsetHeight
得到對象的寬度和高度(自己的,與他人無關)
offsetWidth = width + border + padding
div { width:220px; border-left:2px solid red; padding:10px;}
div.offsetWidth = 220 + 2 + 20
為什么不用 div.style.width
,因為div.style.width
只能得到行內式中設置的寬高的數值
offsetLeft、offsetTop
返回距離上級盒子(最近的帶有定位)左邊的位置
- 如果父級都沒有定位則以body 為準
這里的父級指的是所有上級節點, 不僅僅指的是父親, 還可以是 爺爺 曾爺爺 曾曾爺爺。。。。 - offsetLeft 從父親的padding 開始算 父親的border 不算
**總結一下: ** 就是子盒子到定位的父盒子邊框到邊框的距離
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.gradeFater{
width: 300px;
height: 300px;
background-color: #ccc;
}
.fater{
/*position: relative;*/
width: 200px;
height: 200px;
background-color: yellow;
padding: 10px;
border: 1px solid #cccccc;
}
.son{
width: 100px;
height: 100px;
background-color: pink;
}
</style>
<script type="text/javascript">
window.onload = function () {
var son = document.getElementById("son");
//沒有定位時,打印8,相對于body
//當在gradeFater上添加position: relative;打印11
//當在fater上添加position: relative;打印10
console.log(son.offsetTop);
}
</script>
</head>
<body>
<div class="gradeFater">
<div class="fater">
<div class="son" id="son"></div>
</div>
</div>
</body>
</html>
例:筋斗云
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>筋斗云</title>
<style type="text/css">
body{
background-color: #000;
}
body,ul,li{
margin: 0;
padding: 0;
}
ul,li{
list-style: none;
}
a{
text-decoration: none;
color: #333;
}
.nav{
width: 880px;
height: 42px;
margin: 100px auto;
border-radius: 5px;
background: url("images/rss.png") no-repeat right center #fff;
position: relative;
}
.cloud{
position: absolute;
width: 83px;
height: 42px;
background: url("images/cloud.gif") no-repeat left;
left: 0;
top: 0;
}
.nav ul{
position: absolute;
left: 0;
top: 0;
}
.nav ul li{
float: left;
width: 83px;
height: 42px;
line-height: 42px;
/*background-color: #fff;*/
/*margin-left: 8px;*/
text-align: center;
}
.nav a{
font-size: 14px;
color: #000;
}
</style>
<script type="text/javascript">
window.onload = function () {
var nav = document.getElementById("nav");
var cloud = nav.children[0];
var lis = nav.children[1].children;
var leader = 0,target = 0;
var current = 0;
for(var i=0 ; i<lis.length ; i++){
var li = lis[i];
li.index = i;
li.onmouseover = function () {
//老做法,但是當li的寬度改變就出問題了,所以使用offSetLeft
// target = this.index*83;
target = this.offsetLeft;
}
li.onmouseout = function () {
target = current;
}
li.onclick = function () {
current = this.offsetLeft;
}
}
function AnimationPlay(){
leader = leader + (target - leader)/20;
cloud.style.left = leader+"px";
}
setInterval(AnimationPlay,10);
}
</script>
</head>
<body>
<div class="nav" id="nav">
<div class="cloud">
</div>
<ul>
<li><a href="#">首頁新聞</a></li>
<li><a href="#">師資力量</a></li>
<li><a href="#">活動策劃</a></li>
<li><a href="#">企業文化</a></li>
<li><a href="#">招聘信息</a></li>
<li><a href="#">公司簡介</a></li>
<li><a href="#">上海校區</a></li>
</ul>
</div>
</body>
</html
offsetParent
返回改對象的父級 (帶有定位)
- 如果當前元素的父級元素沒有進行CSS定位 (
position為absolute或relative,fixed
),offsetParent
為body
。 - 如果當前元素的父級元素中有CSS定位(
position為absolute或relative,fixed
),offsetParent
取最近的那個父級元素。 -
與parentNode的區別:
parentNode
是返回直接父親元素,offsetParent
是返回有定位的父級元素
offsetTop、style.top 的區別
一、最大區別在于 offsetTop 可以返回沒有定位的盒子距離頂部的位置。 而 style.top
不可以
只有定位的盒子,才有 style.left
、style.top
、style.right
、style.bottom
二、offsetTop
返回的是數字,而 style.top
返回的是字符串,除了數字外還帶有單位:px
。
三、offsetTop
只讀,而 style.top
可讀寫。
四、如果沒有給 HTML 元素指定過 top 樣式,則style.top
返回的是空字符串。style.top
只能返回行內樣式設置的top距離
五、最重要的區別, style.top
只能返回行內樣式設置的top距離,offsetLeft
隨便
事件對象
我們學過一些事件 : onmouseover onmouseout onclick .....
、btn.onclick = function(event) { 語句 }
event 單詞翻譯過來 事件 的意思 。event
就是事件的對象,指向的是事件是 onclick
在觸發DOM上的某個事件時,會產生一個事件對象event
,這個對象中包含著所有與事件有關的信息。所有瀏覽器都支持event
對象,但支持的方式不同。比如鼠標操作時候,會添加鼠標位置的相關信息到事件對象中。
普通瀏覽器支持 event
, IE 6、7、8 支持 window.event
,所以我們 采取兼容性的寫法 :
var event = event || window.event;
event 常見屬性
|屬性| 作用|
|::|::|
|data| 返回拖拽對象的URL字符串(dragDrop)|
|width| 該窗口或框架的高度|
|height| 該窗口或框架的高度|
|pageX| 光標相對于該網頁的水平位置(IE6、7、8無)|
|pageY| 光標相對于該網頁的垂直位置(IE6、7、8無)|
|screenX| 光標相對于該屏幕的水平位置|
|screenY| 光標相對于該屏幕的垂直位置|
|target| 該事件被傳送到的對象|
|type| 事件的類型|
|clientX| 光標相對于該網頁的水平位置 (當前可見區域)|
|clientY| 光標相對于該網頁的水平位置|
pageX 、clientX 、screenX 區別
-
screenX
是以我們的電腦屏幕 為基準點 測量 -
pageX
是以我們的文檔(類似絕對定位) 的基準點 對齊,IE6、7、8 不認識 -
clientX
以可視區域為基準點 ,類似于固定定位 - 當出現垂直滾動條,滾動條不在最上邊時,
pageY
和clientY
值是不一樣的
例:點擊跟隨鼠標
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
#image{
width: 80px;
position: absolute;
left: 0;
top: 0;
}
</style>
<script type="text/javascript">
window.onload = function () {
var img = document.getElementById("image");
var leaderX=0,leaderY=0,targetX=0,targetY=0;
document.onclick = function (event) {
var event = event||window.event;
targetX=event.clientX-img.offsetWidth/2;
targetY=event.clientY-img.offsetHeight/2;
}
setInterval(function () {
leaderX = leaderX+(targetX-leaderX)/10;
leaderY = leaderY+(targetY-leaderY)/10;
img.style.left = leaderX+"px";
img.style.top = leaderY+"px";
},20);
}
</script>
</head>
<body>

</body>
</html>
盒子內的坐標
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
div{
width: 500px;
height: 500px;
background-color: pink;
margin-left: 100px;
margin-top: 56px;
}
</style>
<script type="text/javascript">
window.onload = function () {
var div = document.getElementsByTagName("div")[0];
div.onmousemove = function (event) {
var event = event||window.event;
var x = event.clientX - this.offsetLeft;
var y = event.clientY - this.offsetTop;
this.innerHTML = x+"px"+"<br>"+y+"px";
// console.dir(event.screenY);
}
}
</script>
</head>
<body>
<div class="box">
</div>
</body>
</html>
例:淘寶商品圖片放大的效果
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>taobao放大鏡</title>
<style type="text/css">
.box{
position: relative;
width: 350px;
height: 350px;
/*padding: 10px;*/
border: 1px solid #ccc;
/*margin: 100px auto;*/
}
img{
display: block;
}
.big{
position: absolute;
width: 450px;
height: 450px;
top: 0;
left: 355px;
border: 1px solid #ccc;
overflow: hidden;
display: none;
}
.mask{
position: absolute;
width: 188px;
height: 188px;
/*background-color: rgba(255,255,255,.2);*/
left: 0;
top: 0;
background: url("images/mask.png");
display: none;
cursor: move;
}
.big img{
position: absolute;
left: 0;
top: 0;
}
</style>
<script type="text/javascript">
window.onload = function () {
var tb = document.getElementById("taobao");
var small = tb.children[0];
var big = tb.children[1];
var mask = small.children[1];
var bigImg = big.children[0];
small.onmouseover = function () {
big.style.display = "block";
mask.style.display = "block";
}
small.onmouseout = function () {
big.style.display = "none";
mask.style.display = "none";
}
small.onmousemove = function (event) {
var event = event||window.event;
var x = event.clientX - this.offsetParent.offsetLeft - mask.offsetWidth/2;
var y = event.clientY - this.offsetParent.offsetTop - mask.offsetHeight/2;
//邊界檢測
x = x<=0?0:x;
y = y<=0?0:y;
var maxWidth = small.offsetWidth-mask.offsetWidth;
var maxHeight = small.offsetHeight-mask.offsetHeight;//2是2個px的邊框
x = x>=maxWidth?maxWidth:x;
y = y>=maxHeight?maxHeight:y;
mask.style.left = x+"px";
mask.style.top = y+"px";
//計算大盒子與小盒子的比率scale
//在小盒子中每移動1px,那么在大盒子中移動scale*1px;
//大圖的寬高為800
var scale = 800/small.offsetWidth;
bigImg.style.left = -x*scale+"px";
bigImg.style.top = -y*scale+"px";
}
}
</script>
</head>
<body>
<div class="box" id="taobao">
<div class="small">

<div class="mask"></div>
</div>
<div class="big">

</div>
</div>
</body>
</html>
常用事件
onmouseover onmouseout onclick
-
onmousemove
當鼠標移動的時候就是說,鼠標移動一像素就會執行的事件
div.onmousemove = function() { 語句 }
當鼠標再div 身上移動的時候,就會執行。
div.onmouseover
和div.onmousemove
區別
他們相同點都是經過 div 才會觸發
div.onmouseover
只觸發一次
div.onmousemove
每移動一像素,就會觸發一次onmouseup
當鼠標彈起onmousedown
當鼠標按下
拖動
- 拖動原理 == 鼠標按下 接著 移動鼠標 。
bar.onmousedown = function(){
document.onmousemove = function(){
}
}
- 當我們按下鼠標的時候,就要記錄當前 鼠標 的位置 - 大盒子的位置
算出 bar 當前 在 大盒子內的距離 。
例:可拖拽的進度條
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.scroll{
width: 400px;
height: 8px;
background-color: #ccc;
margin: 100px auto;
position: relative;
}
.bar{
width: 10px;
height: 20px;
background-color: #369;
position: absolute;
left: 0;
top: -6px;
cursor: pointer;
}
.mask{
width: 10px;
height: 8px;
background-color: #369;
}
</style>
<script type="text/javascript">
window.onload = function () {
var scroll = document.getElementById("scroll");
var bar = scroll.children[0];
var mask = scroll.children[1];
var msg = document.getElementById("msg");
var that = null;
bar.onmousedown = function (event) {
var event = event||window.event;
that = this;
var leftVal = event.clientX-this.offsetLeft;
document.onmousemove = function (event) {
var event = event||window.event;
var lVal = event.clientX - leftVal;
if(lVal<0){
lVal = 0;
}else if(lVal>=scroll.offsetWidth-10){
lVal = scroll.offsetWidth-10;
}
that.style.left = lVal+"px";
mask.style.width = lVal+"px";
msg.innerHTML = "當前進度:"+parseInt(lVal/(scroll.offsetWidth-10)*100)+"%";
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
}
}
document.onmouseup = function () {
document.onmousemove = null;
}
}
</script>
</head>
<body>
<div class="scroll" id="scroll">
<div class="bar"></div>
<div class="mask"></div>
</div>
<span id="msg"></span>
</body>
</html>
防止選擇拖動
我們知道 按下鼠標然后拖拽可以選擇文字的。
清除選中的內容 ,
正常瀏覽器:window.getSelection().removeAllRanges()
;
IE6、7、8:document.selection.empty();
兼容所有瀏覽器的寫法:
window.getSelection ? window.getSelection().removeAllRanges() : document.selection.empty();
例:可拖拽的彈出框
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding:0;
height: 42px;
}
a{
text-decoration: none;
}
.nav{
background-color: #036663;
}
.nav a{
margin-left: 24px;
line-height: 42px;
color: #fff;
}
.d-box{
width: 400px;
height: 300px;
border: 5px solid #eeeeee;
box-shadow: 2px 2px 2px 2px #999;
position: absolute;
left: 50%;
top: 50%;
margin-left: -200px;
margin-top: -150px;
display: none;
}
.b-title{
width: 100%;
height: 25px;
background-color: #7c9299;
color: #fff;
line-height: 25px;
cursor: move;
}
.b-title a{
float: right;
color: #ffffff;
}
</style>
<script type="text/javascript">
window.onload = function () {
function $(id){return document.getElementById(id);}
var con_box = $("con-alert");
var close = con_box.getElementsByTagName("a")[0];
$("m-alert").onclick = function () {
con_box.style.display = "block";
}
close.onclick = function () {
con_box.style.display = "none";
}
function startDrag(current,move){
current.onmousedown = function (event) {
var event = event||window.event;
var x = event.clientX - move.offsetLeft - move.offsetWidth/2;
var y = event.clientY - move.offsetTop - move.offsetHeight/2;
document.onmousemove = function (event) {
var event = event||window.event;
var left = event.clientX - x;
var top = event.clientY - y;
move.style.left = left+"px";
move.style.top = top+"px";
window.getSelection?window.getSelection().removeAllRanges():document.selection.empty();
}
}
document.onmouseup = function () {
document.onmousemove = null;
}
}
startDrag(con_box.children[0],con_box);
}
</script>
</head>
<body>
<div class="nav">
<a href="javascript:;" id="m-alert">注冊信息</a>
</div>
<div class="d-box" id="con-alert">
<div class="b-title">注冊信息 (可以拖拽)
<a href="javascript:;">【關閉】</a>
</div>
<div class="d-con">
</div>
</div>
</body>
</html>
例:歌詞控件,自定義滾動條
滾動條的高度: 容器的高度 / 內容的高度 * 容器的高度
滾動的比率:(內容盒子高度 - 大盒子高度) / (大盒子高度 - 滾動條的高度)
內容的盒子定位top值:(內容盒子高度 - 大盒子高度) / (大盒子高度 - 滾動條的高度) * 滾動條移動的數值
完整代碼:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
.box{
width: 250px;
height: 500px;
margin: 100px auto;
background-color: #eee;
position: relative;
overflow: hidden;
}
.content{
padding: 5px 20px 5px 5px;
/*background-color: pink;*/
text-align: center;
position: absolute;
left: 0;
top: 0;
}
.scroll{
width: 20px;
height: 100%;
position: absolute;
background-color: #ccc;
top: 0;
right: 0;
}
.bar{
width: 100%;
height: 10%;
background-color: #999;
border-radius: 10px;
position: absolute;
left: 0;
top: 0;
}
</style>
<script type="text/javascript">
window.onload = function () {
// 計算滾動條的長度
// 內容越多,滾動條越短
function $(id){return document.getElementById(id);}
var box = $("box");
var content = box.children[0];
var scroll = $("scroll");
var bar = scroll.children[0];
var h = box.offsetHeight*box.offsetHeight/content.offsetHeight;
bar.style.height = h+"px";
bar.onmousedown = function (event) {
var event = event||window.event;
var t = event.clientY - this.offsetTop;
document.onmousemove = function (event) {
var event = event||window.event;
var y = event.clientY - t;
//上下邊界檢測
y = y<0?0:y;
var maxH = scroll.offsetHeight - bar.offsetHeight;
y = y>maxH?maxH:y;
bar.style.top = y+"px";
window.getSelection?window.getSelection().removeAllRanges():document.selection.empty();
//內容的位移比率
var scale = (content.offsetHeight - box.offsetHeight)/maxH;
var content_top = scale*y;
content.style.top = -content_top+"px";
}
}
document.onmouseup = function () {
document.onmousemove = null;
}
}
</script>
</head>
<body>
<div class="box" id="box">
<div class="content" id="content">
看透愛情看透你 (粵語版)<br>
作曲:陳煒<br>
填詞:楊語蓮<br>
演唱:楊語蓮<br>
歌詞千尋:www.lrcgc.com<br>
從未奢望真心可永久<br>
只得放任無言退后<br>
誰說分手仍然是朋友<br>
迷茫夢境苦痛你知否<br>
可嘆心死傾出我所有<br>
強裝鎮定默默眼淚流<br>
流水記憶早已經遠走<br>
斷腸消瘦問這生何求<br>
無情的世界無情的走<br>
無聊愛情早已經看透<br>
尋尋覓覓又添許多愁<br>
懷著千般失落茫然中跌坐<br>
無情的世界無情的酒<br>
飲下這杯愛情無言的詛咒<br>
忘掉了你忘記曾一起<br>
看透愛情也看透了你<br>
可嘆心死傾出我所有<br>
強裝鎮定默默眼淚流<br>
流水記憶早已經遠走<br>
斷腸消瘦問這生何求<br>
無情的世界無情的走<br>
無聊愛情早已經看透<br>
尋尋覓覓又添許多愁<br>
懷著千般失落茫然中跌坐<br>
無情的世界無情的酒<br>
飲下這杯愛情無言的詛咒<br>
忘掉了你忘記曾一起<br>
看透愛情也看透了你<br>
無情的世界無情的走<br>
無聊愛情早已經看透<br>
尋尋覓覓又添許多愁<br>
懷著千般失落茫然中跌坐<br>
無情的世界無情的酒<br>
飲下這杯愛情無言的詛咒<br>
忘掉了你忘記曾一起<br>
看透愛情也看透了你<br>
<br><br>
奔跑吧兄弟<br>
作詞:譚春梅/張佳彤<br>
作曲:張佳彤<br>
演唱:譚春梅<br>
監制混縮:李程<br>
發行:天歌數字音樂傳媒<br>
當我孤獨寂寞的時候<br>
是你讓我快樂無憂<br>
當我徘徊無助的時候<br>
是你伸出溫暖的雙手<br>
當我病痛降臨的時侯<br>
是你日夜在為我操勞<br>
當我痛苦難過的時候<br>
是你為我種下幸福之花<br>
奔跑奔跑吧兄弟<br>
我會一直在你的左右<br>
奔跑奔跑吧兄弟<br>
握緊你的拳頭一直往前走<br>
奔跑奔跑吧兄弟<br>
我們有福同享有難一起扛<br>
奔跑奔跑吧兄弟<br>
你永遠是我們心中的驕傲<br>
奔跑奔跑吧兄弟<br>
幸福之花將開滿你我心頭<br>
奔跑奔跑吧兄弟<br>
為了我們同一個夢想而努力奮斗<br>
當我孤獨寂寞的時候<br>
是你讓我快樂無憂<br>
當我徘徊無助的時候<br>
是你伸出溫暖的雙手<br>
當我病痛降臨的時候<br>
是你日夜在為我操勞<br>
當我痛苦難過的時候<br>
是你為我種下幸福之花<br>
奔跑奔跑吧兄弟<br>
我會一直在你的左右<br>
奔跑奔跑吧兄弟<br>
握緊你的拳頭一直往前走<br>
奔跑奔跑吧兄弟<br>
我們有福同享有難一起扛<br>
奔跑奔跑吧兄弟<br>
你永遠是我們心中的驕傲<br>
奔跑奔跑吧兄弟<br>
幸福之花將開滿你我心頭<br>
奔跑奔跑吧兄弟<br>
為了我們同一個夢想而努力奮斗<br>
奔跑奔跑吧兄弟<br>
我會一直在你的左右<br>
奔跑奔跑吧兄弟<br>
握緊你的拳頭一直往前走<br>
奔跑奔跑吧兄弟<br>
我們有福同享有難一起扛<br>
奔跑奔跑吧兄弟<br>
你永遠是我們心中的驕傲<br>
奔跑奔跑吧兄弟<br>
幸福之花將開滿你我心頭<br>
奔跑奔跑吧兄弟<br>
為了我們同一個夢想而努力奮斗<br>
為了我們同一個夢想而努力奮斗
</div>
<div class="scroll" id="scroll">
<div class="bar"></div>
</div>
</div>
</body>
</html>