小球圆周运动
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><style>.box{position: relative;width: 400px;height: 400px;border: 1px solid red;margin: 200px auto 0px;border-radius: 50%;}.smallbox{position: absolute;width: 10px;height: 10px;border-radius:50%;background-color: black;}</style></head><body><div class="box"><span class="smallbox"></span></div><script>var r=200;var d=document.getElementsByClassName("smallbox");var deg=0;var x,y;function fn(){y=Math.sin(Math.PI/180*deg)*r+r-5;x=Math.cos(Math.PI/180*deg)*r+r-5;d[0].style.cssText="top:"+y+"px;left:"+x+"px;";console.log(x+"====="+y);console.log(deg);++deg;}setInterval(fn,1);</script></body></html>
时间运动框架
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>时间运动框架</title><style>#box{width: 300px;height: 200px;background-color: pink;}</style></head><body><div id="box"></div></body><script>var box=document.getElementById("box");//获取元素animation(box,{data:{width:1100,//属性:属性值height:200,opacity:0.5},options:{speed:"easeInOut",//速度类型type:"Sine" //运动类型}},1000);function animation(ele,data,time,callback){//IE兼容window.requestAnimationFrame=window.requestAnimationFrame||function(method){setTimeout(method,1000/60);};//开始时间var startTime=new Date();//获取css样式以及IE兼容var eCss=ele.currentStyle||getComputedStyle(ele);//要变化的值var diffData={};//原本属性的值var nowData={};//运动方式类型var oType=data["options"]["type"];//运动方式速度var oSpeed=data["options"]["speed"];//Tween.jsvar Tween = {//直线运动Linear:{easeIn:function(t, b, c, d) { return c*t/d + b; }},//二次曲线Quad: {easeIn: function(t, b, c, d) {return c * (t /= d) * t + b;},easeOut: function(t, b, c, d) {return -c *(t /= d)*(t-2) + b;},easeInOut: function(t, b, c, d) {if ((t /= d / 2) < 1) return c / 2 * t * t + b;return -c / 2 * ((--t) * (t-2) - 1) + b;}},//三次曲线Cubic: {easeIn: function(t, b, c, d) {return c * (t /= d) * t * t + b;},easeOut: function(t, b, c, d) {return c * ((t = t/d - 1) * t * t + 1) + b;},easeInOut: function(t, b, c, d) {if ((t /= d / 2) < 1) return c / 2 * t * t*t + b;return c / 2*((t -= 2) * t * t + 2) + b;}},//四次曲线Quart: {easeIn: function(t, b, c, d) {return c * (t /= d) * t * t*t + b;},easeOut: function(t, b, c, d) {return -c * ((t = t/d - 1) * t * t*t - 1) + b;},easeInOut: function(t, b, c, d) {if ((t /= d / 2) < 1) return c / 2 * t * t * t * t + b;return -c / 2 * ((t -= 2) * t * t*t - 2) + b;}},//五Quint: {easeIn: function(t, b, c, d) {return c * (t /= d) * t * t * t * t + b;},easeOut: function(t, b, c, d) {return c * ((t = t/d - 1) * t * t * t * t + 1) + b;},easeInOut: function(t, b, c, d) {if ((t /= d / 2) < 1) return c / 2 * t * t * t * t * t + b;return c / 2*((t -= 2) * t * t * t * t + 2) + b;}},Sine: {easeIn: function(t, b, c, d) {return -c * Math.cos(t/d * (Math.PI/2)) + c + b;},easeOut: function(t, b, c, d) {return c * Math.sin(t/d * (Math.PI/2)) + b;},easeInOut: function(t, b, c, d) {return -c / 2 * (Math.cos(Math.PI * t/d) - 1) + b;}},Expo: {easeIn: function(t, b, c, d) {return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;},easeOut: function(t, b, c, d) {return (t==d) ? b + c : c * (-Math.pow(2, -10 * t/d) + 1) + b;},easeInOut: function(t, b, c, d) {if (t==0) return b;if (t==d) return b+c;if ((t /= d / 2) < 1) return c / 2 * Math.pow(2, 10 * (t - 1)) + b;return c / 2 * (-Math.pow(2, -10 * --t) + 2) + b;}},Circ: {easeIn: function(t, b, c, d) {return -c * (Math.sqrt(1 - (t /= d) * t) - 1) + b;},easeOut: function(t, b, c, d) {return c * Math.sqrt(1 - (t = t/d - 1) * t) + b;},easeInOut: function(t, b, c, d) {if ((t /= d / 2) < 1) return -c / 2 * (Math.sqrt(1 - t * t) - 1) + b;return c / 2 * (Math.sqrt(1 - (t -= 2) * t) + 1) + b;}},Bounce: {easeIn: function(t, b, c, d) {return c - Tween.Bounce.easeOut(d-t, 0, c, d) + b;},easeOut: function(t, b, c, d) {if ((t /= d) < (1 / 2.75)) {return c * (7.5625 * t * t) + b;} else if (t < (2 / 2.75)) {return c * (7.5625 * (t -= (1.5 / 2.75)) * t + .75) + b;} else if (t < (2.5 / 2.75)) {return c * (7.5625 * (t -= (2.25 / 2.75)) * t + .9375) + b;} else {return c * (7.5625 * (t -= (2.625 / 2.75)) * t + .984375) + b;}},easeInOut: function(t, b, c, d) {if (t < d / 2) {return Tween.Bounce.easeIn(t * 2, 0, c, d) * .5 + b;} else {return Tween.Bounce.easeOut(t * 2 - d, 0, c, d) * .5 + c * .5 + b;}}}};//算出每个属性的差值,算出每个属性现在的值分别给到两个对象for(var key in data["data"]){if(eCss[key]!==data["data"][key]){//为了兼容IE的透明属性if(key!=="opacity"){nowData[key]=parseFloat(eCss[key]);}else{nowData[key]=parseFloat(eCss[key])||1;}diffData[key]=data["data"][key]-nowData[key];}}//判断对于速度类型的传值问题if(!data["options"]){oType="Linear";oSpeed="easeIn";}else{if(!data["options"]["speed"]){oSpeed="easeIn";}if(!data["options"]["speed"]){oType="Linear"}}//请求动画帧进行变量的function calcSeconds(){var interTime=new Date()-startTime;interTime=Math.min(time,interTime);for(var key in diffData){var val=Tween[oType][oSpeed](interTime,nowData[key],diffData[key],time);if(key!=="opacity"){ele.style[key]=val+"px";}else{ele.style[key]=val;ele.style["filter"]="alpha(opacity="+(val*100)+")";}}if(interTime===time){callback&&callback();}else{requestAnimationFrame(calcSeconds);}}requestAnimationFrame(calcSeconds);}</script></html>
获取当前时间
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><style>#box{width: 400px;height: 400px;border: 1px solid red;margin: 200px auto 0px;background-color: green;}</style></head><body><div id="box"></div><script>function getMyTime(){var t=new Date();var weeks=["日","一","二","三","四","五","六"]var year=t.getFullYear();var month=t.getMonth()>8?t.getMonth()+1:"0"+(t.getMonth()+1);var day=t.getDate()>9?t.getDate():"0"+t.getDate();var week=weeks[t.getDay()];var hours=t.getHours()>9?t.getHours():"0"+t.getHours();var minutes=t.getMinutes()>9?t.getMinutes():"0"+t.getMinutes();var seconds=t.getSeconds()>9?t.getSeconds():"0"+t.getSeconds();var time=year+"年"+month+"月"+day+"日 星期"+week+" "+hours+":"+minutes+":"+seconds;return time;}box.innerHTML=getMyTime();setInterval(function(){box.innerHTML=getMyTime();},1000);</script></body></html>
时间图标
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><style>#box{width: 900px;height: 700px;border: 1px solid red;margin: 200px auto 0px;background-color: green;}span{font-size: 50px;position: relative;top: -12px;}</style></head><body><div id="box"><img src="images/0.png" class="photo" alt=""><img src="images/1.png" class="photo" alt=""><img src="images/2.png" class="photo" alt=""><img src="images/3.png" class="photo" alt=""><span>年</span><img src="images/4.png" class="photo" alt=""><img src="images/5.png" class="photo" alt=""><span>月</span><img src="images/6.png" class="photo" alt=""><img src="images/7.png" class="photo" alt=""><span>日</span><img src="images/8.png" class="photo" alt=""><img src="images/9.png" class="photo" alt=""><img src="images/d.png" class="" alt=""><img src="images/9.png" class="photo" alt=""><img src="images/9.png" class="photo" alt=""><img src="images/d.png" class="" alt=""><img src="images/9.png" class="photo" alt=""><img src="images/9.png" class="photo" alt=""></div><script>function getMyNumber(num,index){if(isNaN(num)){return null;}var len=new String(num).length;if(index===1){return Math.floor(num/Math.pow(10,len-index));}else if(index===len){return Math.floor(num%10);}else{return Math.floor(num%Math.pow(10,len-index+1)/Math.pow(10,len-index));}}function photoTime(){var t=new Date();var year=t.getFullYear();var t0=getMyNumber(year,1);var t1=getMyNumber(year,2);var t2=getMyNumber(year,3);var t3=getMyNumber(year,4);var month=t.getMonth()>8?t.getMonth()+1:"0"+(t.getMonth()+1);var t4=getMyNumber(month,1);var t5=getMyNumber(month,2);var day=t.getDate()>9?t.getDate():"0"+t.getDate();var t6=getMyNumber(day,1);var t7=getMyNumber(day,2);var hours=t.getHours()>9?t.getHours():"0"+t.getHours();var t8=getMyNumber(hours,1);var t9=getMyNumber(hours,2);var minutes=t.getMinutes()>9?t.getMinutes():"0"+t.getMinutes();var t10=getMyNumber(minutes,1);var t11=getMyNumber(minutes,2);var seconds=t.getSeconds()>9?t.getSeconds():"0"+t.getSeconds();var t12=getMyNumber(seconds,1);var t13=getMyNumber(seconds,2);for(let j=0;j<14;j++){var b="t"+j;i[j].setAttribute("src","images/"+eval(b)+".png");}}var i=document.getElementsByClassName("photo");photoTime();setInterval(photoTime,1000);</script></body></html>
图片飞出
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title></head><style>#box{width: 400px;height: 400px;background-color: transparent;position: relative;}.smallbox{width: 40px;height: 40px;background: transparent;position: absolute;}</style><body><div id="box"><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div><div class="smallbox"></div></div></body><script src="js/animation.js"></script><script>var box=document.getElementById("box");var smallBox=document.querySelectorAll(".smallbox");function getMyNumber(num,index){if(isNaN(num)){return null;}var len=new String(num).length;if(index===1){return Math.floor(num/Math.pow(10,len-index));}else if(index===len){return Math.floor(num%10);}else{return Math.floor(num%Math.pow(10,len-index+1)/Math.pow(10,len-index));}}for(let i=0;i<100;i++){if(i<10){var ii="0"+i;smallBox[i].index=ii;}else{smallBox[i].index=i;}}for(let i=0;i<100;i++){var random=Math.floor(Math.random()*100);var medium=smallBox[i].index;smallBox[i].index=smallBox[random].index;smallBox[random].index=medium;}var j=0;var time=setInterval(function(){if(j>99){clearTimeout(time);}let y=getMyNumber(smallBox[j].index,1)*40;let x=getMyNumber(smallBox[j].index,2)*40;console.log("background: url('images/cd.JPG') no-repeat "+(-x)+"px "+(-y)+"px;");smallBox[j].style.cssText="background: url('images/cd.JPG') no-repeat "+(-x)+"px "+(-y)+"px;";animation(smallBox[j],{data:{top:y,left:x}},500);j++;},500);</script></html>
无缝轮播
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title></head><style>*{list-style: none;margin: 0px;padding: 0px;}.big-box{border: 1px solid blue;margin: 200px auto;width: 700px;overflow: hidden;position: relative;}.box{width: 8888px;position: relative;overflow: hidden;}.box>li{float: left;}.box li>img{vertical-align: top;}.big-box .left{position: absolute;width: 40px;height: 40px;top: 50%;transform: translate(0%, -50%);left: -20px;background: deeppink;opacity: 0.8;border-radius: 50%;cursor: pointer;}.big-box .right{position: absolute;width: 40px;height: 40px;top: 50%;transform: translate(0%, -50%);right: -20px;background: deeppink;opacity: 0.8;border-radius: 50%;cursor: pointer;}.circle{overflow: hidden;width: 150px;background-color: black;opacity: 0.3;position: absolute;bottom: 20px;left: 50%;transform: translate(-50%,0%);border-radius: 20px;}.circle li{float: left;width: 20px;height: 20px;border-radius: 50%;margin:2px 5px;background: orange;opacity: 0.3;}</style><body><div class="big-box"><ul class="box"><li><img src="images/cd (1).JPG" alt=""></li><li><img src="images/cd (2).JPG" alt=""></li><li><img src="images/cd (3).JPG" alt=""></li><li><img src="images/cd (4).JPG" alt=""></li><li><img src="images/cd (5).JPG" alt=""></li><li><img src="images/cd (1).JPG" alt=""></li></ul><span class="left"></span><span class="right"></span><ul class="circle"><li></li><li></li><li></li><li></li><li></li></ul></div><script src="js/animation.js"></script><script>var left=document.querySelector(".left");var right=document.querySelector(".right");var box=document.querySelector(".box");var circleLi=document.querySelectorAll(".circle li");var len=circleLi.length;var num=0;var date=new Date();circleLi[num].style.opacity="1";function clearOpacity(){for(let i=0;i<len;i++){circleLi[i].style.opacity="0.3";}if(num===0||num===5){circleLi[0].style.opacity="1";}else{circleLi[num].style.opacity="1";}}right.onclick=function(){num++;if(num>5)num=1;if((new Date()-date)>500){clearOpacity();console.log(num);animation(box,{data:{left:-700*num}},500,function(){if(num===5)box.style.left="0px";});}date=new Date();}left.onclick=function(){num--;if(num<0)num=4;if((new Date-date)>500){if((num+1)===5)box.style.left="-3500px"clearOpacity()console.log(num);animation(box,{data:{left:-700*num}},500);}date=new Date();}for(let i=0;i<len;i++){circleLi[i].onclick=function(){for(let i=0;i<len;i++){circleLi[i].style.opacity="0.3";}this.style.opacity="1";animation(box,{data:{left:-700*i}},500);}}</script></body></html>
小球运动
示意图:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title> </title><style>#ball{width: 200px;height: 200px;background-color: purple;position: absolute;top: 0px;left: 0px;border-radius: 50%;}</style></head><body><div id="ball"></div></body><script>var ball=document.getElementById("ball");var speedX=4; //小球每次x轴移动的像素var speedY=4; //小球每次y轴移动的像素var moveX=ball.offsetLeft; //小球x轴移动了多少var moveY=ball.offsetTop; //小球y轴移动了多少var maxX=document.documentElement.clientWidth-ball.offsetWidth;//x轴最大值var maxY=document.documentElement.clientHeight-ball.offsetHeight;window.onresize=function(){//监控窗口变化maxY=document.documentElement.clientHeight-ball.offsetHeight;maxX=document.documentElement.clientWidth-ball.offsetWidth;};function ballRun(){moveX+=speedX;moveY+=speedY;if(moveX<=0||moveX>=maxX){//防止超过边界值,以及变化方向moveX=Math.max(0,moveX);moveX=Math.min(maxX,moveX);speedX=-speedX;}if(moveY<=0||moveY>=maxY){moveY=Math.max(0,moveY);moveY=Math.min(maxY,moveY);speedY=-speedY;}ball.style.left=moveX+"px";//改变小球的绝对定位进行移动ball.style.top=moveY+"px";requestAnimationFrame(ballRun);}ballRun();</script></html>
盒子移动和放大缩小
重点代码:
boxMoveX=Math.min(mouseMoveX-mouseNowX,parseFloat(getStyle(box).width)-100);
boxMoveY=Math.min(mouseMoveY-mouseNowY,parseFloat(getStyle(box).height)-100);
<!DOCTYPE html><html lang="en" ondragstart="return false"><head><meta charset="UTF-8"><title>盒子拖放</title><style>*{margin: 0px;padding: 0px;list-style: none;}#box{width: 300px;height: 300px;background-color: purple;position: absolute;top: 0px;left: 0px;}#box>div{width: 20px;height: 20px;background-color: red;position: absolute;}#eft-top{top: 0px;left: 0px;}#left{top: 50%;left: 0px;transform: translate(0, -50%);}#left-bottom{bottom:0px;left: 0px;}#right-top{right: 0px;top: 0px;}#right{right: 0px;top: 50%;transform: translate(0, -50%);}#right-bottom{right: 0px;bottom: 0px;}#top{left: 50%;top: 0px;transform: translate(-50%, 0);}#bottom{left: 50%;bottom: 0px;transform: translate(-50%, 0);}</style></head><body><div id="box"><div id="top"></div><div id="bottom"></div><div id="left-top"></div><div id="left"></div><div id="left-bottom"></div><div id="right-top"></div><div id="right"></div><div id="right-bottom"></div></div><script>var box=document.getElementById("box");//获取盒子的idbox.onmousedown=function(e){ //盒子的鼠标按下事件e=e||window.event; //对于鼠标对象的ie兼容var mouseDownX=e.clientX, //获取第一下鼠标按下的点距离左边x轴值mouseDownY=e.clientY; //获取第一下鼠标按下的点距离右边y轴值targetId=e.target.id;document.onmousemove=function(e){//鼠标按下后的鼠标移动事件e=e||window.event; //兼容鼠标对象//假设场景有很多定位元素嵌套,建议还是写个方法获取到它的左边和上边的距离var boxX=box.offsetLeft, //盒子到浏览器左边距离boxY=box.offsetTop, //盒子到浏览器上边距离mouseMoveX=e.clientX, //获取每次鼠标移动获取的点距离左边x轴值mouseMoveY=e.clientY, //获取每次鼠标移动获取的点距离左边y轴值mouseDiffX=mouseMoveX-mouseDownX, //获取第一次按下和每次移动x轴的差值mouseDiffY=mouseMoveY-mouseDownY; //获取第一次按下和每次移动Y轴的差值boxWidth=box.offsetWidth; //获取盒子模型的宽度boxHeight=box.offsetHeight; //获取盒子模型的高度mouseDownX=mouseMoveX; //每次移动完都将每次移动的x轴的值重新赋值给第一次鼠标按下的x轴的值mouseDownY=mouseMoveY; //每次移动完都将每次移动的x轴的值重新赋值给第一次鼠标按下的y轴的值switch(targetId){//根据事件代理的得到的id使用switch语句赋予对应功能case "top":mouseDiffY=Math.min(mouseDiffY,boxHeight-150);//最重点就是这个,限制盒子的大小box.style.height=boxHeight-mouseDiffY+"px";box.style.top=boxY+mouseDiffY+"px";break;case "bottom":mouseDiffY=Math.max(mouseDiffY,-(boxHeight-150));box.style.height=boxHeight+mouseDiffY+"px";break;case "left":mouseDiffX=Math.min(mouseDiffX,boxWidth-150);box.style.width=boxWidth-mouseDiffX+"px";box.style.left=boxX+mouseDiffX+"px";break;case "right":mouseDiffX=Math.max(mouseDiffX,-(boxWidth-150));box.style.width=boxWidth+mouseDiffX+"px";break;case "left-top":mouseDiffX=Math.min(mouseDiffX,boxWidth-150);mouseDiffY=Math.min(mouseDiffY,boxHeight-150);box.style.width=boxWidth-mouseDiffX+"px";box.style.left=boxX+mouseDiffX+"px";box.style.height=boxHeight-mouseDiffY+"px";box.style.top=boxY+mouseDiffY+"px";break;case "left-bottom":mouseDiffX=Math.min(mouseDiffX,boxWidth-150);mouseDiffY=Math.max(mouseDiffY,-(boxHeight-150));box.style.width=boxWidth-mouseDiffX+"px";box.style.left=boxX+mouseDiffX+"px";box.style.height=boxHeight+mouseDiffY+"px";break;case "right-top":mouseDiffX=Math.max(mouseDiffX,-(boxWidth-150));mouseDiffY=Math.min(mouseDiffY,boxHeight-150);box.style.width=boxWidth+mouseDiffX+"px";box.style.height=boxHeight-mouseDiffY+"px";box.style.top=boxY+mouseDiffY+"px";break;case "right-bottom":mouseDiffX=Math.max(mouseDiffX,-(boxWidth-150));mouseDiffY=Math.max(mouseDiffY,-(boxHeight-150));box.style.width=boxWidth+mouseDiffX+"px";box.style.height=boxHeight+mouseDiffY+"px";break;default:box.style.left=boxX+mouseDiffX+"px"; //然后把原本距离左边的值加上应该移动的值就是盒子的x轴定位值box.style.top=boxY+mouseDiffY+"px"; //然后把原本距离上边的值加上应该移动的值就是盒子的y轴定位值}}}document.onmouseup=function(){//鼠标抬起事件,清除掉鼠标的移动事件this.onmousemove=null;}</script></body></html>
表格增删选择
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>TEST</title><style>*{margin: 0px;padding: 0px;}div{text-align: center;}table{margin: 0px auto;border-collapse: collapse;width: 500px;height:70px;}tr,td,th{border: 1px solid black;}</style></head><body><div><p>请输入信息</p>性别:<input type="text" id="sex"><br>年龄:<input type="text" id="age"><br>姓名:<input type="text" id="nam"><br><input type="button" value="提交" id="input"><input type="button" value="全选" id="all"><input type="button" value="反选" id="reverse"><input type="button" value="删除选中的" id="deleteSome"><table id="table"><tr><th>性别</th><th>年龄</th><th>姓名</th><th>勾选</th><th>删除</th></tr><tr><td>男</td><td>13</td><td>呵呵大</td><td><input type="checkbox" class="che"></td><td class="deleteX" onclick="deleteTr.call(this)">X</td></tr></table></div><script>var input=document.getElementById("input"),sex=document.getElementById("sex"),age=document.getElementById("age"),nam=document.getElementById("nam"),all=document.getElementById("all"),reverse=document.getElementById("reverse"),deleteSome=document.getElementById("deleteSome"),deleteX=document.getElementById("deleteX");table=document.getElementById("table");tbody=table.children[0];input.onclick=function(){var newTr=document.createElement("tr"),newStd=document.createElement("td"),newAtd=document.createElement("td"),newNtd=document.createElement("td"),newItd=document.createElement("td"),newDtd=document.createElement("td"),newInput=document.createElement("input");newStd.innerHTML=sex.value;newAtd.innerHTML=age.value;console.log(nam.value);newNtd.innerHTML=nam.value;newInput.setAttribute("type","checkbox");newInput.className="che";newDtd.className="deleteX";newDtd.setAttribute("onclick","deleteTr.call(this)");newDtd.innerHTML="X";newItd.appendChild(newInput);newTr.appendChild(newStd);newTr.appendChild(newAtd);newTr.appendChild(newNtd);newTr.appendChild(newItd);newTr.appendChild(newDtd);tbody.appendChild(newTr);sex.value="";age.value="";nam.value="";}all.onclick=function(){var checkBox=document.getElementsByClassName("che");var cLength=checkBox.length;for(let i=0;i<cLength;i++){checkBox[i].checked="true";}}reverse.onclick=function(){var checkBox=document.getElementsByClassName("che");var cLength=checkBox.length;for(let i=0;i<cLength;i++){checkBox[i].checked=!checkBox[i].checked;}}deleteSome.onclick=function(){var checkBox=document.getElementsByClassName("che");for(let i=0;i<checkBox.length;i++){if(checkBox[i].checked){tbody.removeChild(checkBox[i].parentNode.parentNode);i-=1;}}}function deleteTr(){tbody.removeChild(this.parentNode);}</script></body></html>
画板
<!DOCTYPE html><html lang="en" ondragstart="return false"><head><meta charset="UTF-8"><title>paint</title><style>*{margin: 0px;padding: 0px;}#paint{border: 3px solid black;height: 600px;margin-top: 100px;}</style></head><body><input type="text" placeholder="size" id="size"><input type="color" id="background-color"><input type="color" id="pen-color"><input type="button" id="clear" value="清空"><input type="button" id="go-back" value="撤回"><div id="paint"></div><script>var penColor=document.getElementById("pen-color"),boardColor=document.getElementById("background-color"),penSize=document.getElementById("size"),board=document.getElementById("paint"),clear=document.getElementById("clear"),goBack=document.getElementById("go-back"),penColorSave="black",penSizeSave=10,goBackArr=[],goBackSave=[];boardColor.onchange=function(){board.style.backgroundColor=this.value;}penColor.onchange=function(){penColorSave=this.value;}penSize.onchange=function(){penSizeSave=this.value;}clear.onclick=function(){var cLength=board.children.length;for(let i=0;i<cLength;i++){board.removeChild(board.children[0]);}}goBack.onclick=function(){var goBackArrLength=goBackArr.length;if(goBackArrLength===0){return;}else{var lastSave=goBackArr[goBackArrLength-1],lastSaveLength=lastSave.length;for(let i=0;i<lastSaveLength;i++){board.removeChild(lastSave[i]);}}goBackArr.pop();}board.onmousedown=function(e){e=e||window.event;var mouseDownX=e.clientX,mouseDownY=e.clientY,mouseDownDiv=document.createElement("div");mouseDownDiv.style.cssText="width:"+penSizeSave+"px;height:"+penSizeSave+"px;background-color:"+penColorSave+";position:absolute;top:"+(mouseDownY-penSizeSave*0.5)+"px;left:"+(mouseDownX-penSizeSave*0.5)+"px;border-radius:50%";this.appendChild(mouseDownDiv);goBackSave.push(mouseDownDiv);this.onmousemove=function(e){e=e||window.event;var mouseMoveX=e.clientX,mouseMoveY=e.clientY,mouseMoveDiv=document.createElement("div");mouseMoveDiv.style.cssText="width:"+penSizeSave+"px;height:"+penSizeSave+"px;background-color:"+penColorSave+";position:absolute;top:"+(mouseMoveY-penSizeSave*0.5)+"px;left:"+(mouseMoveX-penSizeSave*0.5)+"px;border-radius:50%";this.appendChild(mouseMoveDiv);goBackSave.push(mouseMoveDiv);}document.onmouseup=function(){goBackArr.push(goBackSave);goBackSave=[];board.onmousemove=null;this.onmouseup=null;}}</script></body></html>
JOSNP百度案例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0px;
padding: 0px;
list-style: none;
}
html {
min-width: 800px;
}
#box {
cursor: default;
}
.nav {
padding-top: 18px;
padding-right: 12px;
}
.nav li {
font-size: 14px;
font-weight: bold;
float: right;
margin-left: 25px;
}
.nav a {
color: black;
}
.more-product {
background-color: #3388ff;
padding: 4px;
position: relative;
top: -4px;
}
.more-product>a {
text-decoration: none;
color: white;
}
h1 {
margin-top: 170px;
text-align: center;
}
h1 img {
width: 270px;
}
#search {
width: 532px;
height: 34px;
padding-left: 10px;
border: 1px solid #cccccc;
border-right: 0px;
outline: none;
}
#goBaidu {
color: white;
font-size: 15px;
text-decoration: none;
background-color: #3388ff;
padding: 8px;
margin-left: -6px;
position: relative;
top: 1px;
}
.center {
position: relative;
text-align: center;
}
.center i {
display: inline-block;
position: absolute;
top: 8px;
left: -26px;
width: 18px;
height: 22px;
background: url(images/camera_new_5606e8f.png) no-repeat 0px 3px;
cursor: pointer;
}
.two-code {
text-align: center;
}
.two-code img {
display: block;
margin: 380px auto 5px auto;
}
.two-code span {
font-size: 14px;
font-weight: bold;
}
footer {
text-align: center;
margin-top: 20px;
font-size: 12px;
}
.f-firstline a {
margin-right: 20px;
color: #a69999;
}
.f-firstline a:nth-of-type(4) {
margin: 0px;
}
.lastline {
position: relative;
color: #a69999;
margin-top: 10px;
}
.lastline a {
color: #a69999;
}
#box {
width: 541px;
position: absolute;
border: 1px solid #cccccc;
border-top: 0px;
left: 651px;
text-align: left;
}
}
</style>
</head>
<body>
<ul class="nav">
<li class="more-product">
<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>
<li>
<a href="">hao123</a>
</li>
<li>
<a href="">新闻</a>
</li>
</ul>
<h1>
<img src="images/bd_logo1.png" alt="">
</h1>
<div class="center">
<input type="text" placeholder="" id="search">
<a href="" id="goBaidu">
<span id="btn">百度一下</span>
<i id="icon"></i>
</a>
<ul id="box">
</ul>
</div>
<div class="two-code">
<img src="images/zbios_efde696.png" alt="">
<span>百度</span>
</div>
<footer>
<div class="f-firstline">
<a href="">把百度设为主页</a>
<a href="">关于百度</a>
<a href="">About Baidu</a>
<a href="">百度推广</a>
</div>
<div class="lastline">
©2018 Baidu
<a href="">使用百度前必读</a>
<a href="">意见反馈</a> 京ICP证030173号 京公网安备11000002000001号
<i class="last-icon-one"></i>
<i class="last-icon-two"></i>
</div>
</footer>
<script>
var search = document.getElementById("search"),
head = document.getElementsByTagName("head")[0],
arr = [],
box = document.getElementById("box"),
btn = document.getElementById("btn"),
goBaidu = document.getElementById("goBaidu"),
icon = document.getElementById("icon");
arrL = 0;
search.oninput = function () {
for (key in head.children) {
if (head.children[key].name === "baidu") {
head.removeChild(head.children[key]);
}
}
var searchFont = this.value,
myScript = document.createElement("script");
head.appendChild(myScript);
myScript.src = "https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=" + searchFont + "&json=1&p=3&sid=1424_25809_21101_26350_27244_22159&req=2&csor=2&pwd=r&cb=fnShow";
myScript.name = "baidu";
}
function fnShow(json) {
box.innerHTML = "";
arr = json.s;
arrL = arr.length;
console.log(arr);
for (let i = 0; i < arrL; i++) {
var li = document.createElement("li");
li.innerHTML = " " + arr[i];
li.style.cssText = "height:30px;padding-left: 10px;line-height:30px";
li.index = i;
li.onmouseover = function () {
removeAll();
this.style.backgroundColor = "#e1e1e1";
arrL = this.index;
}
box.appendChild(li);
}
}
function getStyle(ele) {
return ele.currentStyle || getComputedStyle(ele);
}
function removeAll() {
var liC = box.children;
for (let i = 0; i < arr.length; i++) {
liC[i].style.backgroundColor = "#ffffff";
}
}
search.onkeyup = function (e) {
var lis = box.children;
if (e.keyCode === 38) {
removeAll();
--arrL < 0 ? (arrL = arr.length - 1) : arrL;
lis[arrL].style.backgroundColor = "#e1e1e1";
} else if (e.keyCode === 40) {
removeAll();
++arrL > arr.length - 1 ? arrL = 0 : arrL;
lis[arrL].style.backgroundColor = "#e1e1e1";
} else if (e.keyCode === 13) {
console.log(1111111111);
for (let i = 0; i < arr.length; i++) {
console.log(lis[i]);
if (getStyle(lis[i]).backgroundColor === "rgb(225, 225, 225)") {
window.location.href = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=" + lis[i].innerHTML;
}
}
if (search.value.trim === "") {
window.location.href = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=" + search.value;
}
}
}
search.onfocus = function () {
this.style.borderColor = "#4791ff";
}
search.onblur = function () {
this.style.borderColor = "#cccccc";
}
btn.onclick = function () {
var searchFont = search.value;
if (searchFont != "") {
goBaidu.href = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=" + searchFont;
}
}
box.onclick = function (e) {
window.location.href = "https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=" + e.target.innerHTML;
}
box.onmouseout = function () {
removeAll();
}
icon.onmouseover = function () {
this.style.backgroundPositionY = "-17px";
}
icon.onmouseout = function () {
this.style.backgroundPositionY = "3px";
}
</script>
</body>
</html>
