滑动出菜单

时间:2018-12-21 09:41:20
【文件属性】:

文件名称:滑动出菜单

文件大小:5KB

文件格式:HTML

更新时间:2018-12-21 09:41:20

滑动 菜单

使用js,当鼠标滑动,滑出隐藏菜单 //通过参数获取class或者id 对象 var window_width = document.body.scrollWidth; var window_height = document.body.scrollHeight; var g=function(id){ if(id.substr(0,1)=='.') { return document.getElementsByClassName(id.substr(1)); } return document.getElementById(id); } var isShow = false; var showNav= function(left_val){ setTimeout(function a(){ if(left_val < 0){ g("nav").style.left=left_val +"px"; left_val += 5; a(); } },200); } var hiddNav = function(left_val){ setTimeout(function b(){ if(left_val > Math.min(window_width,window_height)/-2){ g("nav").style.left=left_val +"px"; left_val -= 5; b(); // hiddNav(left_val); } },200); } function a(){ g("nav").style.width = Math.min(window_width,window_height)/2 +"px"; g("nav").style.left = Math.min(window_width,window_height)/-2 +"px"; } a(); var glideNav = function (glide){ var nav = function(){ if(!isShow){ // g("nav").className = "nav nav_action"; var left_val = Math.min(window_width,window_height)/-2; showNav(left_val); isShow = true; } } var navHidd = function(){ if(isShow){ // g("nav").className = "nav"; var left_val = 0; hiddNav(left_val); isShow = false; } } if(glide){ nav(); }else{ navHidd(); } }; var obj = { state:{ mouseX:0, mouseY:0 }, move:{ moveX:0, moveY:0 } } document.addEventListener("touchstart",function(e){ e.stopImmediatePropagation(); obj.state.dragable = true; obj.state.mouseX = e.targetTouches[0].pageX; obj.state.mouseY = e.targetTouches[0].pageY; }); document.addEventListener("touchmove", function(e){ e.stopImmediatePropagation(); var x = e.targetTouches[0].pageX - obj.state.mouseX; var y = e.targetTouches[0].pageY - obj.state.mouseY; if(x>10){ glideNav(true); }else if(x<10){ glideNav(false); } }); document.addEventListener("mousedown",function(e){ e.stopImmediatePropagation(); obj.state.dragable = true; obj.state.mouseX = e.screenX; obj.state.mouseY = e.screenY; }); document.addEventListener("mouseup", function(e){ e.stopImmediatePropagation(); var x = e.screenX - obj.state.mouseX; var y = e.screenY - obj.state.mouseY; if(x>10){ glideNav(true); }else if(x<10){ glideNav(false); } });


网友评论