溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

js實現(xiàn)類似iphone的網(wǎng)頁滑屏解鎖功能示例【附源碼下載】

發(fā)布時間:2020-09-18 09:04:58 來源:腳本之家 閱讀:154 作者:輕舞肥羊 欄目:web開發(fā)

本文實例講述了js實現(xiàn)類似iphone的網(wǎng)頁滑屏解鎖功能。分享給大家供大家參考,具體如下:

iphone 的出現(xiàn),打破了人們的用戶體驗,這一用戶體驗也延伸到了網(wǎng)頁設(shè)計上。最近看到很多blog的評論都用類似iphone滑動解鎖的方式實現(xiàn)。只有滑動解鎖之后才能評論,或者做其他的事情。這個功能的實現(xiàn),其實并不麻煩,關(guān)鍵是要有好的美工,做出好的滑動圖片,然后javascript配合CSS就可以完成,我在這里也簡單實現(xiàn)了一個,基本功能如下

1. 打開頁面時隱藏評論框,你可以做成disable形式,下載源碼后可以修改。
2. 滑動解鎖圖片,顯示評論框,你可以做成讓textarea字段enable方式。
3. 采用原生javascript實現(xiàn),兼容ie,firefox,chrome,safari.

效果圖基本如下:

js實現(xiàn)類似iphone的網(wǎng)頁滑屏解鎖功能示例【附源碼下載】

js實現(xiàn)類似iphone的網(wǎng)頁滑屏解鎖功能示例【附源碼下載】

你可以改動部分源代碼測試,加入你自己想要的邏輯。

源代碼貼在下面,你也可以在文章的最后下載:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>yihaomen.com js滑屏解鎖</title>
<style type="text/css"> 
#slider_comment{position:relative;width:426px;height:640px;margin:10px auto;}
#lock{width:200px;height:30px;border:1px dashed #ccc;line-height:30px;}
#lock span{position:absolute;width:45px;height:30px;cursor:pointer;background:url(img/arrow.png) no-repeat;}
</style>
<script type="text/javascript"> 
window.onload = function ()
{
  var slider_comment = document.getElementById("slider_comment");
  var oLock = document.getElementById("lock");
  var oBtn = oLock.getElementsByTagName("span")[0];
  var comment=document.getElementById('comment');
  var disX = 0;
  var maxL = oLock.clientWidth - oBtn.offsetWidth;  
  oBtn.onmousedown = function (e)
  {
    var e = e || window.event;
    disX = e.clientX - this.offsetLeft;
    document.onmousemove = function (e)
    {
      var e = e || window.event;
      var l = e.clientX - disX;
      l < 0 && (l = 0);
      l > maxL && (l = maxL);      
      oBtn.style.left = l + "px";      
      oBtn.offsetLeft == maxL && (comment.style.display="block",oLock.innerHTML = "請輸入評論內(nèi)容");
      return false;
    };
    document.onmouseup = function ()
    {
      document.onmousemove = null;
      document.onmouseup = null;
      oBtn.releaseCapture && oBtn.releaseCapture();
      oBtn.offsetLeft > maxL / 2 ?
        startMove(maxL, function ()
        {
          comment.style.display="block";
          oLock.innerHTML = "請輸入評論內(nèi)容";
          oLock.style.display = "block";
        }) :
        startMove(0)
    };
    this.setCapture && this.setCapture();
    return false
  };
  function startMove (iTarget, onEnd)
  {
    clearInterval(oBtn.timer);
    oBtn.timer = setInterval(function ()
    {
      doMove(iTarget, onEnd)
    }, 30)
  }
  function doMove (iTarget, onEnd)
  {
    var iSpeed = (iTarget - oBtn.offsetLeft) / 5;
    iSpeed = iSpeed > 0 ? Math.ceil(iSpeed) : Math.floor(iSpeed);
    iTarget == oBtn.offsetLeft ? (clearInterval(oBtn.timer), onEnd && onEnd()) : oBtn.style.left = iSpeed + oBtn.offsetLeft + "px"
  }
};
</script>
</head>
<body>
<div id="slider_comment">
<div id="lock"><span></span></div>
<div id="comment" >
  <textarea id="comment_text" rows=5 ></textarea>
</div>
</div>
</body>
</html>

源碼點擊此處本站下載。

更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery頁面元素操作技巧匯總》、《jQuery常見事件用法與技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery擴展技巧總結(jié)》及《jquery選擇器用法總結(jié)》

希望本文所述對大家jQuery程序設(shè)計有所幫助。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI