亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

if里面的判斷不理想,請大神出個解決方案?

if里面的判斷不理想,請大神出個解決方案?

泛舟湖上清波郎朗 2018-09-17 07:07:38
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 - (oDiv.clientWidth / 2)) {                oDiv.style.top = oa1.clientHeight - oDiv.clientHeight + "px";            } else {                oDiv.style.top = y - 50 + "px";            }            if (oEvnt.clientY - oa1.offsetTop >= oa1.clientHeight - oDiv.clientHeight / 2) {                oDiv.style.left = oa1.clientWidth - oDiv.clientWidth + "px"            } else {                oDiv.style.left = x - 50 + "px";            }        };在上面的if條件我想的是當鼠標劃到離a標簽邊距只有50px就讓div的top值固定為一個值。上面只是部分代碼。也許我用判斷語句不是很合適,請教這位大神如何解決?
查看完整描述

1 回答

?
肥皂起泡泡

TA貢獻1829條經驗 獲得超6個贊

如果想判斷范圍,比如說,小矩形(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 的值應該會算吧


查看完整回答
反對 回復 2018-10-10
  • 1 回答
  • 0 關注
  • 580 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號