js实现类似iphone的网页滑屏解锁功能示例【附源码

iphone 的出现,打破了人们的用户体验,这一用户体验也延伸到了网页设计上。最近看到很多blog的评论都用类似iphone滑动解锁的方式实现。只有滑动解锁之后才能评论,或者做其他的事情。这个功能的实现,其实并不麻烦,关键是要有好的美工,做出好的滑动图片,然后javascript配合CSS就可以完成,我在这里也简单实现了一个,基本功能如下

1. 打开页面时隐藏评论框,你可以做成disable形式,下载源码后可以修改。
2. 滑动解锁图片,显示评论框,你可以做成让textarea字段enable方式。
3. 采用原生javascript实现,兼容ie,firefox,chrome,safari.

效果图基本如下:

js实现类似iphone的网页滑屏解锁功能示例【附源码

js实现类似iphone的网页滑屏解锁功能示例【附源码

你可以改动部分源代码测试,加入你自己想要的逻辑。

源代码贴在下面,你也可以在文章的最后下载:

<!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 = "请输入评论内容"); 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 = "请输入评论内容"; 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> <div><span></span></div> <div> <textarea rows=5></textarea> </div> </div> </body> </html>

源码点击此处本站下载

更多关于jQuery相关内容感兴趣的读者可查看本站专题:《jQuery页面元素操作技巧汇总》、《jQuery常见事件用法与技巧总结》、《jQuery常用插件及用法总结》、《jQuery扩展技巧总结》及《jquery选择器用法总结

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/e1c03f270f4b1c9285c7b4083503bbd7.html