每天学习一点点---------------------------------------------------移动端的那些小东西
1.首先用的模块加载时seaJS
define(function(require, exports, module) {})-------------------------加载模块。
module.exports = {
AssistiveTouch: AssistiveTouch
};----------------------------------------------暴露模块,方便调用。
2、我们需要获取点击 || 触摸位置(兼容)
function mousePosition(ev){
if(ev.targetTouches&&ev.targetTouches[0]){
return {x:ev.targetTouches[0].pageX, y:ev.targetTouches[0].pageY};
}
if(ev.touches&&ev.touches[0]){
return {x:ev.touches[0].pageX, y:ev.touches[0].pageY};
}
if(ev.changedTouches&&ev.changedTouches[0]){
return {x:ev.changedTouches[0].pageX, y:ev.changedTouches[0].pageY};
}
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
3.控件对象本身
function AssistiveTouch(sets,clickFunc){
var atClass=(sets.atClass)?sets.atClass:“”,
imgUrl=(sets.imgUrl)?sets.imgUrl:", //图片地址
closeBtn=(sets.closeBtn)?sets.closeBtn:false; // 图片按钮
clipH=(sets.clipH)?sets.clipH:0; //切割的高度
clipW = (sets.clipW)?sets.clipW:0, //切割宽度
autoSide=(sets.autoSide)?sets.autoSide:false; //自动停靠
$(atClass).css("position","fixed");
$(atClass).css("z-index","100");
$(atClass).append('<div class="assistive-touch-content"></div>'); ----添加样式
if(imgUrl){ $(".assistive-touch-content").css("background-image","url('"+imgUrl+"')");}
if(closeBtn){
$(atClass).append('<div class="assistive-touch-close-btn">×</div>');
}
$(".assistive-touch-close-btn").on(" click mouseup touchend",function(e){ //防止点击事件刺穿
window.event? window.event.cancelBubble = true : e.stopPropagation();
$(atClass).hide();
}); ----------------------------------------------------点击关闭
$(atClass).on("mousedown touchstart",function(e){
var e=e||window.event;
e.preventDefault();
var mousePos=mousePosition(e);
$(this).attr("moving","true");
$(this).attr("offset-left",mousePos.x-$(this)[0].offsetLeft);
$(this).attr("offset-top",mousePos.y-$(this)[0].offsetTop);
$(this).attr("origin-left",mousePos.x);
$(this).attr("origin-top",mousePos.y);
})-------------------------------------------------开始触摸的位置
$(atClass).on("mouseup touchend",function(e){
if($(this).attr("moving")!="true")return;
var e=e||window.event;
e.preventDefault();
var mousePos=mousePosition(e);
$(this).[0].style.left = (mousePos.x-$(this).attr("offset-left"))+"px";
$(this)[0].style.top = (mousePos.y-$(this).attr("offset-top"))+"px";
})-------------------------------------------------触摸中的位置
$(atClass).on("mouseup touchend",function(e){
var e=e||window.event;
e.preventDefault();
var windowHeight = document.documentElement.clientHeight||document.body.clientHeight;
var windowWidth = document.documentElement.offsetWidth||document.body.offsetWidth;
windowHeight += clipH;
windowWidth += clipW;
$(this).attr("moving","false");
var mousePos = mousePosition(e);
$(this)[0].style.top = (mousePos.y-$(this).attr("offset-top"))+"px";
if(Math.abs(mousePos.x-$(this).attr("origin-left"))<=3||Math.abs(mousePos.y-$(this).attr("origin-top"))<=3){
$(this)[0].style.left = ($(this).attr("origin-left")-$(this).attr("offset-left"))+"px";
$(this)[0].style.top = ($(this).attr("origin-top")-$(this).attr("offset-top"))+"px";
$(this).click(function(e){
clickFunc($(".assistive-touch-content")[0],$(this)[0]);
});
$(this).click();
return;
}
$(".assistive-touch-close-btn").removeClass("right").removeClass('top');
function sideOverflowFunc(t) {
if(t[0].offsetLeft<0){
t[0].style.left = 0;
}
if((t[0].offsetTop+t[0].offsetHeight)>windowHeight){
t[0].style.top = (windowHeight - t[0].offsetHeight)+"px";
}
if((t[0].offsetLeft+t[0].offsetWidth)>windowWidth){
t[0].style.left = (windowWidth - t[0].offsetWidth)+"px";
$(".assistive-touch-close-btn").addClass("right");
}
}--------------------------------------------------------------------临界值处理
function autoSideFunc(t){
if (!autoSide)return;
if(t[0].offsetTop<windowHeight/10){
t.animate({top:"0px"},200);
$(".assistive-touch-close-btn").addClass("top");
return;
}else if(t[0].offsetTop+t[0].offsetHeight>windowHeight/10*9){
t.animate({top:(windowHeight-t[0].offsetHeight)+"px"},200);
return;
}
if(t[0].offsetLeft+(t[0].offsetWidth/2)<=windowWidth/2){
t.animate({left:"0px"},200);
}else{
t.animate({left:(windowWidth-t[0].offsetWidth)+"px"},200);
$(".assistive-touch-close-btn").addClass("right");
}
}--------------------------------------------------------------临界靠边的动画
sideOverflowFunc($(this));
autoSideFunc($(this));
})------------------------------------------------------------------触摸结束的处理的位置
return $(atClass);--------------------------------------------------------返回对象
}
4.调用:
assTouch.AssistiveTouch({
atClass:".js_drap_img",
imgUrl:config.baseloc+"images/index/fenqi2_icon.png",
closeBtn:false,
autoSide:true,
clipH:-50
},function(content,self){
com.jumpUrl("lend/frontstage.html?productId=1&footer=1&productType=2",config.USERCERTIFICATION);
});