你了解getBoundingClientRect()?
理解:getBoundingClientRect用于获取某个元素相对于视窗的位置集合。集合中有top, right, bottom, left等属性。
1.语法:这个方法没有参数。
rectObject = object.getBoundingClientRect();
2.返回值类型:
rectObject.top:元素上边到视窗上边的距离;
rectObject.right:元素右边到视窗左边的距离;
rectObject.bottom:元素下边到视窗上边的距离;
rectObject.left:元素左边到视窗左边的距离;

3.思考 :这个属性在ie5就开始支持,能做什么呢? 在淘宝 京东 支付宝 里面可见的效果 。
4.如下效果:

5.Html:

6.逻辑:
(function ($) {
function myScroll(element, option) {
this.element = element;
this.setting = $.extend({}, option, myScroll.defaults)
this.init();
}
myScroll.defaults = {
fixed: {
"position": "fixed",
"top": 0,
"z-index": 1000,
},
none: {
"position": "relative",
"z-index": 0
}
}
myScroll.prototype = {
init: function () {
var target = this.setting.target;
var fixed = this.setting.fixed;
var none = this.setting.none;
var element = this.element;
$(window).scroll(function () {
var obj = document.getElementById(target.slice(1)).getBoundingClientRect();
if (obj.top - $(this.element).height() < 0 && obj.bottom - $(this.element).height() > 0) {
$(element).css(fixed)
$(element).css("width",$(element).parent().width()+"px")
} else {
$(element).css(none)
}
});
},
}
function myPlugin(option) {
return this.each(function () {
var that = $(this)
var data = that.data('bs')
var options = typeof option == 'object' && option
that.data('bs', new myScroll(this, options))
})
}
$.fn.myScroll = myPlugin
$.fn.myScroll.Constructor = myScroll
$(window).on('load', function () {
$('[data-type="top"]').each(function () {
var type = $(this)
myPlugin.call(type, type.data())
})
})
})(jQuery)
作者:原型设计
链接:https://www.jianshu.com/p/824eb6f9dda4
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
你了解getBoundingClientRect()?的更多相关文章
- 盒子 offsetLeft、offsetTop、offsetWidth、getBoundingClientRect等属性解释
offsetLeft 获取的是忽略 margin 当前元素距离上一级父节点(有没有设置position,有的话依据父节点,没有的话依据页面最左端这时候不管滚动条移到哪) 当前元素向左的位置 记住它会将 ...
- JavaScript中getBoundingClientRect()方法详解
获取浏览器滚动的高度: scrollTop=document.documentElement.scrollTop || document.body.scrollTop getBoundingClien ...
- js中getBoundingClientRect的作用及兼容方案
js中getBoundingClientRect的作用及兼容方案 1.getBoundingClientRect的作用 getBoundingClientRect用于获取某个html元素相对于视窗的位 ...
- 利用getBoundingClientRect方法实现简洁的sticky组件
补充于2016-03-20: 本文实现有不足,不完美的地方,请在了解本文相关内容后,移步阅读<sticky组件的改进实现>了解更佳的实现. sticky组件,通常应用于导航条或者工具栏,当 ...
- CSSOM之getboundingclientrect和getclientrects
TextRectangle 对于文本对象,W3C提供了一个 TextRectangle 对象,这个对象是对文本区域的一个解释. 对于 i,span,em等display 是inline的标签,在书写文 ...
- getBoundingClientRect在IE9/10里的bug
getBoundingClientRect可以获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置,最早在IE中实现,后其它浏览器均已实现. 但它在IE9,10中有个bug,当出现垂直滚动条时, ...
- getBoundingClientRect详解
getBoundingClientRect获取的left和top始终是该元素相对于页面原点坐标的位置,当它大于原点坐标则为正数,等于原点坐标为零,小于为负数.
- getBoundingClientRect() 来获取页面元素的位置
getBoundingClientRect() 来获取页面元素的位置 document.documentElement.getBoundingClientRect 下面这是MSDN的解释: Syn ...
- getBoundingClientRect()兼容性处理
getBoundingClientRect() 这个方法返回一个矩形对象,包含四个属性:left.top.right和bottom.分别表示元素各边与页面上边和左边的距离. var box=docum ...
- .getBoundingClientRect()
.getBoundingClientRect() 该方法获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置,他返回的是一个对象,即Object,该对象有4个属性:top,left,right, ...
随机推荐
- Selenium3+python自动化009- 多选框
多选框 # 随机选择多选框# sports=driver.find_elements_by_name("sport")# maxnum=len(sports)# num=rando ...
- vue formatter element表格处理数据
formatter 指定一个vue methods 方法 作用:对从数据库中取出的数据进行处理后展示. <el-table-column prop="partner1" // ...
- C语言-无符号数与有符号数不为人知的秘密
一.无符号数与有符号数 1.计算机中的符号位 数据类型的最高位用于标识数据的符号 -最高位为1,表明这个数为负数 -最高位为0,表明这个数为正数 #include <stdio.h> in ...
- solr 对于 关键字的特殊处理
public static String transformMetachar(String input){ StringBuffer sb = new StringBuffer(); ...
- Java基本语法--程序流程控制
流程控制语句是用来控制程序中各语句执行顺序的语句,可以把语句组合成能完成一定功能的小逻辑模块.流程控制方式采用结构化程序设计中规定的三种基本流程结构,即:顺序结构.分支结构.循环结构.本篇博客主要是对 ...
- Docker最全教程——从理论到实战(十一)
前言 容器教程的路还很长,笔者尽量根据实践来不断地完善.由于在编写的过程中还会有完善和补充,后续可能会以番外来补充. 接下来会分享TeamCity.树莓派等内容,节奏可能会有点跳脱. 另外,长沙.NE ...
- php单例模式封装数据库操作类增删改查
<?php//三私一公 单例class Db{ //数据库连接对象 private static $instance; private static $table_name; private $ ...
- 【Unity|C#】番外篇(1)——6个重要概念:栈与堆,值类型与引用类型,装箱与拆箱
传送门:https://www.cnblogs.com/arthurliu/archive/2011/04/13/2015120.html
- ubuntu apt
一些命令: sudo apt-get update 更新源 sudo apt-get install package --reinstall 重新安装包 sudo apt-get upgrade ...
- MyEclipse把普通的项目变成hibernate项目