javascript —IPhone滑動解鎖javascript–拖動圖片時取消瀏覽器預設提示

來源:互聯網
上載者:User
<script type="text/javascript">    var untilEvent = {        addEvent:function(oElement,oType,fun){            oElement.attachEvent?oElement.attachEvent("on"+oType,fun):oElement.addEventListener(oType,fun,false);        },        removeEvent:function(oElement,oType,fun){            oElement.detachEvent?oElement.detachEvent("on"+oType,fun):oElement.removeEventListener(oType,fun,false);        }    }    window.onload = function(){        var oPar = document.getElementById("iphone");        var oDiv = document.getElementById("lock");            var oSpan = oDiv.getElementsByTagName("span")[0];        var spanW = oSpan.offsetWidth;        var divW =  oDiv.offsetWidth;        var downX = 0;        var moveX = 0;        var movekg = true;                var mDown = function(event){            var ev = event || window.event;            downX = ev.clientX;            untilEvent.addEvent(oSpan,"mousemove",mMove);            untilEvent.addEvent(document,"mouseup",mUp);            if(event.preventDefault){                event.preventDefault();                event.stopPropagation();            }                }                var mMove = function(event){            ev = event || window.event;            if(ev.cancelBubble){                ev.cancelBubble=true;                ev.returnValue = false;            }            moveX = ev.clientX;            var moveLenght = moveX - downX;            if(moveLenght>0){moveLenght = Math.min(moveLenght,divW-spanW)}else{                moveLenght = 0;            }            oSpan.style.left = moveLenght+"px";        }                var mUp  = function(){            untilEvent.removeEvent(oSpan,"mousemove",mMove);            untilEvent.removeEvent(document,"mouseup",mUp);            if(oSpan.offsetLeft>=(divW-spanW)){                oPar.style.background = "url('http://fgm.cc/iphone/2.jpg')";                oSpan.style.background = "none";            }            var dirct = oSpan.offsetLeft>(divW/2-spanW+20)?true:false;            var setInt = setInterval(function(){                if(dirct){                    oSpan.style.left = Math.min(divW-spanW,(oSpan.offsetLeft + 15))+"px";                    if(oSpan.offsetLeft >= (divW-spanW)){                        oPar.style.background = "url('http://fgm.cc/iphone/2.jpg')";                        oSpan.style.background = "none";                        clearInterval(setInt);                    }                }else{                    oSpan.style.left = Math.max(0,(oSpan.offsetLeft - 15))+"px";                    oSpan.offsetLeft <= 0 && clearInterval(setInt);                }            },30)                    }        untilEvent.addEvent(oSpan,"mousedown",mDown);    }</script>

 

一、實現功能

1.在滑塊移動到中間時向左自動,滑到起始點

2.當滑塊超過中間時,向右自動,滑到終點,並解鎖

3.滑鼠拖動滑塊到終點解鎖

 

二、注意點

1.去掉滑鼠拖動時的預設事件

可以參看我部落格裡面隨筆

javascript--拖動圖片時取消瀏覽器預設提示

  2.控制滑塊的地區,可以用Math.MAx或Marh.Min 3.在使用Setinterval時,注意區別兩種不同情況 原效果地址:http://js.fgm.cc/learn/iphone-unlock/ 我的DEMO下載 2.控制滑塊的地區,可以用Math.MAx或Marh.Min
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.