JQuery杂项
创建时间:2018/7/12 9:47
最后更新时间:2018/7/12 9:47
主要内容:
记录一些忘记了的jQuery方法&jQuery操作
jQuery获得元素的位置信息
参考:https://www.cnblogs.com/fnz0/p/5510758.html
https://www.cnblogs.com/xiaohuochai/p/5923469.html
//获得浏览器窗口滚动条的当前位置
$(window).scrollTop()
$(window).scrollLeft()
aaa = $("#aaa");
//特定元素当前相对文档顶端和左端的位置
aaa.offset().top
aaa.offset().left
//获得宽和高 / 设置
aaa.height() / aaa.height(value)
aaa.width() / aaa.width(value)
//不包含border的宽高
aaa.innerHeight()
aaa.innerWidth()
//全宽高(包含content、padding、border)
outerWidth()
outerHeight()
//获得定位用的父级元素的jQuery对象
offsetParent()
//相对该父元素的位置,也就是css中的left和top
position().left
position().top
//也可以用于设置left和top
$(this).offset({top:20,left:20})
//使用带参数的函数的进行设置,
//index为元素在匹配的元素集合中的索引位置
//coords为元素当前的坐标
offset(function(index,coords){
return {left: coords.left+10, top: coords.top}
})
实现类似fixed定位的效果
//需要将元素设置为absolute或者relative
function scroll_p(){
aaa.css("top",$(window).scrollTop()+"px");
}
window.onscroll = scroll_p