位置

简介: 位置

1.offset()


方法描述:相对页面左上角的坐标。

需求描述:获取div相对页面左上角的坐标

.box {
width: 100px;
height: 100px;
background: coral;
}


<div class="box"></div>


var offset = $('.box').offset();
console.log(offset.left, offset.top);



2.position()


方法描述:相对于父元素左上角的坐标。

需求描述:获取div相对于父元素左上角的坐标

.box-container {
    width: 300px;
    height: 300px;
    background: pink;
    position: absolute;
    left: 20px;
    top: 20px;
}


.box {
    width: 100px;
    height: 100px;
    background: coral;
    position: absolute;
    left: 20px;
    top: 20px;
}
<div class="box-container">
    <div class="box"></div>
</div>
var offset = $('.box').position();
console.log(offset.left, offset.top);


3.scrollLeft()


设置页面滚动条滚动到指定位置(兼容chrome和IE)


$('body,html').scrollLeft(60);

需求描述:设置页面的宽度为2000px,设置滚动条的X轴坐标为300px,要求兼容各种浏览器


$('body').css('width', '2000px');
$('html,body').scrollLeft(300);



4. scrollTop()


方法描述:读取/设置滚动条的Y坐标,该方法读写合一。

读取页面滚动条的Y坐标(兼容chrome和IE)


$('body,html').scrollTop(60);


$('body').css('height', '2000px');
$('html,body').scrollTop(300);



相关文章
|
1月前
|
存储 算法 C语言
找到数组位置
找到数组位置
|
1月前
|
API Python
文件位置标记及其定位
文件位置标记及其定位
13 2
|
4月前
|
算法 小程序 API
uniapp显示当前位置与所传入位置的距离
uniapp显示当前位置与所传入位置的距离
66 0
实现strrstr,找到子串最后的出现位置
实现strrstr,找到子串最后的出现位置
79 0
|
Java Python
在数组中查找元素的第一个和最后一个位置
在数组中查找元素的第一个和最后一个位置
在数组中查找元素的第一个和最后一个位置
|
JavaScript 前端开发 Go
快速找到thtmlbUtil的定义位置
Created by Jerry Wang, last modified on Oct 10, 2014 Go to start of metadata
快速找到thtmlbUtil的定义位置
|
JavaScript
Dom中偏移父节点及偏移位置
Dom中偏移父节点及偏移位置
123 0
Dom中偏移父节点及偏移位置
|
安全
阿里云常见参数获取位置
示例主要介绍阿里云常见参数的获取位置。
522 0
阿里云常见参数获取位置
|
移动开发 Linux Windows
文件的指针位置
f = open('指针测试.txt','a+',encoding='utf-8') # 这里会直接创建文件,可查看a,w,r,以及分别加加号‘+’和加b的区别 # tell() 显示文件指针 print(f.
920 0