pageX/Y, offset(), position(), scrollTop(), screenX/Y, clientX/Y, pageX/Y
event.pageX get mouse position
Description: The mouse position relative to the left edge of the document.
Example
<script>
$(document).on( "mousemove", function( event ) {
console.log( "pageX: " + event.pageX + ", pageY: " + event.pageY );
});
</script>
.offset() get offset position of an element
Description: Get the current coordinates of the first element, or set the coordinates of every element, in the set of matched elements, relative to the document.
Example(get)
<script>
var p = $(element);
var offset = p.offset();
p.html( "left: " + offset.left + ", top: " + offset.top );
</script>
Example(set)
<script>
// the parameter must be PlainObject
var coord = {top: 50, left: 100};
$(element).offset(coord);
</script>
.position() get the relative Position of an element
Description: Get the current coordinates of the first element in the set of matched elements, relative to the offset parent.
Note: this method does not accept any arguments.
Example
<script>
var p = $(element);
var position = p.position();
console.log( "left: " + position.left + ", top: " + position.top );
</script>
Description: Get the current vertical position of the scroll bar for the first element in the set of matched elements or set the vertical position of the scroll bar for every matched element.
Example(get)
<script>
var p = $(element);
console.log( "scrollTop:" + p.scrollTop() );
</script>
Example(set)
<script>
var topValue = 500;
$(element).scrollTop(topValue);
</script>
The difference between screenX/Y, clientX/Y and pageX/Y
- pageX/Y gives the coordinates relative to the
<html>element in CSS pixels. - clientX/Y gives the coordinates relative to the
viewportin CSS pixels. - screenX/Y gives the coordinates relative to the
screenin device pixels.
Example
<script>
document.addEventListener('click', function(e) {
console.log(
'page: ' + e.pageX + ',' + e.pageY,
'client: ' + e.clientX + ',' + e.clientY,
'screen: ' + e.screenX + ',' + e.screenY)
});
</script>
A picture explaining the difference between pageY and clientY:

jQuery Scroll to bottom of page/iframe
<script>
$('html, body').animate({
scrollTop: $(document).height()-$(window).height()},
2000
);
</script>
Composite example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Demo</title>
</head>
<body style="height:1000px;">
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<div id="A" style="left:100px;"> Default <br /> mouse<br/>position </div>
<div id="B" style="left:300px;"> offset() <br /> mouse<br/>position </div>
<div id="C" style="left:500px;"> position() <br /> mouse<br/>position </div>
<div id="D" style="left:700px;"> client <br /> mouse<br/>position </div>
</body>
<style>
#A,#B,#C,#D {
width: 100px;
height: 100px;
cursor: pointer;
background: #2f2f2f;
position: absolute;
top: 50px;
color: #fff;
font: bold 15px Arial;
}
</style>
<script>
$(document).ready(function (e) {
// pageX
$('#A').click(function (e) {
console.log(e.pageX + ' , ' + e.pageY);
});
// offset()
$('#B').click(function (e) {
var posX = $(this).offset().left,
posY = $(this).offset().top;
console.log((e.pageX - posX) + ' , ' + (e.pageY - posY));
});
// position
$('#C').click(function (e) {
var posX = $(this).position().left,
posY = $(this).position().top;
console.log((e.pageX - posX) + ' , ' + (e.pageY - posY));
});
// client offset scroll
$('#D').click(function (e) {
var offset_t = $(this).offset().top - $(window).scrollTop();
var offset_l = $(this).offset().left - $(window).scrollLeft();
var left = Math.round( (e.clientX - offset_l) );
var top = Math.round( (e.clientY - offset_t) );
console.log("Left: " + offset_t + " Top: " + offset_l);
});
});
</script>
</body>
</html>
pageX/Y, offset(), position(), scrollTop(), screenX/Y, clientX/Y, pageX/Y的更多相关文章
- screenX、clientX、pageX的区别
screenX:鼠标位置相对于用户屏幕水平偏移量,而screenY也就是垂直方向的,此时的参照点也就是原点是屏幕的左上角. clientX:跟screenX相比就是将参照点改成了浏览器内容区域的左上角 ...
- offset() position() scrollTop() scrollLeft()
(1)offset:获取当前元素相对于文档的高度.只对可见元素有效. 不管该元素如何定位,也不管其父元素如何定位,都是获取的该元素相对于当前视口的偏移 (2) position:获取元素相对于最近的一 ...
- 一句话解释jquery中offset、pageX, pageY、position、scrollTop, scrollLeft的区别
offset 元素相对文档的偏移 pageX, pageY 事件(鼠标)相对文档的偏移 注意:文档是指document, 而不是当前窗口,是包含了滚动位置的,即滚动条的位置对这些值是不产生影响的 ...
- 通过了解JS的clientX、pageX、screenX等方法来获取鼠标位置相对屏幕,相对浏览器窗口,相对文档的坐标详解
在一些DOM操作中我们经常会跟元素的位置打交道,鼠标交互式一个经常用到的方面,令人失望的是不同的浏览器下会有不同的结果甚至是有的浏览器下没结果,这篇文章就上鼠标点击位置坐标获取做一些简单的总结,没特殊 ...
- pageX/pageY,screenX/screenY,clientX/clientY的差别
pageX/pageY,screenX/screenY,clientX/clientY的差别 $(document).click(function(e){ //x方向无差别 //alert(e.pag ...
- 18年春招某编程题:有三个整数X,Y,Z,要求进行若干次操作使得X,Y,Z相等
题目描述: 给定三个整数X,Y,Z,要求进行若干次操作使得X,Y,Z相等,操作有两种: 1.从X,Y,Z中选择两个数都加1. 2.从X,Y,Z中选择一个数加2. 求最少需要多少次操作. 题目思路: 1 ...
- 函数的光滑化或正则化 卷积 应用 两个统计独立变量X与Y的和的概率密度函数是X与Y的概率密度函数的卷积
http://graphics.stanford.edu/courses/cs178/applets/convolution.html Convolution is an operation on t ...
- javascript中offsetWidth、clientWidth、width、scrollWidth、clientX、screenX、offsetX、pageX
原文:https://www.cnblogs.com/ifworld/p/7605954.html 元素宽高 offsetWidth //返回元素的宽度(包括元素宽度.内边距和边框,不包括外边距) o ...
- clientX、pageX、offsetX、screenX的区别
这几个属性的区别说难不难,可是很容易搞混,很长一段时间没用,发现又忘记区别了,记不清哪个是哪个!真的很抓狂! 区别: clientX.clientY: 相对于浏览器窗口可视区域的X,Y坐标(窗口坐标) ...
随机推荐
- android-Spinner的学习和使用
Spinner下拉列表的使用和功能 执行步骤: * 1.添加一个下拉列表项的list * 2.为下拉列表定义一个数组适配器(ArrayAdapter),添加数据资源 * 3.位适配器设置下拉列表下拉时 ...
- GitLab安装手记
阿里云1G内存20G硬盘 1.首先下载GitLab Deb包(官网附有apt-get安装方式,但国内环境貌似不成功): https://about.gitlab.com/downloads/ 2. d ...
- call与apply基础用法告诉你
Js用法中还有个极好的但是有些抽象的用法——call与apply,今天我要简单总结一下: var ob1={ //对象ob1 name:"ob1", func1:function( ...
- 从jQuery中学习来的另一种继承方式(技巧)
遵从Js的原型链规则,利用js灵活的特性灵活地改造原型,可以实现各种创意地继承方式,昨天研究了jQuery,对作者实现继承的方式感到佩服,他对js原型和原型链的理解比较透彻,运用自如.这里给出jQue ...
- 2016HUAS暑假集训训练2 E - I Hate It
Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老 ...
- Javascript学习总结
---恢复内容开始--- 浏览器对象树
- 链表的C++实现——创建-插入-删除-输出-清空
注:学习了数据结构与算法分析后,对链表进行了C++实现,参考博文:http://www.cnblogs.com/tao560532/articles/2199280.html 环境:VS2013 // ...
- 20145334 第五次 java 实验报告
实验内容 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 我和20145306张文锦组队编程 http://www.cnblogs.com/besti145306/ ...
- BizTalk开发系列(三十七) 性能监视器在BizTalk性能测试中的使用
BizTalk应用程序的性能测试和分析是一个非常重要的过程,因为BizTalk的应用程序在Run-time时受部署结构.消息请求数量和消息大小等 的影响很大,因此无论是简单还是复杂的的应用都需要在部署 ...
- IOS第16天(5,Quartz2D雪花)
*** #import "HMView.h" @interface HMView() { int count; } @property (nonatomic, assign) CG ...