5 Star 1 Fork 0

wangzhiweik/shop-system

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test.html 3.35 KB
一键复制 编辑 原始数据 按行查看 历史
zhiweiwang6 提交于 2017-06-19 10:36 . edit test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<style type="text/css">
html,body{ position: absolute; width: 100%; height: 100%; margin: 0px; padding: 0px;}
#div1{ width: 100%; height: 100%; position: relative; overflow: hidden;}
#img-box{width: 800px; height: 800px; position: absolute; left: -100px; top: -100px;}
#div1 img{ position: absolute; left: 0; top: 0; height: 800px; }
/*#div1 img{ position: absolute; left: 0; top: 0; }*/
</style>
</head>
<body>
<div id="div1">
<div id="img-box">
<img src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1497607058528&di=2816848e9283fa7513bce6c0dbe69c70&imgtype=0&src=http%3A%2F%2Fimg.sj33.cn%2Fuploads%2Fallimg%2F201210%2F2012101121161762.jpg" alt="" id="drag">
<div id="pos1" style="display: none; position: absolute; left: 200px; top: 200px; background: #fff;">
1111
</div>
</div>
</div>
<script type="text/javascript">
var drag = document.getElementById('img-box');
var pos1 = document.getElementById('pos1');
var startX = 0;
var startY = 0;
var offsetLeft = 0;
var offsetTop = 0;
var left = 0;
var top1 = 0;
//touchstart事件
function touchSatrtFunc(evt) {
//evt.preventDefault(); //阻止触摸时浏览器的缩放、滚动条滚动等
var touch = evt.touches[0]; //获取第一个触点
var x = Number(touch.pageX); //页面触点X坐标
var y = Number(touch.pageY); //页面触点Y坐标
//记录触点初始位置
startX = x;
startY = y;
offsetLeft = drag.offsetLeft;
offsetTop = drag.offsetTop;
}
//touchmove事件
function touchMoveFunc(evt) {
//evt.preventDefault(); //阻止触摸时浏览器的缩放、滚动条滚动等
var touch = evt.touches[0]; //获取第一个触点
var x = Number(touch.pageX); //页面触点X坐标
var y = Number(touch.pageY); //页面触点Y坐标
left = offsetLeft+(x - startX);
top1 = offsetTop+(y - startY);
if(left>0){
left=0;
}
else if(left <window.innerWidth-drag.offsetWidth){
left = window.innerWidth-drag.offsetWidth;
}
console.log(offsetTop+(y - startY));
console.log(window.innerHeight-drag.offsetHeight);
if(top1>0){
top1=0;
}else if(top1 <window.innerHeight-drag.offsetHeight){
console.log(1)
top1 = window.innerHeight-drag.offsetHeight;
}
// console.log(top);
if(left >-400 && left <= -100){
pos1.style.display = 'block';
}
else{
pos1.style.display = 'none';
}
if (x - startX != 0) {
//左右滑动
drag.style.left = left+'px';
}
if (y - startY != 0) {
//上下滑动
drag.style.top = top1+'px';
}
}
drag.addEventListener("touchstart", touchSatrtFunc);
drag.addEventListener("touchmove", touchMoveFunc);
drag.addEventListener("touchend", function(){
});
var eventFn = function (event) {
event.preventDefault();
}
document.body.addEventListener('touchmove', eventFn,false);
document.body.removeEventListener('touchend', eventFn,false);
</script>
</body>
</html>
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/jason_wangzhiwei/shop-system.git
git@gitee.com:jason_wangzhiwei/shop-system.git
jason_wangzhiwei
shop-system
shop-system
master

搜索帮助