理解: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()?的更多相关文章

  1. 盒子 offsetLeft、offsetTop、offsetWidth、getBoundingClientRect等属性解释

    offsetLeft 获取的是忽略 margin 当前元素距离上一级父节点(有没有设置position,有的话依据父节点,没有的话依据页面最左端这时候不管滚动条移到哪) 当前元素向左的位置 记住它会将 ...

  2. JavaScript中getBoundingClientRect()方法详解

    获取浏览器滚动的高度: scrollTop=document.documentElement.scrollTop || document.body.scrollTop getBoundingClien ...

  3. js中getBoundingClientRect的作用及兼容方案

    js中getBoundingClientRect的作用及兼容方案 1.getBoundingClientRect的作用 getBoundingClientRect用于获取某个html元素相对于视窗的位 ...

  4. 利用getBoundingClientRect方法实现简洁的sticky组件

    补充于2016-03-20: 本文实现有不足,不完美的地方,请在了解本文相关内容后,移步阅读<sticky组件的改进实现>了解更佳的实现. sticky组件,通常应用于导航条或者工具栏,当 ...

  5. CSSOM之getboundingclientrect和getclientrects

    TextRectangle 对于文本对象,W3C提供了一个 TextRectangle 对象,这个对象是对文本区域的一个解释. 对于 i,span,em等display 是inline的标签,在书写文 ...

  6. getBoundingClientRect在IE9/10里的bug

    getBoundingClientRect可以获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置,最早在IE中实现,后其它浏览器均已实现. 但它在IE9,10中有个bug,当出现垂直滚动条时, ...

  7. getBoundingClientRect详解

    getBoundingClientRect获取的left和top始终是该元素相对于页面原点坐标的位置,当它大于原点坐标则为正数,等于原点坐标为零,小于为负数.

  8. getBoundingClientRect() 来获取页面元素的位置

    getBoundingClientRect() 来获取页面元素的位置   document.documentElement.getBoundingClientRect 下面这是MSDN的解释: Syn ...

  9. getBoundingClientRect()兼容性处理

    getBoundingClientRect() 这个方法返回一个矩形对象,包含四个属性:left.top.right和bottom.分别表示元素各边与页面上边和左边的距离. var box=docum ...

  10. .getBoundingClientRect()

    .getBoundingClientRect() 该方法获得页面中某个元素的左,上,右和下分别相对浏览器视窗的位置,他返回的是一个对象,即Object,该对象有4个属性:top,left,right, ...

随机推荐

  1. WSO2 ESB XML定义语法(1)

    1.(基于Apache Synapse的WSO2 ESB实现)参考文件位置: http://synapse.apache.org/userguide/config.html 基于Apache Syna ...

  2. ueditor使用本地保存,自动恢复上次编辑的内容

    个人博客 地址:http://www.wenhaofan.com/article/20180912212800 ueditor默认开启了自动保存至本地的功能,但是依然需要在初始化的时候额外添加代码该功 ...

  3. Python里的Flask开发环境的搭建

    在已经安装好了Python后,我这里用的是Python3.5,准备进一步学习Flask框架,下面记录搭建Flask环境 这里使用了虚拟环境,在虚拟环境里,最小化的安装Flask. 参考步骤: 1.首先 ...

  4. Javascript数组与字符串常用api

    目录 javaScript(api学习) 数组有关的api 创建数组 数组的增删改查 indexOf(); push(),pop(),unshift(),shift() forEach() map() ...

  5. python:if else 语句

    #!/usr/bin/python# -*- coding:utf-8 -*- import os fileName1 = 'a.txt'if os.path.exists(fileName1): f ...

  6. logistic回归损失函数(非常重要,深入理解)

    2.2 logistic回归损失函数(非常重要,深入理解) 上一节当中,为了能够训练logistic回归模型的参数w和b,需要定义一个成本函数 使用logistic回归训练的成本函数 为了让模型通过学 ...

  7. linux中的oracle数据库

    安装orcal http://www.cnblogs.com/nucdy/p/5603998.html 搭建Linux操作系统+Oracle数据库的环境 https://blog.csdn.net/p ...

  8. sql注入的原理是什么,怎么预防sql注入

    为什么会产生sql注入: 主要原因,对用户输入的绝对信任,相信所有用户的输入都是可信的,没有对用户输入的语句进行过滤或者筛选,直接放到sql语句中进行拼接,从而导致了sql注入的产生 例如: < ...

  9. HTML文字标签

    <h1>标题标签,总共六个等级,不能创造标签,只有预定义好的标签才可以被浏览器解析 <br>换行标签,没有内容可以修饰也称为空标签 <p>段落标签</p> ...

  10. python之路之面向对象

    一.编程 1.面向对象编程和函数式编程 a.面向对象编程之self(重点) b.面向对象的封装  c.继承 (1)继承的优先级 d.重载 e.多态  2.查看socketserver源码执行过程