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 ...
随机推荐
- top:failed tty get 错误
运行命令,ps -ef | grep test | grep -v test | awk '{ print $2 }' | xargs top -H -p 想看test的实时状态,结果报了错,查了一下 ...
- 企业邮箱在Android(安卓)系统手机上POP3/IMAP协议的设置方法
此处以三星(系统版本4.4.2)为例,介绍下使用安卓系统自带的客户端如何设置pop/imap协议方式方法 以下我们将使用test@zhuyuming.so 为测试案例,请您操作时更换成您自己的邮箱账号 ...
- POI读写Word docx文件
使用POI读写word docx文件 目录 1 读docx文件 1.1 通过XWPFWordExtractor读 1.2 通过XWPFDocument读 2 写docx ...
- JAVA-android 更改APP名称与图标
首先要在你的资源文件放入你想换的图标图片拖到drawable-XX文件夹下,然后你打开AndroidManifest.xml这个配置清单文件找到application标签里的这句android:ico ...
- NopCommerce 发布时 Could not load file or assembly 'file:///...\Autofac.3.5.2\lib\net40\Autofac.dll' or one of its dependencies
本文转自:http://www.nopcommerce.com/boards/t/33637/4-errors.aspx 问题: The 3.5 solution compiles fine, and ...
- Ahead-of-time compilation(AOT)
Ahead-of-time (AOT) compilation is the act of compiling a high-level programming language such as C ...
- [Flash 3D] 又是一个难题解决了。(Flash3D在android中运行)
做了一些away3D(4.1.6)+Flash cc,项目比较大,面数多达2000万个,发现电脑还有跑20多帧,可见away3d表现确实相当不错.想把这些东西放到手机上来看,却发现总是白屏,网上搜索了 ...
- ConcurrentHashMap是如何提高并发时的吞吐性能
为并发吞吐性能所做的优化 ConcurrentHashMap使用了一些技巧来获取高的并发性能,同时避免了锁.这些技巧包括: 为不同的Hash bucket(所谓hash bucket即不同范围的key ...
- log4j日志文件配置说明及使用
一.log4j.properties文件格式说明: log4j.rootLogger=info, stdout log4j.appender.stdout=org.apache.log4j.Co ...
- sql 盲注之正则表达式攻击
-----------------------------------------MYSQL 5+----------------------------------------- 我们都已经知道,在 ...