读书人

获取页面的滚动位置与鼠标事件中的座标

发布时间: 2012-11-11 10:07:57 作者: rapoo

获取页面的滚动位置与鼠标事件中的坐标

/**    获取当前滚动的位置。*/function getScrollingPosition(){var position = [0, 0];    //FFif (typeof window.pageYOffset != 'undefined'){position = [window.pageXOffset,window.pageYOffset];}    //IEelse if (typeof document.documentElement.scrollTop!= 'undefined' && document.documentElement.scrollTop > 0 ||document.documentElement.scrollLeft > 0){position = [document.documentElement.scrollLeft,document.documentElement.scrollTop];}else if (typeof document.body.scrollTop != 'undefined'){position = [document.body.scrollLeft,document.body.scrollTop];}return position;}

/** 获取鼠标位置*/function displayCursorPosition(event){ if (typeof event == "undefined") { event = window.event; } var scrollingPosition = getScrollingPosition(); var cursorPosition = [0, 0]; if (typeof event.pageX != "undefined" && typeof event.x != "undefined") { cursorPosition[0] = event.pageX; cursorPosition[1] = event.pageY; } else { cursorPosition[0] = event.clientX + scrollingPosition[0]; cursorPosition[1] = event.clientY + scrollingPosition[1]; } document.title = cursorPosition.toString();}

读书人网 >Web前端

热点推荐