javascript代码片段
DOMReady函数,只要DOM结构加载完成即可,不必等待所有资源加载完成,节约时间,"DOMContentLoaded"在H5中被标准化
var DOMReady=function(f,a,d){
d = d || document;
a = a || "addEventListener";
d[a]?d[a]('DOMContentLoaded',f,false):window.attachEvent('onload',f);
} DOMReady(function(){
alert("The DOM is Ready");
})
检查元素是否在视窗(分完全在视窗内,部分在视窗内以及完全不在视窗内)
function isInViewPort(ele){
var rect=ele.getBoundingClientRect();
alert(rect.top+","+rect.right+","+rect.bottom+","+rect.left);
var newRect=getRect(ele);
alert(newRect.top+","+newRect.right+","+newRect.bottom+","+newRect.left);
return rect.top < (window.innerHeight || document.documentElement.clientHeight) &&
rect.right < (window.innerWidth || document.documentElement.clientWidth) &&
rect.bottom < (window.innerHeight || document.documentElement.clientHeight) &&
rect.left < (window.innerWidth || document.documentElement.clientWidth);
}
IE7及以下浏览器的坐标从(2,2)开始计算,需要提供兼容方案
function getRect(ele){
var rect=ele.getBoundingClientRect();
var top=document.documentElement.clientTop; //IE=2 非IE=0
var left=document.documentElement.clientLeft; //IE=2 非IE=0
return{
top:rect.top-top,
bottom:rect.bottom-top,
left:rect.left-left,
right:rect.right-left
}
}
javascript代码片段的更多相关文章
- 精心收集的 48 个 JavaScript 代码片段,仅需 30 秒就可理解!
原文:https://github.com/Chalarangelo/30-seconds-of-code#anagrams-of-string-with-duplicates 作者:Chalaran ...
- 精心收集的 48 个 JavaScript 代码片段,仅需 30 秒就可理解
原文:Chalarangelo 译文:IT168 https://github.com/Chalarangelo/30-seconds-of-code#anagrams-of-string-with ...
- 精心收集的48个JavaScript代码片段,仅需30秒就可理解
源文链接 :https://github.com/Chalarangelo/30-seconds-of-code#anagrams-of-string-with-duplicates 该项目来自于 G ...
- 超实用的 JavaScript 代码片段( ES6+ 编写)
Array 数组 Array concatenation (数组拼接) 使用 Array.concat() ,通过在 args 中附加任何数组 和/或 值来拼接一个数组. const ArrayCon ...
- 精彩 JavaScript 代码片段
1. 根据给定的条件在原有的数组上,得到所需要的新数组. ——<JavaScript 王者归来> var a = [-1,-1,1,2,-2,-2,-3,-3,3,-3]; functio ...
- 一些非常有用的html,css,javascript代码片段(持久更新)
1.判断设备是否联网 if (navigator.onLine) { //some code }else{ //others code } 2.获取url的指定参数 function getStrin ...
- JavaScript 代码片段
1.无题 if (i && i.charAt(i.length - 1) == "/") { i = i.substr(0, i.length - 1) } 2.无 ...
- 常用javascript代码片段集锦
常用方法的封装 根据类名获取DOM元素 var $$ = function (className, element) { if (document.getElementsByClassName) { ...
- 实用Javascript代码片段
清除select下拉选项,添加并选择特点选项 $('#mySelect') .find('option') .remove() .end() .append('<option value=&qu ...
随机推荐
- Windows10 利用 Docker 配置 TensofFlow 深度学习工具
TensorFlow 这个不用多介绍了吧,大家都知道,Google的开源深度学习软件库,官网点这里:https://www.tensorflow.org/ 当然这个工具官方支持装在 Ubuntu 和 ...
- linux清理内存命令
1.清理前内存使用情况 free -m 2.开始清理 echo 1 > /proc/sys/vm/drop_caches3.清理后内存使用情况 free -m4.完成! 查看内存条数命令: # ...
- Android,LIstView中的OnItemClick点击无效的解决办法
在List_Item布局文件中的根节点加上如下背景标黄的这一行 <?xml version="1.0" encoding="utf-8"?> < ...
- 【学习笔记】Wireshark的用法
计算机网络课上,需要我们灵活运用网络协议分析仪wireshark,最近一直在看,感觉有点难,并不是软件本身操作难,而是看懂一大群包的含义难,这个难主要也因为它是全英文的~~.. 好了,大致总结一下,基 ...
- LNMP环境搭建
LNMP环境搭建 Linux + Nginx + MySQL + PHP PHP是一种脚本语言,当前中国乃至世界上使用PHP语言开发的网站非常普遍 Nginx是一个web服务软件,和apache是一类 ...
- 上一周,小白的我试着搭建了两个个人博客:在github和openshift上
上一周,突发奇想,想搭建个自己的博客. 由于是突发奇想,自然想先找免费的试试手.仔细搜索下,选定了目标Openshift和Github. Openshift 安装WordPress OpenShift ...
- mapred-site.xml 配置在线更新
环境:ibm jdk , cdh2.35.0.2 需求:更新mapred-site.xml 中的mapreduce.map.java.opts 和 mapreduce.reduce.java.opts ...
- Docker on Microsoft Azure
Docker蓬勃发展,如日中天.微软自然也不甘落后,且不说即将发布的.支持Docker技术的Windows Nano Server和Windows Server 2016.我们来看看在Microsof ...
- AI (Adobe Illustrator)详细用法(三)
本文主要是介绍和色彩相关的用法. 一.路径外观设置 1.设置描边粗细 手动输入20px 下拉选择 鼠标选中数值,按向上或向下的箭头调整 在右边的描边菜单中修改 Note:按住shift键,然后上下箭头 ...
- Zookeeper C API 指南三(回调函数)(转)
2013-02-21 12:54 by Haippy, 9237 阅读, 0 评论, 收藏, 编辑 接上一篇<Zookeeper C API 指南二(监视(Wathes), 基本常量和结构体介绍 ...