cocos 构建出来的web包里,index.html 文件里添加 <script src="fanhui.js" charset="utf-8"></script> //(调用以下脚本)
新建脚本 fanhui_url.js
var isClick = true;//??????????????¼?
var styleElement = document.createElement("style");
styleElement.innerHTML = ‘#payRetBtnDiv {‘ +
‘ display: none;‘ +
‘ position: fixed;‘ +
‘ width: 35px;‘ +
‘ height: 35px;‘ +
//‘ background: url(fanhui_btn.24180.png)??‘ +
‘ background-color: rgba(0, 0, 0, 0.2);‘ +
‘ z-index: 10000;‘ +
‘ cursor: pointer;‘ +
‘ top: 0px;‘ +
‘ left: 0px;‘ +
‘ border-radius: 30%;‘ +
‘ padding: 6px;‘ +
‘ }‘ +
‘ ‘ +
‘ #payRetBtnDiv>div {‘ +
‘ border-radius: 50%;‘ +
‘ width: 100%;‘ +
‘ height: 100%;‘ +
//‘ background: url(c:/fanhui_btn.24180.png)??‘ +
//‘ 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;‘ +
‘ }‘ +
‘ #payRetBtnDiv:hover>div{‘ +
‘ background-color: rgba(0, 0, 0, 0.6);‘ +
//‘ background: url(fanhui_btn.24180.png)??‘ +
‘ } ‘;
document.body.appendChild(styleElement);
var new_element_N = document.createElement(‘div‘);
new_element_N.setAttribute("id", "payRetBtnDiv");
new_element_N.style.top = "100px";
new_element_N.style.left = "100px";
new_element_N.innerHTML = ‘ <div style="background:url(fanhui_btn.24180.png);background-size:cover"></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("payRetBtnDiv");
fdiv.onmousedown = function(e)
{
isClick = true;
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)
{
isClick = false;
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("?????????????????");
}
fdiv.onclick = function() { //????¼?????????????????????????????¼????
if (isClick == true) {
cc.find(‘Canvas‘).getComponent(‘dating_chongzhi‘).HideWebView();
}
}
原文:https://www.cnblogs.com/luorende/p/13072983.html