<html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="main" style="position:relative"></div> <script> function Rect(options) { this._init(options); } Rect.prototype={ //屬性 _init:function (options) { //父標簽 this.parentId=options.parentId; //位置 this.left=options.left||100; this.top=options.top||100; //自身的屬性 this.width=options.width||100; this.height=options.height||50; this.bgColor=options.bgColor||'#000'; this.borderRadius=options.borderRadius||0; this.border=options.border||'none'; }, //行為 render:function () { //1.獲取父標簽 var parentEle=document.getElementById(this.parentId); //2.創建矩形標簽 var reactEle=document.createElement('div'); parentEle.appendChild(reactEle); reactEle.style.position='absolute'; reactEle.style.left=this.left+'px'; reactEle.style.top=this.top+'px'; reactEle.style.width=this.width+'px'; reactEle.style.height=this.height+'px'; reactEle.style.backgroundColor=this.bgColor; reactEle.style.border=this.border; reactEle.style.borderRadius=this.borderRadius+'px'; } } //創建矩形對象 var rect=new Rect({ parentId:'main', left:200, top:200, width:500, height:300, bgColor:'pink', borderRadius:20, border:'10px solid #000' }); rect.render(); </script> </body> </html>
面向對象js計算功能
<html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <script type="text/html"></script> <script> var Caculator={ result:0, jia:function (num) { this.result+=num; return this; }, jian:function (num) { this.result-=num; return this; }, cheng:function(num){ this.result*=num; return this; }, chu:function (num) { this.result/=num; return this; }, log:function () { console.log(this.result); return this; }, clean:function () { this.result=0; return this; } }; Caculator.jia(6); Caculator.jia(6); Caculator.cheng(2); Caculator.chu(3); Caculator.jian(4); console.jia(6).jia(6).cheng(2).chu(3).jian(4).log(); </script> </body> </html>
面向對象方法分裝tab選項卡
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>選項卡</title> <link rel="stylesheet" href="css/index.css"> </head> <body> <div id="tab"> <!--頭部--> <div id="tab_header"> <ul> <li class="selected">公告</li> <li>規則</li> <li>論壇</li> <li>安全</li> <li>公益</li> </ul> </div> <!--主要內容--> <div id="tab_content"> <div class="dom" style="display: block"> <ul> <li><a href="#">數據七夕:金牛愛送玫瑰</a></li> <li><a href="#"> 阿里打造"互聯網監管"</a></li> <li><a href="#">10萬家店60萬新品</a></li> <li><a href="#">全球最大網上時裝周</a></li> </ul> </div> <div class="dom" > <ul> <li> <a href="#">“全額返現”要管控啦</a> </li> <li> <a href="#">淘寶新規發布匯總(7月)</a> </li> <li> <a href="#">炒信規則調整意見反饋</a> </li> <li> <a href="#">質量相關規則近期變更</a> </li> </ul> </div> <div class="dom"> <ul> <li> <a href="#">阿里建商家全鏈路服務</a> </li> <li> <a href="#">個性化的消費時代來臨</a> </li> <li> <a href="#">跨境貿易是中小企業機</a> </li> <li> <a href="#">美妝行業虛假信息管控</a> </li> </ul> </div> <div class="dom"> <ul> <li> <a href="#">接次文件,毀了一家店</a> </li> <li> <a href="#">賬號安全神器阿里錢盾</a> </li> <li> <a href="#">新版阿里110上線了</a> </li> <li> <a href="#">賣家學違禁避免被處罰</a> </li> </ul> </div> <div class="dom"> <ul> <li> <a href="#">為了公益high起來</a> </li> <li> <a href="#">魔豆媽媽在線申請</a> </li> </ul> </div> </div> </div> <script src="s/Tab.js"></script> <script src="s/index.js"></script> </body> </html>
*{ margin: 0; padding: 0; list-style: none; box-sizing: border-box; } a{ text-decoration: none; color: #000; } #tab{ width: 500px; height: 120px; border: 1px solid #ccc; margin: 100px auto; } /*選項卡--頭部*/ #tab_header{ height: 29px; line-height: 28px; /*background-color: red;*/ overflow: hidden; } #tab_header ul{ width: 500px; display: flex; justify-content: space-around; align-items: center; } #tab_header ul li{ flex: 1; text-align: center; background-color: #e8e8e8; border-bottom: 1px solid #cccccc; } #tab_header ul li.selected{ background-color: #ffffff; border-bottom: none; /*左右線條*/ border-left: 1px solid #cccccc; border-right: 1px solid #cccccc; } #tab_header ul li:nth-child(1){ border-left: none; } #tab_header ul li:nth-last-child{ border-right: none; } #tab_header ul li:hover{ cursor: pointer; font-weight: bolder; color: orangered; } /*選項卡--頭部*/ /*選項卡--主要內容*/ #tab_content{ } #tab_content .dom{ padding-top: 20px; display: none; } #tab_content .dom ul li{ float: left; width: 220px; /*background-color: red;*/ text-align: center; margin: 5px; } /*選項卡--主要內容*/
var tab = new TabsFn(); tab.init(); function $(id) { return typeof id === 'string' ? document.getElementById(id) : null; }
function TabsFn() { // 屬性 this.lis = $('tab_header').getElementsByTagName('li'); this.contents = $('tab_content').getElementsByClassName('dom'); } TabsFn.prototype = { init: function(){ // 1. 設置索引 this.setId(); // 2. 綁定事件 this.bindEvent(); }, // 設置索引(id) setId: function () { for(var i=0; i<this.lis.length; i++){ this.lis[i].id = i; } }, // 綁定事件 bindEvent: function () { // 備份指針 var self = this; for (var i = 0; i < this.lis.length; i++) { this.lis[i].onmouseover = function () { // 2.1 所有的都不被選中 for(var j=0; j<self.lis.length; j++){ self.lis[j].className = ''; self.contents[j].style.display = 'none'; } // 2.2 當前的li被選中 this.className = 'selected'; self.contents[this.id].style.display = 'block'; } } } };
絢麗五彩小球:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> *{ margin: 0; padding:0; list-style: none; } html,body,div{ width: 100%; height:100%; } #box{ background-color: #000; position: relative; } </style> </head> <body> <div id="box"> </div> <script src="五彩特效/js/Underscore-min.js"></script> <script src="五彩特效/js/Ball.js"></script> <script type="text/html"> //1.獲取父標簽 var box=document.getElementById('box'); var ball=new Ball({ parentId:'box', left:100, top:200, bgColor:'green' }); ball.render(); </script> <script> //1.獲取需要的標簽 var box=document.getElementById('box'); //2.監聽鼠標在盒子上的移動 var colorArr=['red','green','blue','yellow','orange','purple','pink']; var ballArr=[]; box.onmousemove=function (ev) { //2.1創建小球 var ball=new Ball({ parentId:'box', left:ev.clientX, top:ev.clientY, bgColor:colorArr[_.random(0,colorArr.length-1)] }); //2.2創建的小球放入數組中 ballArr.push(ball); }; //3.設置定時器 setInterval(function(){ //1.清除上一幀產生的小球 for(var i=0;i<box.children.length;i++){ box.children[i].remove(); } //2.讓小球移動變小 for(var j=0;j<ballArr.length;j++){ ballArr[j].render(); ballArr[j].update(); } },50); </script> </body> </html>
function Ball(options){ this._init(options); } Ball.prototype={ _init:function (options) { //1.必要屬性 this.parentId=options.parentId; this.left=options.left; this.top=options.top; this.r=60; this.bgColor=options.bgColor||'red'; //2.小球變化的量 this.dX=_.random(-5,5); this.dY=_.random(-5,5); this.dR=_.random(1,3);//半徑 }, //行為 render:function () { var parentNode=document.getElementById(this.parentId); var childNode=document.createElement('div'); parentNode.appendChild(childNode); childNode.style.position='absolute'; childNode.style.left=this.left+'px'; childNode.style.top=this.top+'px'; childNode.style.width=this.r+'px'; childNode.style.height=this.r+'px'; childNode.style.borderRadius='50%'; childNode.style.backgroundColor=this.bgColor; }, update:function () { this.left+=this.dX; this.top+=this.dY; this.r-=this.dR; //判斷 if(this.r<=0){ this.r=0; //把小球移除 ballArr=_.without(ballArr,this); } } };