Javascript实现苹果悬浮虚拟按钮
作者:bea
Javascript实现苹果悬浮虚拟按钮 直接引入代码到页面即可 代码有部分冗余的地方,有兴趣的小伙伴可也自己修改 如果有什么BUG 记得评论 告诉我哦 web-touch.js var new_element_N=document.createElement("style"); new_element_N.innerHTML = '#drager {' + ' position: fixed;' + ' width: 35px;' + '
Javascript实现苹果悬浮虚拟按钮
直接引入代码到页面即可 代码有部分冗余的地方,有兴趣的小伙伴可也自己修改 如果有什么BUG 记得评论 告诉我哦
web-touch.js
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 = ' <div></div>' ;
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('touchstart', fdiv.onmousedown, false);
fdiv.addEventListener('touchmove', 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('touchend', document.onmouseup , false);
fdiv.ondblclick=function(){//双击事件可能在手机端浏览器会与网页缩放事件冲突
alert("发挥你们的想象力吧");
}
html
<!doctype html>
<html >
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
</body>
<script src="web-touch.js" type="text/javascript"></script>
</html>
演示图
有用 | 无用
直接引入代码到页面即可 代码有部分冗余的地方,有兴趣的小伙伴可也自己修改 如果有什么BUG 记得评论 告诉我哦
web-touch.js
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 = ' <div></div>' ;
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('touchstart', fdiv.onmousedown, false);
fdiv.addEventListener('touchmove', 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('touchend', document.onmouseup , false);
fdiv.ondblclick=function(){//双击事件可能在手机端浏览器会与网页缩放事件冲突
alert("发挥你们的想象力吧");
}
html
<!doctype html>
<html >
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
</body>
<script src="web-touch.js" type="text/javascript"></script>
</html>
演示图
有用 | 无用
猜你喜欢
您可能感兴趣的文章:
- 学习使用bootstrap3栅格系统
- 分享两段简单的JS代码防止SQL注入
- NodeJS创建基础应用并应用模板引擎
- DIV随滚动条滚动而滚动的实现代码【推荐】
- 原生js实现百叶窗效果及原理介绍
- CascadeView级联组件实现思路详解(分离思想和单链表)
- jquery中实现时间戳与日期相互转换
- Angular.js与Bootstrap相结合实现表格分页代码
- JS代码防止SQL注入的方法(超简单)
- zTree插件下拉树使用入门教程
- 对jquery的ajax进行二次封装以及ajax缓存代理组件:AjaxCache详解
- js创建jsonArray传输至后台及后台全面解析
- javascript HTML5 Canvas实现圆盘抽奖功能
- 详解JavaScript的另类写法
- 详解jQuery中的empty、remove和detach
- JQuery导航菜单选择特效
- JavaScript实现图片自动加载的瀑布流效果
- javascript冒泡排序小结
- javascript原生ajax写法分享