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 ...
随机推荐
- Spring学习之第一个AOP程序
IOC和AOP是Spring的两大基石,AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对 ...
- Windows 2008 R2 64位上安装wamp失败的原因
Exception Exception in module wampmanager.exe at 000F15A0... 因测试PHP程序需要,需要在windows系统上布署WAMP环境测试程序,对性 ...
- java 23 - 1 设计模式之工厂方法模式
转载: JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式)
- java 22 - 13 多线程之解决线程安全问题的实现方式2
上一章说了,解决线程安全问题的实现方式1是使用同步代码块 同时也知道了,同步代码块的锁对象是任意对象:(Object obj ; Demo d;)这些都行 那么,现在来说解决线程安全问题的实现方式2 ...
- Eclipse tooltip变黑的修正
- Js实现简单的省市级联的效果
需要注意的是当需要动态添加项的时候一定要先var newoption=new Option("项","值");然后再 select.options.add(ne ...
- JS获取当前页面的URL信息
设置或获取对象指定的文件名或路径. <script> alert(window.location.pathname) </script> 设置或获取整个 URL 为字符串. & ...
- AlertDialog禁止返回键
android 如何让dialog不消失,即使是用户按了返回键dialog也不消失 解决的问题:软件提示升级的dialog时候,用户有可能按了返回键,但是现在的需求是用户只能按"确定升级&q ...
- 【QCon笔记】Native 和 Web 融合
#main img{width:100%;} 简介 理清 Native 和 Web 的亮点和痛点,借鉴对方亮点解决自身的痛点,并给出淘系 App 在这些方面的实践. Mobile Web 的协作能力底 ...
- 未能正确加载包“Microsoft.Data.Entity.Design.Package.MicrosoftDataEntityDesignPackage
本文出处:http://blog.sina.com.cn/s/blog_6fe3efa301016i64.html vs 2005 ,vs 2008, vs 2010,安装后有时出现这个错误(我的机器 ...