登录  /  注册
首页 > web前端 > js教程 > 正文

Javascript制作仿苹果触摸屏效果

php中世界最好的语言
发布: 2018-03-08 11:44:35
原创
2331人浏览过

这次给大家带来javascript制作仿苹果触摸屏效果,javascript制作仿苹果触摸屏效果的注意事项有哪些,下面就是实战案例,一起来看一下。

Javascript制作仿苹果小白点
直接引入代码到页面即可
代码有部分冗余的地方,有兴趣的小伙伴可也自己修改
如果有什么BUG 记得评论 告诉我哦

var new_element_N=document.createElement("style");
    new_element_N.innerHTML = '#drager {' +
        '     position: fixed;' +
        '     width: 35px;' +
        '     height: 35px;' +
        '     background-color: rgba(0, 0, 0, 0.2);' +
        '     z-index: 10000;' +
        '     cursor: pointer;' +
        '     top: 0px;' +
        '     left: 0px;' +
        '     border-radius: 30%;' +
        '     padding: 6px;' +
        ' }' +
        ' ' +
        ' #drager>div {' +
        '     border-radius: 50%;' +
        '     width: 100%;' +
        '     height: 100%;' +
        '     background-color: rgba(0, 0, 0, 0.3);' +
        '      transition: all 0.2s;' +
        '   -webkit-transition: all 0.2s;' +
        '   -moz-transition: all 0.2s;' +
        '   -o-transition: all 0.2s;' +
        ' }' +
        ' #drager:hover>div{' +
       '      background-color: rgba(0, 0, 0, 0.6);' +
       ' } ';
    document.body.appendChild(new_element_N);
    new_element_N=document.createElement('div');
    new_element_N.setAttribute("id","drager");
    new_element_N.style.top="100px";
    new_element_N.style.left="100px";
    new_element_N.innerHTML = &#39; <div></div>&#39; ;
    document.body.appendChild(new_element_N);
    //
    //
        var posX;
        var posY;     
        var screenWidth =document.documentElement.clientWidth;
        var screenHeight = document.documentElement.clientHeight;  
        var fdiv = document.getElementById("drager"); 
        fdiv.onmousedown=function(e)
        {
            screenWidth =document.documentElement.clientWidth;
            screenHeight = document.documentElement.clientHeight;  
            if(!e){ e = window.event; } //IE
            posX = e.clientX - parseInt(fdiv.style.left);
            posY = e.clientY - parseInt(fdiv.style.top);
            document.onmousemove = mousemove;          
        }
        document.onmouseup = function()//释放时自动贴到最近位置
        {
            document.onmousemove = null;
            if((parseInt(fdiv.style.top)+parseInt(fdiv.clientHeight)/2)<=(screenHeight/2)){//在上半部分
                if((parseInt(fdiv.style.left)+parseInt(fdiv.clientWidth)/2)<=(screenWidth/2)){//在左半部分
                    if((parseInt(fdiv.style.top)+parseInt(fdiv.clientHeight)/2)<=(parseInt(fdiv.style.left)+parseInt(fdiv.clientWidth)/2)){//靠近上方
                        fdiv.style.top="0px";
                    }else{//靠近左边
                        fdiv.style.left="0px";
                    }
                }else{//在右半部分
                    if((parseInt(fdiv.style.top)+parseInt(fdiv.clientHeight)/2)<=(screenWidth-(parseInt(fdiv.style.left)+parseInt(fdiv.clientWidth)/2)) ){//靠近上方
                        fdiv.style.top="0px";
                    }else{//靠近右边
                        fdiv.style.left=(screenWidth-parseInt(fdiv.clientWidth))+"px";
                    }
                }
            }else{ //下半部分
                 if((parseInt(fdiv.style.left)+parseInt(fdiv.clientWidth)/2)<=(screenWidth/2)){//在左半部分
                    if( (screenHeight-(parseInt(fdiv.style.top)+parseInt(fdiv.clientHeight)/2))<=(parseInt(fdiv.style.left)+parseInt(fdiv.clientWidth)/2)){//靠近下方
                        fdiv.style.top=(screenHeight-parseInt(fdiv.clientHeight))+"px";
                    }else{//靠近左边
                        fdiv.style.left="0px";
                    }
                }else{//在右半部分
                    if( (screenHeight-(parseInt(fdiv.style.top)+parseInt(fdiv.clientHeight)/2))<=(screenWidth-(parseInt(fdiv.style.left)+parseInt(fdiv.clientWidth)/2)) ){//靠近上方
                        fdiv.style.top=(screenHeight-parseInt(fdiv.clientHeight))+"px";
                    }else{//靠近右边
                        fdiv.style.left=(screenWidth-parseInt(fdiv.clientWidth))+"px";
                    }
                }
            }
        }
        function mousemove(ev)
        {
            if(ev==null){ ev = window.event;}//IE
            if((ev.clientY - posY)<=0){//超过顶部
                 fdiv.style.top="0px";
            }else if((ev.clientY - posY) >(screenHeight-parseInt(fdiv.clientHeight))){//超过底部
                fdiv.style.top=(screenHeight-parseInt(fdiv.clientHeight))+"px";
            }else{
                fdiv.style.top = (ev.clientY - posY) + "px";
            }
             
             if((ev.clientX- posX)<=0){//超过左边
                 fdiv.style.left="0px";
            }else if((ev.clientX - posX) >(screenWidth-parseInt(fdiv.clientWidth))){//超过右边
                fdiv.style.left=(screenWidth-parseInt(fdiv.clientWidth))+"px";
            }else{
                fdiv.style.left = (ev.clientX - posX) + "px";
            }
            // console.log( posX +"  "+ fdiv.style.left);
             
        }
       window.onload =  window.onresize = function() { //窗口大小改变事件
            screenWidth =document.documentElement.clientWidth;
            screenHeight = document.documentElement.clientHeight;  
            if( (parseInt(fdiv.style.top)+parseInt(fdiv.clientHeight))>screenHeight){//窗口改变适应超出的部分
                 fdiv.style.top=(screenHeight-parseInt(fdiv.clientHeight))+"px";
            }   
            if( (parseInt(fdiv.style.left)+parseInt(fdiv.clientWidth))>screenWidth){//窗口改变适应超出的部分
                 fdiv.style.left=(screenWidth-parseInt(fdiv.clientWidth))+"px";
            }   
            document.onmouseup.apply()
        };
        fdiv.addEventListener(&#39;touchstart&#39;,  fdiv.onmousedown, false);  
        fdiv.addEventListener(&#39;touchmove&#39;, function(event) {
                        // 如果这个元素的位置内只有一个手指的话
                        if (event.targetTouches.length == 1) {
                         event.preventDefault();// 阻止浏览器默认事件,重要 
                            var touch = event.targetTouches[0];
                            if((touch.pageY)<=0){//超过顶部
                                fdiv.style.top="0px";
                            }else if(touch.pageY>(screenHeight-parseInt(fdiv.clientHeight))){//超过底部
                                fdiv.style.top=(screenHeight-parseInt(fdiv.clientHeight))+"px";
                            }else{
                                fdiv.style.top = (touch.pageY-parseInt(fdiv.clientHeight)/2) + "px";
                            }
                             
                            if(touch.pageX<=0){//超过左边
                                fdiv.style.left="0px";
                            }else if( touch.pageX >(screenWidth-parseInt(fdiv.clientWidth))){//超过右边
                                fdiv.style.left=(screenWidth-parseInt(fdiv.clientWidth))+"px";
                            }else{
                                fdiv.style.left = (touch.pageX-parseInt(fdiv.clientWidth)/2) + "px";
                            }
                       }
                    }, false);
        fdiv.addEventListener(&#39;touchend&#39;, document.onmouseup , false);            
        fdiv.ondblclick=function(){//双击事件可能在手机端浏览器会与网页缩放事件冲突
            alert("发挥你们的想象力吧");
        }
登录后复制

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

相关阅读:

 简易的图片点击上传功能

JS可以截取video的标签视频缩略图吗?

以上就是Javascript制作仿苹果触摸屏效果的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号