我放弃了,这边界太难弄了

我放弃了,这边界太难弄了

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

    <style>

        .box{width: 50px;height: 50px;background-color: red;margin: 20px auto;position: absolute;}

        .background{width: 100%;height: 2000px;background-color: pink;position: relative;}

    </style>

</head>

<body>

    <div class="background">

        <div class="box" id="box"></div>

    </div>

    <script>

        var drag = function(el,direction){

            if(direction.x !== undefined && direction.y !== undefined){

                if(!direction.x || !direction.y){

                    return;

                }else{

                    var curpoint = {x:0,y:0};

                    var startpoint = {x:0,y:0}

                    

                    function move(el,transl){

                        x = transl.x || 0;

                        y = transl.y || 0;

                        el.style.left = x+'px';

                        el.style.top = y+'px';

                    }

                    function Limit(el,transl){

                       if(el.offsetLeft<=0||(el.offsetLeft+el.offsetWidth)>=window.innerWidth){transl.x=0}

                        if(el.offsetTop<0||(el.offsetTop+el.offsetHeight)>window.innerHeight){transl.y=0}

                    }


                    el.addEventListener('touchstart',changestart,false);

                    el.addEventListener('touchmove',changemove,false);

                    el.addEventListener('touchend',changeend,false);


                    function changestart(e){

                        var touchdata = e.changedTouches[0];

                        startpoint.x = touchdata.pageX;

                        startpoint.y = touchdata.pageY;

                    }

                    function changemove(e){

                        var diffent = {x:0,y:0};

                        var movepoiont = {x:0,y:0};

                        e.preventDefault();

                        var touchdata = e.changedTouches[0];;

                        diffent.x = touchdata.pageX-startpoint.x;

                        diffent.y = touchdata.pageY-startpoint.y;

                        if(direction.x == true){movepoiont.x = diffent.x+curpoint.x;}

                        if(direction.y == true){movepoiont.y = diffent.y+curpoint.y;}

                        Limit(el,movepoiont);

                        move(el,movepoiont);

                        

                        var vw = window.innerWidth;

                        var vh = window.innerHeight;

                        console.log(vw,vh)

                        console.log(el.offsetLeft,el.offsetTop)

                        console.log(movepoiont.x,movepoiont.y)

                    }

                    function changeend (e){

                    var touchdata = e.changedTouches[0];

                    curpoint.x += touchdata.pageX - startpoint.x;

                    curpoint.y += touchdata.pageY - startpoint.y;

                    }

                }

            }

        }

        

        

        function changestart (e){

            var touchdata = e.changedTouches[0];

            ts.innerHTML = 'touch开始得位置:'+touchdata.pageX+'.'+touchdata.pageY;

        }

        function changemove (e){

            var touchdata = e.changedTouches[0];

            tm.innerHTML = 'touch当前得位置:'+touchdata.pageX+'.'+touchdata.pageY;

        }

        

    </script>

    <script>

        var box = document.getElementById('box');

        var boxmove = {x:true,y:true}

        drag(box,boxmove);

    </script>

</body>

</html>

老师看看到底是怎么回事?1)到了边界不是不动而是疯狂闪烁,我以为是offsetleft是0的时候,movepointx也恒为0就好了,事实上并不是这样    2)感觉根本没法精准确认哪个位置才是边界?不是左边就是offset为0吗,右边界根本没法算出来,上下同理了,浪费了我一下午时间,都没弄出来,真闹心

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

4回答
好帮手慕慕子 2020-06-22 11:03:47

同学你好,document.documentElement.getBoundingClientRect().height是获取页面的高度,也就是body的高度2000px, 但同学想要实现的是将方块现在在窗口区域内,那么就不能使用页面高度来计算你,所以需要通过window.innerHeight获取窗口的高度来计算

祝学习愉快~

好帮手慕慕子 2020-06-22 09:37:47

同学你好,获取当前窗口的大小是通过window.innerWidth获取的,需要去掉盒子的外边距,并将盒子修改为固定定位,老师这里以同学最开始粘贴的代码进行修改,具体代码如下,同学可以测试理解下

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin:0;
            padding:0;
        }
        .box {
            width: 50px;
            height: 50px;
            background-color: red;
            /* margin: 20px auto; */
            /* position: absolute; */
            position: fixed;
        }
        .background {
            width: 100%;
            height: 2000px;
            background-color: pink;
            position: relative;
        }
    </style>
</head>
<body>
    <div class="background">
        <div class="box" id="box"></div>
    </div>
    <script>
        var drag = function (el, direction) {
            if (direction.x !== undefined && direction.y !== undefined) {
                if (!direction.x || !direction.y) {
                    return;
                } else {
                    var curpoint = { x: 0, y: 0 };
                    var startpoint = { x: 0, y: 0 }
                    function move(el, transl) {
                        x = transl.x || 0;
                        y = transl.y || 0;
                        el.style.left = x + 'px';
                        el.style.top = y + 'px';
                    }
                    function Limit(el, transl) {
                        // var viewportX = document.documentElement.getBoundingClientRect().width || window.innerWidth,
                        //     viewportY = document.documentElement.getBoundingClientRect().height || window.innerHeight;
                       var viewportX = window.innerWidth
                       var viewportY = window.innerHeight
                        var limitX = viewportX - el.offsetWidth;
                        var limitY = viewportY - el.offsetHeight;
                        if (transl.x >= limitX) {
                            transl.x = limitX;
                        } else if (transl.x < 0) {
                            transl.x = 0;
                        }
                        if (transl.y >= limitY) {
                            transl.y = limitY;
                        } else if (transl.y < 0) {
                            transl.y = 0;
                        }
                    }
                    // if (el.offsetLeft <= 0 || (el.offsetLeft + el.offsetWidth) >= window.innerWidth) { transl.x = 0 }
                    // if (el.offsetTop < 0 || (el.offsetTop + el.offsetHeight) > window.innerHeight) { transl.y = 0 }
                }
                el.addEventListener('touchstart', changestart, false);
                el.addEventListener('touchmove', changemove, false);
                el.addEventListener('touchend', changeend, false);
                function changestart(e) {
                    var touchdata = e.changedTouches[0];
                    startpoint.x = touchdata.pageX;
                    startpoint.y = touchdata.pageY;
                }
                function changemove(e) {
                    var diffent = { x: 0, y: 0 };
                    var movepoiont = { x: 0, y: 0 };
                    e.preventDefault();
                    var touchdata = e.changedTouches[0];;
                    diffent.x = touchdata.pageX - startpoint.x;
                    diffent.y = touchdata.pageY - startpoint.y;
                    if (direction.x == true) { movepoiont.x = diffent.x + curpoint.x; }
                    if (direction.y == true) { movepoiont.y = diffent.y + curpoint.y; }
                    Limit(el, movepoiont);
                    move(el, movepoiont);
                    var vw = window.innerWidth;
                    var vh = window.innerHeight;
                    console.log(vw, vh)
                    console.log(el.offsetLeft, el.offsetTop)
                    console.log(movepoiont.x, movepoiont.y)
                }
                function changeend(e) {
                    var touchdata = e.changedTouches[0];
                    curpoint.x += touchdata.pageX - startpoint.x;
                    curpoint.y += touchdata.pageY - startpoint.y;
                }
            }
        }
        function changestart(e) {
            var touchdata = e.changedTouches[0];
            ts.innerHTML = 'touch开始得位置:' + touchdata.pageX + '.' + touchdata.pageY;
        }
        function changemove(e) {
            var touchdata = e.changedTouches[0];
            tm.innerHTML = 'touch当前得位置:' + touchdata.pageX + '.' + touchdata.pageY;
        }
    </script>
    <script>
        var box = document.getElementById('box');
        var boxmove = { x: true, y: true }
        drag(box, boxmove);
    </script>
</body>
</html>

祝学习愉快~

  • 提问者 慕雪9296518 #1
    viewportY =document.documentElement.getBoundingClientRect().height ||window.innerHeight;这句话又出问题吗
    2020-06-22 10:08:09
好帮手慕慕子 2020-06-20 09:51:21

同学你好,对于你的问题解答如下:

  1. 因为body默认有8px的外间距,所以导致上下有间隙,直接使用通配符去除所有的默认的内外间距就可以了

  2. 因为同学给body设置的高度为2000px, 页面存在滚动条, 所以需要将页面滚动到底部测试垂直方向的边界限制情况

  3. 下图所示,同学是想要实现将方块限制在当前窗口吗?

    http://img1.sycdn.imooc.com//climg/5eed6a50097ccd9912420116.jpg

    如果这样的话,可以将老师第一次回复给出的解决方案中,将获取外层盒子的大小换成浏览器当前窗口的大小,然后将方块调整成固定定位就可以了。示例:

    http://img1.sycdn.imooc.com//climg/5eed6b5b098acb1021361758.jpg

    http://img1.sycdn.imooc.com//climg/5eed6b6b09ef34e609520800.jpg

同学可以结合代码自己测试下,如果不是指这里,可以详细描述下具体想要实现什么效果,便于帮助同学准确的定位与解决问题

祝学习愉快~

  • 提问者 慕雪9296518 #1
    var viewportX =document.documentElement.getBoundingClientRect().width ||window.innerWidth, viewportY =document.documentElement.getBoundingClientRect().height ||window.innerHeight; var limitX = viewportX - el.offsetWidth; var limitY = viewportY - el.offsetHeight; if(direction.x == true){ movepoiont.x = diffent.x+curpoint.x; if(movepoiont.x>=limitX){ movepoiont.x = limitX; }else if (movepoiont.x < 0) { movepoiont.x = 0;} } if(direction.y == true){ movepoiont.y = diffent.y+curpoint.y; if(movepoiont.y>=limitY){ movepoiont.y = limitY; }else if (movepoiont.y < 0) { movepoiont.y = 0;} } move(el,movepoiont); } 这边我就是这样改的,还是可以到最底部
    2020-06-22 09:08:24
好帮手慕慕子 2020-06-19 19:31:44

同学你好,同学的这两个问题都是因为边界处理有问题才导致的。offsetLeft是只读属性,也就是只能获取该属性值,但是不能够设置,那么就只有元素位置改变之后,才可以获取到,而且offsetLeft属性的兼容性不好, 在不同浏览器下表现不同, 所以不推荐使用这个作为判断条件。

边界条件就是通过获取外层盒子的宽高,然后减去红色方块的宽高,得到的就是红色方块left和top的最大值。示例:

http://img1.sycdn.imooc.com//climg/5eeca1a809c6fb1821721540.jpg

另,去掉所有的外边距带来的影响,

http://img1.sycdn.imooc.com//climg/5eeca1c80967ad1709200632.jpg

如果我的回答帮助到了你,欢迎采纳,祝学习愉快~

  • 提问者 慕雪9296518 #1
    对,我刚想问为什么上下还有个空隙。另外,为什么我的小方块可以拉到下方边界,也就是滚条到最下方才收到限制?怎么才可以让它限制在当前那个高度下的下框上面,而不是到最底部才受限制
    2020-06-19 19:35:02
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
3.WebAPP开发与小程序
  • 参与学习           人
  • 提交作业       622    份
  • 解答问题       6815    个

微信带火了小程序,也让前端工程师有了更多的展现机会,本阶段带你从移动基础知识的学习到webAPP开发,及小程序开发,让你PC端与移动端两端通吃。

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师