中软Day08
-
盒子塌陷的问题
问题父盒子和子盒子有时外边合并,此时用 overflow:hidden
-
特效
- 偏移量
-
客户区大小
-
滚动偏移
-
案例
-
鼠标跟随点击
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> img{ width: 100px; position: absolute; top: 0; left: 0; } </style> </head> <body> <img src="img/img.jpg" id="image"> </body> <script type="text/javascript"> var image = document.getElementById("image"); document.onclick=function(event){ var event = event || window.event; targetX = event.clientX-image.offsetWidth/2; targetY = event.clientY-image.offsetHeight/2; } //缓动 var leaderX = 0,leaderY = 0,targetX = 0,targetY = 0; setInterval(function(){ leaderX=leaderX+(targetX-leaderX)/10; leaderY=leaderY+(targetY-leaderY)/10; image.style.left=leaderX+"px"; image.style.top=leaderY+"px"; },10) </script> </html>
-
跟随鼠标导航栏
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <style type="text/css"> *{margin: 0;padding: 0;} body{background-color: #333;} ul{ list-style: none; } .nav{ width: 1200px; height: 42px; margin: 100px auto; border-radius: 5px; background: url(img/rss.png) no-repeat right center #fff; position: relative; } .cloud{ width: 83px; height: 42px; background: url(img/cloud.gif) no-repeat; position: absolute; left: 0; top: 0; } .nav li{ width: 83px; height: 42px; float: left; line-height: 42px; text-align: center; color: black; cursor: pointer; } .nav ul{ position: absolute; left: 0; top: 0; } </style> </head> <body> <div class="nav" id="nav"> <span class="cloud" id="cloud"> </span> <ul> <li>师资培训</li> <li>首页新闻</li> <li>活动策划</li> <li>企业文化</li> <li>招聘信息</li> <li>公司简介</li> <li>联系我们</li> <li>关于我们</li> </ul> </div> </body> </html> <script type="text/javascript"> var cloud = document.getElementById("cloud"); var nav = document.getElementById("nav"); var lis = nav.children[1].children; var current = 0; for(var i = 0;i<lis.length;i++){ lis[i].onmouseover=function(){ // alert(this.offsetLeft) target=this.offsetLeft; } lis[i].onmouseout=function(){ target=current; } lis[i].onclick=function(){ current=this.offsetLeft } } var leader = 0,target = 0; setInterval(function(){ leader = leader+(target-leader)/10; cloud.style.left=leader+"px"; },10) </script>
-
客户区
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> body { margin: 0; } #box { /*position: relative;*/ width: 300px; height: 300px; background-color: red; overflow: hidden; margin: 50px; } #child { width: 100px; height: 100px; background-color: blue; margin: 50px; border: 10px solid yellow; padding: 10px; } </style> </head> <body> <div id="box"> <div id="child"> </div> </div> <script type="text/javascript"> var box = document.getElementById('box'); var child = document.getElementById('child'); /* console.log(box.offsetParent); //offsetParent,有定位的父元素 console.log(box.offsetLeft); //左外边距 console.log(box.offsetLeft); //上外边距 console.log(box.offsetWidth); //内容宽度 console.log(box.offsetHeight); //offset属性,回去盒子大小和盒子距离父级元素*/ console.log(child.offsetParent); //offsetParent,有定位的父元素 console.log(child.offsetHeight); </script> </body> </html>
-
拖拽案例
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> * { margin: 0; padding: 0; } .nav { height: 30px; background: #036663; border-bottom: 1px solid #369; line-height: 30px; padding-left: 30px; } .nav a { color: #fff; text-align: center; font-size: 14px; text-decoration: none; } .d-box { width: 400px; height: 300px; border: 5px solid #eee; box-shadow: 2px 2px 2px 2px #666; position: absolute; top: 40%; left: 40%; background-color: white; } .hd { width: 100%; height: 25px; background-color: #7c9299; border-bottom: 1px solid #369; line-height: 25px; color: white; cursor: move; } #box_close { float: right; cursor: pointer; } </style> </head> <body> <div class="nav"> <a href="javascript:void(0)" id="register">注册信息</a> </div> <div class="d-box" id="d_box"> <div class="hd" id="drop">注册信息 (可以拖拽) <span id="box_close">【关闭】</span> </div> <div class="bd"></div> </div> <script type="text/javascript"> var box = document.getElementById('d_box'); var drop = document.getElementById('drop'); drop.onmousedown = function(e) { //1.获取鼠标在盒子中的距离 //1.1 兼容性 e = e || window.evnet; //1.2鼠标在盒子中的位置 = 鼠标在页面上的位置 - 盒子位置 var x = e.pageX - box.offsetLeft; var y = e.pageY - box.offsetTop; //2.当鼠标在页面移动时,求盒子的坐标 //2.1鼠标在文档中移动 document.onmousemove = function(e) { //盒子的坐标 = 鼠标在页面中的位置 - 鼠标在盒子的位置 var boxX = e.pageX - x; var boxY = e.pageY - y; console.log(boxX + ' ' + boxY); box.style.left = boxX + 'px'; box.style.top = boxY + 'px'; } } //鼠弹起的时候,移除鼠标移动事件 document.onmouseup = function() { document.onmousemove = null; } //点击关闭时,隐藏盒子 var btn = document.getElementById('box_close'); btn.onclick = function() { box.style.display = 'none'; } </script> </body> </html>
-
弹出登录窗口
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <style> .login-header { width: 100%; text-align: center; height: 30px; font-size: 24px; line-height: 30px; } ul, li, ol, dl, dt, dd, div, p, span, h1, h2, h3, h4, h5, h6, a { padding: 0px; margin: 0px; } .login { width: 512px; position: absolute; border: #ebebeb solid 1px; height: 280px; left: 50%; right: 50%; background: #ffffff; box-shadow: 0px 0px 20px #ddd; z-index: 9999; margin-left: -256px; margin-top: 140px; display: none; } .login-title { width: 100%; margin: 10px 0px 0px 0px; text-align: center; line-height: 40px; height: 40px; font-size: 18px; position: relative; cursor: move; -moz-user-select:none;/*火狐*/ -webkit-user-select:none;/*webkit浏览器*/ -ms-user-select:none;/*IE10*/ -khtml-user-select:none;/*早期浏览器*/ user-select:none; } .login-input-content { margin-top: 20px; } .login-button { width: 50%; margin: 30px auto 0px auto; line-height: 40px; font-size: 14px; border: #ebebeb 1px solid; text-align: center; } .login-bg { width: 100%; height: 100%; position: fixed; top: 0px; left: 0px; background: #000000; opacity: 0.3; display: none; } a { text-decoration: none; color: #000000; } .login-button a { display: block; } .login-input input.list-input { float: left; line-height: 35px; height: 35px; width: 350px; border: #ebebeb 1px solid; text-indent: 5px; } .login-input { overflow: hidden; margin: 0px 0px 20px 0px; } .login-input label { float: left; width: 90px; padding-right: 10px; text-align: right; line-height: 35px; height: 35px; font-size: 14px; } .login-title span { position: absolute; font-size: 12px; right: -20px; top: -30px; background: #ffffff; border: #ebebeb solid 1px; width: 40px; height: 40px; border-radius: 20px; } </style> </head> <body> <div class="login-header"><a id="link" href="#">点击,弹出登录框</a></div> <div id="login" class="login" > <div id="title" class="login-title">登录会员 <span><a id="closeBtn" href="javascript:void(0);" class="close-login">关闭</a></span> </div> <div class="login-input-content"> <div class="login-input"> <label>用户名:</label> <input type="text" placeholder="请输入用户名" name="info[username]" id="username" class="list-input"> </div> <div class="login-input"> <label>登录密码:</label> <input type="password" placeholder="请输入登录密码" name="info[password]" id="password" class="list-input"> </div> </div> <div id="loginBtn" class="login-button"><a href="javascript:void(0);" id="login-button-submit">登录会员</a></div> </div> <!-- 遮盖层 --> <div id="bg" class="login-bg" ></div> <script> //1.显示登录框和遮盖层 var login = document.getElementById('login'); var bg = document.getElementById('bg'); //1.1点击按钮,弹出登录框和遮盖曾 var link = document.getElementById('link'); link.onclick = function() { login.style.display = 'block'; bg.style.display = 'block'; return false; } //2.点击关闭按钮,隐藏登录框和遮盖层 var close_btn = document.getElementById('closeBtn'); close_btn.onclick = function() { login.style.display = 'none'; bg.style.display = 'none'; return false; } //3.拖拽 var title = document.getElementById('title'); title.onmousedown = function (e) { //当鼠标按下的时候,求鼠标在盒子中的位置 var x = e.pageX - login.offsetLeft; var y = e.pageY - login.offsetTop; //鼠标移动时,求盒子的坐标 document.onmousemove = function(e) { var loginX = e.pageX - x; var loginY = e.pageY - y; login.style.left = loginX + 256 + "px"; login.style.top = loginY -140 + "px"; } document.onmouseup = function() { //移除鼠标事件 document.onmousemove = null; } } </script> </body> </html>
-
滚动条
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <style> body { margin: 0; } #box { width: 100px; height: 100px; margin: 50px; border: 30px solid red; padding: 10px; background-color: green; overflow: auto; } </style> </head> <body> <div id="box"> 今天:@电影小Q官博 发文宣布该片票房正式破亿。据悉,该片由任达华 </div> <script type="text/javascript"> var box = document.getElementById('box'); //横向滚动条的距离 console.log(box.scrollLeft); //纵向滚动条的距离 console.log(box.scrollTop); //内容距离减去滚动条距离 console.log(box.scrollWidth); //内容的总高度 console.log(box.scrollHeight); //scroll,获取滚动内容大小和位置 </script> </body> </html>
-
微博发布
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{ margin: 0; padding: 0; } ul{ list-style: none; } .box{ margin: 100px auto; width: 600px; height: auto; border: 1px solid #333; padding: 30px 0; } textarea{ width: 450px; resize: none;/*防止用户拖动*/ } .box li{ width: 450px; line-height: 30px; border-bottom: 1px dashed #ccc; margin-left: 80px; } .box li a{ float: right; } </style> <script> window.onload=function () { //活获取对象、操作对象; var btn=document.getElementsByTagName("button")[0]; var txt=document.getElementsByTagName("textarea")[0]; var ul = document.createElement("ul");//创建ul; btn.parentNode.appendChild(ul); btn.onclick=function () { if(txt.value=="") { alert("输入不能为空"); return false;//终止函数执行; } var newli = document.createElement("li"); newli.innerHTML=txt.value+"<a href='javascript:;'>删除</a>";//把表单的值给li; ul.appendChild(newli); txt.value="";//清空表单; var lis = ul.children;//获得有多少个li; //下面的代码是让我们新的内容靠上显示; if(lis.length==0){//第一次创建 ul.appendChild(newli);//ul的后面追加 } else { ul.insertBefore(newli,lis[0]);//每次生成的新的li放到第一个的前面; } var as = document.getElementsByTagName("a");//获取所有的a for(var i=0;i<as.length;i++){ as[i].onclick=function () { ul.removeChild(this.parentNode);//ul 它的爸爸; //a的爸爸是li; } } } } </script> </head> <body> <div class="box"> 微博发布:<textarea name="" id="" cols="30" rows="10"></textarea> <button>发布</button> <!--<ul id="ul">--> <!--<li></li>--> <!--<li></li>--> <!--<li></li>--> <!--</ul>--> </div> </body> </html>
-
短信验证
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>发送短信验证</title> </head> <body> <input type="text"> <button id="btn">点击发送短信</button> <script> var btn = document.getElementById("btn"); var count = 10; var timer = null;// 给定时器起名字 btn.onclick=function () { this.disabled=true;// 取消按钮的点击状态; var that = this; timer = setInterval(sendTextMessage,1000); function sendTextMessage() { count--; if (count>=0){ that.innerHTML="还需要"+count+"秒" } else { that.innerHTML="重新发送"; that.disabled=false; clearInterval(timer);// 关闭定时器 count=5; } } } </script> </body> </html>
46. 附录
元素的类型