你们好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新......javascript
- github:https://github.com/Daotin/Web
- 微信公众号:Web前端之巅
- 博客园:http://www.cnblogs.com/lvonve/
- CSDN:https://blog.csdn.net/lvonve/
在这里我会从 Web 前端零基础开始,一步步学习 Web 相关的知识点,期间也会分享一些好玩的项目。如今就让咱们一块儿进入 Web 前端学习的冒险之旅吧!html
// 获取 body document.body; // 获取 title document.title; // 获取的是 title 中的值 // 获取 html document.documentElement;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> img { position: absolute; } </style> </head> <body> <img src="images/Daotin.png" id="im"> <script src="common.js"></script> <script> document.onmousemove = function (ev) { // 获取鼠标的横纵坐标 my$("im").style.left = ev.clientX + "px"; my$("im").style.top = ev.clientY + "px"; } </script> </body> </html>
一、获取鼠标的横纵坐标在鼠标移动的事件中;前端
二、注意:图片可以移动,必定要脱标。java
offsetWidth:获取元素的宽(加边框) offsetHeight:获取元素的高(加边框) offsetLeft:获取元素距离左边位置的值 offsetTop:获取元素距离上边位置的值
scroll:卷曲git
scrollWidth:若是元素中内容宽度小于元素的宽度,则为元素的宽度(不含边框),不然为元素中内容的实际宽度。 scrollHeight:若是元素中内容高度小于元素的高度,则为元素的高度(不含边框),不然为元素中内容的实际高度。 scrollLeft:元素中的内容往左卷曲出去的距离。(有滚动条的时候) scrollTop:元素中的内容往上卷曲出去的距离。(有滚动条的时候)
function getScroll() { return { left: window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft, top: window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop }; }
一、返回的是一个对象,这个对象有两个自定义属性 left 和 top ,使用的时候直接使用 getScroll().left 或者 getScroll().top 便可得到浏览器滚动条向左向上移动的距离。github
二、之因此用 “||” 操做是由于浏览器兼容问题。chrome
// 变速动画移动函数 function animation(element, target) { clearInterval(element.timeId); // 每次调用函数就清理以前的timeId // 判断当前的位置 element.timeId = setInterval(function () { var current = element.offsetLeft; // 不能使用 element.style.left var onestep = (target - current) / 10; onestep = onestep > 0 ? Math.ceil(onestep) : Math.floor(onestep); current += onestep; element.style.left = current + "px"; // if (Math.abs(current - target) >= onestep) { // element.style.left = current + "px"; // } else { // clearInterval(timeId); // element.style.left = target + "px"; // } if(target === current) { clearInterval(element.timeId); return; } // 测试代码 console.log("target="+target+", current="+current+", step="+onestep); }, 20); }
一、Math.ceil() 和 Math.round() 和 Math.floor() 的区别:json
zconsole.log(Math.ceil(11.1)); // 12 console.log(Math.ceil(11.8)); // 12 console.log(Math.ceil(-11.1)); // -11 console.log(Math.ceil(-11.8)); // -11 console.log(Math.round(11.1)); // 11 console.log(Math.round(11.8)); // 12 console.log(Math.round(-11.1)); // -11 console.log(Math.round(-11.8));// -12 console.log(Math.floor(11.1)); // 11 console.log(Math.floor(11.8)); // 11 console.log(Math.floor(-11.1)); // -12 console.log(Math.floor(-11.8)); // -12
二、这里 onestep 使用向上取整,才能走到终点。浏览器
三、这里就不须要判断 if (Math.abs(current - target) >= onestep) 了,由于每次走的 onestep 老是愈来愈小,到最后都会变成1,因此不存在走不够或者超出的状况。微信
四、定时器中加个 return,能够防止走到终点,函数还在不停循环的状况。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> * { margin: 0; padding: 0; } #box { width: 800px; height: 50px; margin: 0 auto; margin-top: 200px; background-color: pink; position: relative; } li { float: left; list-style-type: none; width: 100px; height: 30px; background-color: #fff; margin: 10px; cursor: pointer; text-align: center; font: 700 20px/30px "Microsoft YaHei"; } span { position: absolute; width: 100px; height: 30px; background-color: rgba(181, 14, 205, 0.8); left: 10px; top: 10px; } </style> </head> <body> <input type="button" value="按钮" id="btn"> <div id="box"> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> </ul> <span></span> </div> <script src="common.js"></script> <script> // 获取box元素 var boxObj = my$("box"); // 获取span元素 var spanObj = boxObj.children[1]; // 获取全部li元素 var liObjs = boxObj.children[0].children; // 为全部的li注册事件 for (var i = 0; i < liObjs.length; i++) { // 注册鼠标进入 liObjs[i].onmouseover = mouseoverHandle; // 注册鼠标点击 liObjs[i].onclick = clickHandle; // 注册鼠标出来 liObjs[i].onmouseout = mouseoutHandle; } function mouseoverHandle() { animationChangeSpeed(spanObj, this.offsetLeft); } var currentPos = 10; function clickHandle() { currentPos = this.offsetLeft; } function mouseoutHandle() { animationChangeSpeed(spanObj, currentPos); } </script> </body> </html>
一、var currentPos = 10; 是由于个人 span 有个 maigin-left:10px,若是是从最左边开始的话就为 0。
在 window 下有一个方法:window.getComputedStyle(element, string)
能够获取一个元素全部的属性值。
其中第一个参数为须要获取的元素;第二个参数为是否有伪类或者伪样式。返回值是这个元素全部属性的对象集合。
当咱们须要什么属性的时候,点出来就能够了。
可是这个方法 IE8 不支持,在 IE8 下有一个属性 currentStyle, 经过 元素.currentStyle
的方式能够获得返回值为这个元素全部属性的集合。
兼容代码:
function getStyle(element, attr) { return window.getComputedStyle ? window.getComputedStyle(element, null)[attr] : element.currentStyle[attr]; }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> * { margin: 0; padding: 0; } input { margin-top: 10px; } div { position: absolute; width: 200px; height: 100px; background-color: yellowgreen; margin-top: 10px; /*left: 20px;*/ } </style> </head> <body> <input type="button" value="移动400px" id="btn1"> <input type="button" value="移动800px" id="btn2"> <div id="dv"></div> <script src="common.js"></script> <script> // 移动400px my$("btn1").onclick = function () { animation(my$("dv"), "top", 400); }; // 移动800px my$("btn2").onclick = function () { animation(my$("dv"), "width", 800); }; var timeId = 0; // 变速动画移动函数 // element --- 任意元素 // attr ---- 任意属性名字 // target ---目标位置 function animation(element, attr, target) { clearInterval(element.timeId); // 每次调用函数就清理以前的timeId // 判断当前的位置 element.timeId = setInterval(function () { var current = parseInt(getStyle(element, attr)); // 获取任意元素的任意一个属性值 var onestep = (target - current) / 10; onestep = onestep > 0 ? Math.ceil(onestep) : Math.floor(onestep); current += onestep; element.style[attr] = current + "px"; if(target === current) { clearInterval(element.timeId); } // 测试代码 console.log("target="+target+", current="+current+", step="+onestep); }, 20); } </script> </body> </html>
getStyle 函数返回的属性值是加“px”的因此要加
parseInt
进行处理。
<body> <input type="button" value="移动" id="btn"> <div id="dv"></div> <script src="common.js"></script> <script> my$("btn").onclick = function () { animation(my$("dv"), {"left":100,"top":400,"width":400,"height":200}); }; var timeId = 0; // 变速动画移动函数 // element --- 任意元素 // attr ---- 任意属性名字 // target ---目标位置 function animation(element, json) { clearInterval(element.timeId); // 每次调用函数就清理以前的timeId // 判断当前的位置 element.timeId = setInterval(function () { var flag = true; for(var attr in json) { var current = parseInt(getStyle(element, attr)); // 获取任意元素的任意一个属性值 var target = json[attr]; var onestep = (target - current) / 10; onestep = onestep > 0 ? Math.ceil(onestep) : Math.floor(onestep); current += onestep; element.style[attr] = current + "px"; // 保证全部属性都达到目标才清理定时器 if(target !== current) { flag = false; } } if (flag) { clearInterval(element.timeId); } // 测试代码 console.log("target="+target+", current="+current+", step="+onestep); }, 20); } </script> </body>
一、既然须要多对属性,很天然的想到 json
二、在移动的时候使用 for in 循环遍历 json
三、由于每一个属性达到目标值的次数不一样,因此须要在全部属性都到达目标值时才清理定时器。
回调函数:当一个函数做为参数的时候,这个函数就是回调函数。
做用:增长动画的次数。
<body> <input type="button" value="移动" id="btn"> <div id="dv"></div> <script src="common.js"></script> <script> my$("btn").onclick = function () { animation(my$("dv"), {"left":100,"top":400,"width":400,"height":200}, function (){ animation(my$("dv"), {"left":300,"top":40,"width":140,"height":20}, function (){ animation(my$("dv"), {"left":50,"top":200,"width":200,"height":100}); }); }); }; var timeId = 0; // 变速动画移动函数 // element --- 任意元素 // attr ---- 任意属性名字 // target ---目标位置 function animation(element, json, fn) { clearInterval(element.timeId); // 每次调用函数就清理以前的timeId // 判断当前的位置 element.timeId = setInterval(function () { var flag = true; for(var attr in json) { var current = parseInt(getStyle(element, attr)); // 获取任意元素的任意一个属性值 var target = json[attr]; var onestep = (target - current) / 10; onestep = onestep > 0 ? Math.ceil(onestep) : Math.floor(onestep); current += onestep; element.style[attr] = current + "px"; // 保证全部属性都达到目标才清理定时器 if(target !== current) { flag = false; } } if (flag) { clearInterval(element.timeId); if(fn) { fn(); } } // 测试代码 //console.log("target="+target+", current="+current+", step="+onestep); }, 20); } // 获取任意元素的任意一个属性值 function getStyle(element, attr) { return window.getComputedStyle ? window.getComputedStyle(element, null)[attr] : element.currentStyle[attr]; } </script> </body>
一、回调函数的调用应该在循环以后,清理定时器以后调用。
二、测试 chrome、firefox 均可以, IE8 出错,显示 element.style[attr] = current + "px"; 有问题,暂时不知道什么缘由。
透明度:opacity
层级:z-Index
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> * { margin: 0; padding: 0; } div { position: absolute; width: 200px; height: 100px; background-color: yellowgreen; } input { z-index: 1; position: absolute; } </style> </head> <body> <input type="button" value="移动" id="btn"> <div id="dv"></div> <script src="common.js"></script> <script> my$("btn").onclick = function () { animation(my$("dv"), {"height": 200, "width":15,"opacity":1,"zIndex":10}); }; // 获取任意元素的任意一个属性值 function getStyle(element, attr) { return window.getComputedStyle ? window.getComputedStyle(element, null)[attr] : element.currentStyle[attr]; } // 变速动画移动函数 // element --- 任意元素 // attr ---- 任意属性名字 // target ---目标位置 function animation(element, json, fn) { clearInterval(element.timeId); // 每次调用函数就清理以前的timeId // 判断当前的位置 element.timeId = setInterval(function () { var flag = true; for (var attr in json) { // 判断attr是否是层级zIndex if (attr === "zIndex") { element.style[attr] = json[attr]; } else if (attr === "opacity") { // 判断attr是否是透明度opacity // 获取当前透明度*100,方便计算 var current = getStyle(element, attr) * 100; // 目标透明度也*100 var target = json[attr] * 100; var onestep = (target - current) / 10; onestep = onestep > 0 ? Math.ceil(onestep) : Math.floor(onestep); current += onestep; element.style[attr] = current / 100; } else { // 其余属性 var current = parseInt(getStyle(element, attr)); // 获取任意元素的任意一个属性值 var target = json[attr]; var onestep = (target - current) / 10; onestep = onestep > 0 ? Math.ceil(onestep) : Math.floor(onestep); current += onestep; element.style[attr] = current + "px"; } // 保证全部属性都达到目标才清理定时器 if (target !== current) { flag = false; } } if (flag) { clearInterval(element.timeId); if (fn) { fn(); } } // 测试代码 //console.log("target="+target+", current="+current+", step="+onestep); }, 20); } </script> </body> </html>
一、此为最终版变速动画函数。
二、透明度的设置由于是小数计算,因此须要都乘以100,最后再除以100.
三、层级 zIndex 不须要渐变,直接设置便可。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> * { margin: 0; padding: 0; } div { width: 1200px; height: 500px; margin: 100px 0 0 100px; border: 1px solid red; overflow: hidden; } li { float: left; list-style: none; width: 240px; height: 500px; } </style> </head> <body> <div id="dv"> <ul> <li></li> <li></li> <li></li> <li></li> <li></li> </ul> </div> <script src="common.js"></script> <script> var liObjs = my$("dv").getElementsByTagName("li"); for (var i = 0; i < liObjs.length; i++) { liObjs[i].style.backgroundImage = "url(images/dos.jpg)"; // 鼠标进入 liObjs[i].onmouseover = mouseoverHandle; // 鼠标退出 liObjs[i].onmouseout = mouseoutHandle; } function mouseoverHandle() { // 先设置全部宽度为100 for (var j = 0; j < liObjs.length; j++) { animation(liObjs[j], {"width": 100}); } // 再设置当前元素宽度为800 animation(this, {"width": 800}); } function mouseoutHandle() { for (var j = 0; j < liObjs.length; j++) { animation(liObjs[j], {"width": 240}); } } // 获取任意元素的任意一个属性值 function getStyle(element, attr) { return window.getComputedStyle ? window.getComputedStyle(element, null)[attr] : element.currentStyle[attr]; } // 变速动画移动函数 // element --- 任意元素 // attr ---- 任意属性名字 // target ---目标位置 function animation(element, json, fn) { clearInterval(element.timeId); // 每次调用函数就清理以前的timeId // 判断当前的位置 element.timeId = setInterval(function () { var flag = true; for (var attr in json) { // 判断attr是否是层级zIndex if (attr === "zIndex") { element.style[attr] = json[attr]; } else if (attr === "opacity") { // 判断attr是否是透明度opacity // 获取当前透明度*100,方便计算 var current = getStyle(element, attr) * 100; // 目标透明度也*100 var target = json[attr] * 100; var onestep = (target - current) / 10; onestep = onestep > 0 ? Math.ceil(onestep) : Math.floor(onestep); current += onestep; element.style[attr] = current / 100; } else { // 其余属性 var current = parseInt(getStyle(element, attr)); // 获取任意元素的任意一个属性值 var target = json[attr]; var onestep = (target - current) / 10; onestep = onestep > 0 ? Math.ceil(onestep) : Math.floor(onestep); current += onestep; element.style[attr] = current + "px"; } // 保证全部属性都达到目标才清理定时器 if (target !== current) { flag = false; } } if (flag) { clearInterval(element.timeId); if (fn) { fn(); } } // 测试代码 //console.log("target="+target+", current="+current+", step="+onestep); }, 20); } </script> </body> </html>