老师有几个问题
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no" /> <title>2-7自由编程</title> <style> body{ background-color: pink; height: 2000px; } .box{ position: fixed; top: 20px; left: 20px; width: 45px; height: 45px; background-color: firebrick; } </style> </head> <body> <div class="box" id="box"></div> <script> var box = document.getElementById('box'); function drag(el,options){ options.x = typeof options.x !== 'undefined' ? options.x : true; options.y = typeof options.y !== 'undefined' ? options.y : false; if(!options.x && !options.y) return; var curPoint = { x:0, y:0 }; var startPoint = {}; var isTouchMove = false; el.addEventListener('touchstart',handleStart,false); el.addEventListener('touchmove',handleMove,false); el.addEventListener('touchend',handleEnd,false); function handleStart(ev){ var touch = ev.changedTouches[0]; startPoint.x = touch.pageX; startPoint.y = touch.pageY; } function handleMove(ev){ ev.preventDefault(); isTouchMove = true; var touch = ev.changedTouches[0]; var diffPoint = {}; var movePoint = { x:0, y:0 }; diffPoint.x = touch.pageX - startPoint.x; diffPoint.y = touch.pageY - startPoint.y; if(options.x){ movePoint.x = diffPoint.x + curPoint.x; } if(options.y){ movePoint.y = diffPoint.y + curPoint.y; } move(el,movePoint.x,movePoint.y); } function handleEnd(ev){ if(!isTouchMove) return; var touch = ev.changedTouches[0]; curPoint.x += touch.pageX - startPoint.x; curPoint.y += touch.pageY - startPoint.y; isTouchMove = false; } var limitWidth = el.parentNode.offsetWidth - el.offsetWidth, limitHeight = el.parentNode.offsetHeight - el.offsetHeight; function move(el,x,y){ if(x<0){ x=0; }else if(x>limitWidth){ x = limitWidth; } if(y<0){ y=0; }else if(y >limitHeight){ y=limitHeight; } /* x = x || 0; y = y || 0; */ el.style.transform = 'translate3d(' +x+ 'px,' +y+ 'px,0)'; } } //调用drag函数 drag(box,{ x:true, y:true }); </script> </body> </html>
Q1:这个超出边界的思路应该是什么。。
Q2:我看问答区同学写的,但是将div移到右侧边界后还是超出边界了,该怎么改
谢谢老师
7
收起
正在回答
3回答
同学你好, 底部边界没有生效可能是因为同学给body设置的高度为2000px, 页面存在滚动条, 需要将页面滚动到底部测试哦
另,老师提供的两种方法, CSS和js是独立的两种方法哦
1.通过CSS实现
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no" /> <title>2-7自由编程</title> <style> body { background-color: pink; height: 2000px; } .box { /* position: fixed; top: 20px; left: 20px; */ width: 45px; height: 45px; background-color: firebrick; } </style> </head> <body> <div class="box" id="box"></div> <script> var box = document.getElementById('box'); function drag(el, options) { options.x = typeof options.x !== 'undefined' ? options.x : true; options.y = typeof options.y !== 'undefined' ? options.y : false; if (!options.x && !options.y) return; var curPoint = { x: 0, y: 0 }; var startPoint = {}; var isTouchMove = false; el.addEventListener('touchstart', handleStart, false); el.addEventListener('touchmove', handleMove, false); el.addEventListener('touchend', handleEnd, false); function handleStart(ev) { var touch = ev.changedTouches[0]; startPoint.x = touch.pageX; startPoint.y = touch.pageY; } function handleMove(ev) { ev.preventDefault(); isTouchMove = true; var touch = ev.changedTouches[0]; var diffPoint = {}; var movePoint = { x: 0, y: 0 }; diffPoint.x = touch.pageX - startPoint.x; diffPoint.y = touch.pageY - startPoint.y; if (options.x) { movePoint.x = diffPoint.x + curPoint.x; } if (options.y) { movePoint.y = diffPoint.y + curPoint.y; } move(el, movePoint.x, movePoint.y); } function handleEnd(ev) { if (!isTouchMove) return; var touch = ev.changedTouches[0]; curPoint.x += touch.pageX - startPoint.x; curPoint.y += touch.pageY - startPoint.y; isTouchMove = false; } var limitWidth = el.parentNode.offsetWidth - el.offsetWidth, limitHeight = el.parentNode.offsetHeight - el.offsetHeight; function move(el, x, y) { if (x < 0) { // x = 0; x = 0 } else if (x > limitWidth) { x = limitWidth; } if (y < 0) { y = 0; } else if (y > limitHeight) { y = limitHeight; } /* x = x || 0; y = y || 0; */ el.style.transform = 'translate3d(' + x + 'px,' + y + 'px,0)'; } } //调用drag函数 drag(box, { x: true, y: true }); </script> </body> </html>
2. 通过js实现, 由于固定定位是参考当前视口的,所以需要修改为绝对定位哦
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no" /> <title>2-7自由编程</title> <style> body { background-color: pink; height: 2000px; } .box { /* position: fixed; */ position: absolute; top: 20px; left: 20px; width: 45px; height: 45px; background-color: firebrick; } </style> </head> <body> <div class="box" id="box"></div> <script> var box = document.getElementById('box'); function drag(el, options) { options.x = typeof options.x !== 'undefined' ? options.x : true; options.y = typeof options.y !== 'undefined' ? options.y : false; if (!options.x && !options.y) return; var curPoint = { x: 0, y: 0 }; var startPoint = {}; var isTouchMove = false; el.addEventListener('touchstart', handleStart, false); el.addEventListener('touchmove', handleMove, false); el.addEventListener('touchend', handleEnd, false); function handleStart(ev) { var touch = ev.changedTouches[0]; startPoint.x = touch.pageX; startPoint.y = touch.pageY; } function handleMove(ev) { ev.preventDefault(); isTouchMove = true; var touch = ev.changedTouches[0]; var diffPoint = {}; var movePoint = { x: 0, y: 0 }; diffPoint.x = touch.pageX - startPoint.x; diffPoint.y = touch.pageY - startPoint.y; if (options.x) { movePoint.x = diffPoint.x + curPoint.x; } if (options.y) { movePoint.y = diffPoint.y + curPoint.y; } move(el, movePoint.x, movePoint.y); } function handleEnd(ev) { if (!isTouchMove) return; var touch = ev.changedTouches[0]; curPoint.x += touch.pageX - startPoint.x; curPoint.y += touch.pageY - startPoint.y; isTouchMove = false; } var limitWidth = el.parentNode.offsetWidth - el.offsetWidth - 20, limitHeight = el.parentNode.offsetHeight - el.offsetHeight - 20; function move(el, x, y) { if (x < 0) { // x = 0; x = 0 } else if (x > limitWidth) { x = limitWidth; } if (y < 0) { y = 0; } else if (y > limitHeight) { y = limitHeight; } /* x = x || 0; y = y || 0; */ el.style.transform = 'translate3d(' + x + 'px,' + y + 'px,0)'; } } //调用drag函数 drag(box, { x: true, y: true }); </script> </body> </html>
同学可以在测试与一下哦
如果帮助到了你, 欢迎采纳!
祝学习愉快~~~~
hyperse
2019-09-24 16:38:11
修改后的代码:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no" /> <title>2-7自由编程</title> <style> body{ background-color: pink; height: 2000px; } .box{ /* position: fixed; top: 20px; left: 20px; */ width: 45px; height: 45px; background-color: firebrick; } </style> </head> <body> <div class="box" id="box"></div> <script> var box = document.getElementById('box'); function drag(el,options){ options.x = typeof options.x !== 'undefined' ? options.x : true; options.y = typeof options.y !== 'undefined' ? options.y : false; if(!options.x && !options.y) return; var curPoint = { x:0, y:0 }; var startPoint = {}; var isTouchMove = false; el.addEventListener('touchstart',handleStart,false); el.addEventListener('touchmove',handleMove,false); el.addEventListener('touchend',handleEnd,false); function handleStart(ev){ var touch = ev.changedTouches[0]; startPoint.x = touch.pageX; startPoint.y = touch.pageY; } function handleMove(ev){ ev.preventDefault(); isTouchMove = true; var touch = ev.changedTouches[0]; var diffPoint = {}; var movePoint = { x:0, y:0 }; diffPoint.x = touch.pageX - startPoint.x; diffPoint.y = touch.pageY - startPoint.y; if(options.x){ movePoint.x = diffPoint.x + curPoint.x-20; } if(options.y){ movePoint.y = diffPoint.y + curPoint.y-20; } move(el,movePoint.x,movePoint.y); } function handleEnd(ev){ if(!isTouchMove) return; var touch = ev.changedTouches[0]; curPoint.x += touch.pageX - startPoint.x; curPoint.y += touch.pageY - startPoint.y; isTouchMove = false; } var limitWidth = el.parentNode.offsetWidth - el.offsetWidth, limitHeight = el.parentNode.offsetHeight - el.offsetHeight; function move(el,x,y){ if(x<0){ x=0; }else if(x>limitWidth){ x = limitWidth; } if(y<0){ y=0; }else if(y >limitHeight){ y=limitHeight; } /* x = x || 0; y = y || 0; */ el.style.transform = 'translate3d(' +x+ 'px,' +y+ 'px,0)'; } } //调用drag函数 drag(box,{ x:true, y:true }); </script> </body> </html>
3.WebAPP开发与小程序
- 参与学习 人
- 提交作业 622 份
- 解答问题 6815 个
微信带火了小程序,也让前端工程师有了更多的展现机会,本阶段带你从移动基础知识的学习到webAPP开发,及小程序开发,让你PC端与移动端两端通吃。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星