滑鼠拖拽 晉級限制出界
繼續上一篇繼續學習
想要限制出界就要把 node.style.left和node.style.top最小值設為0 然后獲取頁面視窗的大小
第一種方法
var windowWidth = document.documentElement.clientWidth || document.body.clientWidth;//有兼容問題,咱就這樣寫啦
var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
最大值寬高分別為
l = windowWidth - node.offsetWidth;// node以左上角為原點 如果不減去node的寬度 將會超出整整一個node的寬度!!!
t = windowHeight - node.offsetHeight//同上


第二種方法
原理還是一樣的只不過是使用了Math.min()和Math.max()方法

當l出界限 l的值小于0 取最大值0
當l出界限 l大于windowWidth - node.offsetWidth 取最小值windowWidth - node.offsetWidth

同上
原始碼

**************************快樂的一天學習又結束了 *************
uj5u.com熱心網友回復:
nice,加油轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/7578.html
標籤:JavaScript
上一篇:登錄頁面跳轉后再回傳就登不進去了
