首页 > 代码库 > JavaScript限制div不跑出屏幕外边

JavaScript限制div不跑出屏幕外边

 1 function fnMove(ev) {
 2     var event = ev || window.event;
 3     var l = event.clientX - this.disX;
 4     var t = event.clientY - this.disY
 5 
 6 
 7     if (l < 0) {
 8         l = 0;
 9     }
10     else if (l > document.documentElement.clientWidth - this.oDiv.offsetWidth) {
11 
12         l = document.documentElement.clientWidth - this.oDiv.offsetWidth
13     }
14     ;
15 
16     if (t < 0) {
17         t = 0;
18     }
19     else if (t > document.documentElement.clientHeight - this.oDiv.offsetHeight) {
20 
21         t = document.documentElement.clientHeight - this.oDiv.offsetHeight
22     }
23 
24     this.oDiv.style.left = l + ‘px‘; 
25    this.oDiv.style.top = t + ‘px‘;

 

JavaScript限制div不跑出屏幕外边