整理了关于javascript中mouse坐标几段函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
function ___getPageScroll() {
    var xScroll, yScroll;
    if (window.innerHeight && window.scrollMaxY) {
        xScroll = window.innerWidth + window.scrollMaxX;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight) { // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    return {xScroll: xScroll, yScroll: yScroll};
}
 
function ___getPageSize() {
    var xScroll, yScroll, pageWidth, pageHeight, windowWidth, windowHeight;
     
    xScroll = ___getPageScroll().xScroll;
    yScroll = ___getPageScroll().yScroll;
     
    if (self.innerHeight) { // all except Explorer
        if (document.documentElement.clientWidth) {
            windowWidth = document.documentElement.clientWidth;  
        } else {
            windowWidth = self.innerWidth;
        }
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }
    // for small pages with total height less then height of the viewport
    if (yScroll < windowHeight) {
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }
    // for small pages with total width less then width of the viewport
    if (xScroll < windowWidth) {
        pageWidth = xScroll;
    } else {
        pageWidth = windowWidth;
    }
    arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight);
    return arrayPageSize;
}
 
//获取鼠标当前所在的坐标
function mouseCoords(ev){
    if(ev.pageX || ev.pageY){
        return {x:ev.pageX, y:ev.pageY};
    }
    return {
        x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
        y:ev.clientY + document.body.scrollTop  - document.body.clientTop
    };
}
 
//扩展原型对象的属性
Number.prototype.NaN0 = function () { return isNaN(this) ? 0 : this; }
 
function getPosition(e){
    var left = 0;
    var top  = 0;
    while (e.offsetParent){
        left += e.offsetLeft + (e.currentStyle ? (parseInt(e.currentStyle.borderLeftWidth)).NaN0() : 0);
        top  += e.offsetTop  + (e.currentStyle ? (parseInt(e.currentStyle.borderTopWidth)).NaN0() : 0);
        e     = e.offsetParent;
    }
 
 
    left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
    top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
 
    return {x: left, y: top};
}
 
//获取当前鼠标相对目标元素的偏移量
function getMouseOffset(target, ev){
    ev = ev || window.event;
    var docPos    = getPosition(target);
    var mousePos  = mouseCoords(ev);
            console.log('mousePos.x:' + mousePos.x + ' , mousePos.y:' + mousePos.y);
            console.log('docPos.x:' + docPos.x + ' , docPos.y:' + docPos.y);
    return {x: mousePos.x - docPos.x, y: mousePos.y - docPos.y};
}
 
 
console.log(___getPageSize());
 
console.log('document.width:' + $(document).width() + ',' + $(window).width());
posted @   leamiko  阅读(291)  评论(0编辑  收藏  举报
努力加载评论中...
document.getElementById('MySignature') && document.getElementById('MySignature').style.display = "none"; document.getElementById('MySignature') && document.getElementById('blog_post_info').style.display = "none";
点击右上角即可分享
微信分享提示