javascript字體顏色控件的開發(fā) JS實現(xiàn)字體控制
我們在用JS寫程序的時候,經(jīng)常會遇到像在程序中直接控制字體的大小顏色等基本信息,尤其的在后臺方便,小編測試用javascript寫了一個這個控件,喜歡的拿走吧。
以上就是用JS寫的運行效果,一下我們來看看代碼具體實現(xiàn)方式:
知識點:for循環(huán)語句,字符串方法,進制轉(zhuǎn)換,this指向問題,變量,數(shù)組方法,基本事件處理等。
<!doctype html><!--聲明html版本編寫指令 H5--> <html> <head> <!--聲明頁面編碼 uft-8 國際編碼--> <meta charset="UTF-8"> <!--網(wǎng)站三要素 關(guān)鍵字 頁面描述 標(biāo)題--> <meta name="Keywords" content=""> <meta name="Description" content=""> <title>Document</title> <style type="text/css"> *{margin:0px;padding:0px;font-family:"微軟雅黑";} #box{width:400px; height:450px; background:#000; margin:50px auto; border:5px solid #000; border-radius:5px; } #show{width:100%; height:190px; background:#00ccff; line-height:200px; font-size:8px; font-weight:bold; text-align:center; border-radius:5px; } #font ul{margin-left:10px; margin-top:30px;} #font ul li{width:380px; height:50px; list-style-type:none; color:#ddd;} #font ul li span{display:block; width:50px; height:50px; line-height:50px; text-align:right; float:left; } #font ul li .roll{width:290px; height:50px; float:left; line-height:50px; padding-left:30px; } #font ul li .roll .move{width:200px; height:12px; display:inline-block; background:#fff; margin-left:15px; margin-right:15px; border-radius:10px; background-size:cover; border:1px solid #fff; position:relative; } #font ul li .roll .move .prog{position:absolute; top:0px; width:0px; height:12px; border-radius:10px 0 0 10px; background:url("images/slider-bar.png") bottom;} #font ul li .roll .move .prog .but{width:22px; height:22px; position:absolute; top:-3px; background:url("images/dot-bg.png") no-repeat; cursor:pointer; left:0px;} </style> </head> <body> <div id="box"> <div id="show">云煙好帥啊</div> <div id="font"> <ul> <li> <span>字號</span> <div class="roll"> 8 <div class="move"> <div class="prog"> <div class="but"></div> </div> </div> 40px </div> </li> <li> <span>顏色R</span> <div class="roll"> 0 <div class="move"> <div class="prog"> <div class="but"></div> </div> </div> 255 </div> </li> <li> <span>G</span> <div class="roll">0 <div class="move"> <div class="prog"> <div class="but"></div> </div> </div> 255 </div> </li> <li> <span>B</span> <div class="roll"> 0 <div class="move"> <div class="prog"> <div class="but"></div> </div> </div> 255 </div> </li> </ul> </div> </div> </body> <script type="text/javascript"> /* 1.JS主要針對頁面當(dāng)中的HTML元素以及樣式進行修改,從而得到特效 2.我們一般用JS寫特效,要知道觸發(fā)特效的條件是什么 3.促發(fā)這個條件的對象是誰 4.寫這個事件里面發(fā)生的事情 5.獲取鼠標(biāo)的移動水平方向的距離 6.this代表當(dāng)前執(zhí)行這個事件的對象 (這個事件是誰做的 那么這個事件當(dāng)中的this就是誰) */ var oBut =document.getElementsByClassName("but");//通過元素的類名 是以一個數(shù)組的形式保存 var oFont =document.getElementById("show");//通過ID名獲取元素 var oProg =document.getElementsByClassName("prog"); var width = [0,0,0,0]; var rgb = ["00","00","00"]; for (var i=0;i<oBut.length;i++)//重復(fù)執(zhí)行某一個語句(循環(huán)體)限制條件 { oBut[i].index=i;//自定義一個index屬性保存i oBut[i].onmousedown =function(e){//鼠標(biāo)點擊下去 //event事件對象 clientX clientY var e = e || window.event;//做IE兼容 this.x =e.clientX;//當(dāng)前對象的屬性去保存這個值(自定義一個x屬性) var thisIndex = this;//定義一個變量保存this指向?qū)ο? var lastLeft = width[this.index]; //console.log("鼠標(biāo)點擊下去"); document.onmousemove =function(e){//鼠標(biāo)移動事件 //console.log("鼠標(biāo)移動事件"); var e = e || window.event;//做IE兼容 width[thisIndex.index] =e.clientX-thisIndex.x+lastLeft; if (width[thisIndex.index]>180)width[thisIndex.index]=180; if (width[thisIndex.index]<0)width[thisIndex.index]=0; oBut[thisIndex.index].style.left =width[thisIndex.index]+"px"; oProg[thisIndex.index].style.width =width[thisIndex.index]+"px"; if (thisIndex.index ==0) { oFont.style.fontSize =width[thisIndex.index]/180*40+8+"px"; //駝峰命名法 }else{ var num = parseInt(width[thisIndex.index]/180*255); /*if (num<16) { numStr ="0"+num.toString(16); }else{ numStr = num.toString(16); }*/ rgb[thisIndex.index-1] =num<16?"0"+num.toString(16):num.toString(16); oFont.style.color ="#"+rgb[0]+rgb[1]+rgb[2]; } } document.onmouseup =function(){//鼠標(biāo)松開事件 //console.log("鼠標(biāo)松開事件"); this.onmousemove =null;//終止移動事件 this.onmouseup =null;//終止鼠標(biāo)松開事件 } } } </script> </html>
相關(guān)文章
JS實現(xiàn)的定時器展示簡單秒表、頁面彈框及跳轉(zhuǎn)操作完整示例
這篇文章主要介紹了JS實現(xiàn)的定時器展示簡單秒表、頁面彈框及跳轉(zhuǎn)操作,結(jié)合實例形式分析了JavaScript時間函數(shù)及頁面元素動態(tài)操作相關(guān)使用技巧,需要的朋友可以參考下2020-01-01javascript中的offsetWidth、clientWidth、innerWidth及相關(guān)屬性方法
這篇文章主要介紹了javascript中的offsetWidth、clientWidth、innerWidth及相關(guān)屬性方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05js 數(shù)字、字符串、布爾值的轉(zhuǎn)換方法(必看)
下面小編就為大家?guī)硪黄猨s 數(shù)字、字符串、布爾值的轉(zhuǎn)換方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04Js判斷參數(shù)(String,Array,Object)是否為undefined或者值為空
在一些前端控件要提交數(shù)據(jù)到服務(wù)器端的數(shù)據(jù)驗證過程中,需要判斷提交的數(shù)據(jù)是否為空。如果是普通表單的字符串?dāng)?shù)據(jù),只需要在 trim 后判斷 length 即可,而這里需要的數(shù)據(jù)可以是各種不同的類型,通過 JSON.stringify(data) 進行序列化后再傳遞2013-11-11