如何去寫小米手機系統中的按鈕

來源:互聯網
上載者:User

標籤:art   odi   添加   自己   png   nbsp   title   技術   code   

首先貼出兩張圖:

 

今天分享的就是小米手機系統中按鈕案例,本人是初學者,所以有些代碼寫的並不十分的準確,相容性方面也沒有調試,重在記錄和分享思路,請各路大神多多指點[抱拳]

HTML部分:

<body>    <div id="container">        <div id="btn"></div>    </div></body>

CSS部分:

#container {    position: relative;    left: 200px;    top: 100px;    border: 1px solid #c2c2c2;    width: 66px;    height: 26px;    border-radius: 50px 50px 50px 50px;    padding: 4px;}#btn         {            width: 26px;            height: 26px;            border-radius: 26px;            background: #ededed;    margin-left:0px;        }        

 JS部分:

window.onload=function(){                var oDiv=document.getElementById("container");                var oBtn=document.getElementById("btn");                oDiv.onclick=function(){                    var oBtnMarginLeft=parseInt(getStyle(oBtn,"marginLeft"));                    console.log("oBtnMarginLeft="+oBtnMarginLeft);                    if(oBtnMarginLeft>0){                        startMove(oBtn,"marginLeft",0,function(){                            oBtn.style.backgroundColor="#ededed";                        });                    }else{                        startMove(oBtn,"marginLeft",38,function(){                            oBtn.style.backgroundColor="#33b4ff";                        });                    }                }            }            function startMove(obj,attr,iTarget,fn){                clearInterval(obj.timer);                obj.timer=setInterval(function(){                    var iCur=parseInt(getStyle(obj,attr));                    var iSpeed=(iTarget-iCur)/3;                    iSpeed=iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);                    document.title=iCur;                    if(iCur==iTarget){                        clearInterval(obj.timer);//                        console.log(getStyle(obj,"left"));                        if(fn)fn();                    }else{                        iCur+=iSpeed;                        obj.style[attr]=iCur+"px";                    }                },30);            }            function getStyle(obj,attr){                if(obj.currentStyle){                    return obj.currentStyle[attr];                }else{                    return getComputedStyle(obj,false)[attr];                }            }

思路講解:

  1、布局其實就非常簡單了,我採用的是兩個div嵌套,一個是按鈕外部的容器,一個是內部運動的物體

 

  2、在這裡需要強調的是,初學者可能會遇到的一個問題,就是當一個物體需要運動的時候,要給元素添加定位,而在本案例中,我是讓內嵌元素的margin值在變動,所以不需要給元素進行定位。

  3、另外在js中,自己寫了一個簡單的運動架構,是一個緩衝運動,讓按鈕在使用的過程中更加的友好。

如果各位網友有什麼好的建議以及更好的解決方案,歡迎留言提建議。

 

如何去寫小米手機系統中的按鈕

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.