javascript - if里面的判断不理想,请大神出个解决方案?
大家讲道理
大家讲道理 2017-04-11 12:43:56
[JavaScript讨论组]
oa1.onmousemove = function (ev) {
            var oEvnt = ev || event;
            var x     = oEvnt.clientX - oa1.offsetLeft;
            var y     = oEvnt.clientY - oa1.offsetTop;
            console.log(x, y);
            oa2.style.left = -(x * 2 - 100) + 'px';//问题用clientWidth为什么是NAN
            oa2.style.top = -(y * 2 - 100) + 'px';
            if (x >= oa1.clientWidth - (op.clientWidth / 2)) {
                op.style.top = oa1.clientHeight - op.clientHeight + "px";

            } else {
                op.style.top = y - 50 + "px";

            }
            if (oEvnt.clientY - oa1.offsetTop >= oa1.clientHeight - op.clientHeight / 2) {
                op.style.left = oa1.clientWidth - op.clientWidth + "px"
            } else {

                op.style.left = x - 50 + "px";
            }
        };
        
    

在上面的if条件我想的是当鼠标划到离a标签边距只有50px就让p的top值固定为一个值。

上面只是部分代码。也许我用判断语句不是很合适,请教这位大神如何解决?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(1)
PHP中文网

如果想判断范围,比如说,小矩形(inRect)必须在大矩形内部(outRect) 定位的情况,可以这样处理

inRect.x = getInLeft();   // 根据运算得到理论 x 值,可能超出范围,比如为负
inRect.y = getInTop();

inRect.x = Math.min(Math.max(0, inRect.x), outRect.right - inRect.width);
inRect.y = Math.min(Math.max(0, inRect.y), outRect.bottom - inRect.height);

// 其中 outRect.right 的值应该会算吧
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号