js实用方法记录-指不定哪天就会用到的js方法

常用或者不常用都有

判断是否在微信浏览器中

测试代码:isWeiXin()==false

/**
* 是否在微信中
*/
function isWeixin() { return (
navigator.userAgent
.toLowerCase()
.indexOf('micromessenger') > -1
)
}

全角转半角

测试代码:wholetoHalf('hello'')=='hello'

/**
* 转换全角字符串
* @param {string} txt 含全角字符串
*/
function wholetoHalf(txt){
if (!txt) {
return txt;
}
var tmp = "";
for (var i = 0; i < txt.length; i++) {
if (txt.charCodeAt(i) > 65280 && txt.charCodeAt(i) < 65375) {
tmp += String.fromCharCode(txt.charCodeAt(i) - 65248);
}
else if (txt.charCodeAt(i) == 12288) {
tmp += String.fromCharCode(32);
}
else {
tmp += String.fromCharCode(txt.charCodeAt(i));
}
}
return tmp;
}

生成Guid


/**
* 生成Guid
*/
function genGuid() {
function S4() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
return (S4() + S4() + "-" + S4() + "-4" + S4().substr(0, 3) + "-" + S4() + "-" + S4() + S4() + S4()).toLowerCase();
}

获取滚动条距顶部距离

/**
* 获取滚动条距顶部距离
*/
function getScrollTop() {
var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
if (document.body) {
bodyScrollTop = document.body.scrollTop;
}
if (document.documentElement) {
documentScrollTop = document.documentElement.scrollTop;
}
scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
return scrollTop;
}

获取滚动条高度

/**
* 获取滚动条高度 默认
*/
function getScrollHeight() {
var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
if (document.body) {
bodyScrollHeight = document.body.scrollHeight;
}
if (document.documentElement) {
documentScrollHeight = document.documentElement.scrollHeight;
}
scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
return scrollHeight;
}

通过判断滚动条位置操作元素

if(getScrollHeight() > document.documentElement.clientHeight
&& getScrollTop()>getScrollHeight()/4){//有滚动条且滚动条距离顶部在四分之外
//显示回到顶部浮层什么的~~
}

最新文章

  1. C语言 第四章 分支结构练习
  2. python脚步管理工具supervisor=3.3.0的安装、使用。基于linux系统。
  3. HTML 5 音频
  4. Git——2
  5. FTP定时批量下载文件(SHELL脚本及使用方法&#160;)
  6. Hierarchical Softmax
  7. 回发或回调参数无效。在配置中使用 &lt;pages enableEventValidation=&quot;true&quot;/&gt; 或在页面中使用 &lt;%@ Page EnableEventValidation=&quot;true&quot; %&gt; 启用了事件验证。
  8. [转]ionic Accordion list three levels
  9. (转)Thread.setDaemon设置说明
  10. 如何解决:新建Android程序的时候发生了找不到 \android-sdk-windows\tools\lib\proguard.cfg文件 的错误
  11. MEAN教程2-Nodejs安装
  12. 一. IntelliJ IDEA详细配置文档之初始环境搭建
  13. iOS项目之NSLog相关
  14. 剑指Offer 5. 用两个栈实现队列 (栈)
  15. Centos7 开放80,3306端口解决办法
  16. 简单说明一下Token ,Cookie,Session
  17. 006.FTP用户访问控制配置
  18. Android Build.VERSION.SDK_INT兼容介绍
  19. Bootstrap--响应式表格布局
  20. uva1416 dijkstra

热门文章

  1. Oracle语言环境变量配置
  2. webpack 模块方法
  3. 观察者模式C#实现实例(一)
  4. VS2015一新建项目就出现未将对象引用设置到对象的实例怎么办?[z]
  5. Top值
  6. 天然气管道Gaz[POI2007]
  7. 第5-7次OO作业总结分析
  8. Python之路(第三十八篇) 并发编程:进程同步锁/互斥锁、信号量、事件、队列、生产者消费者模型
  9. 原来这就是 UI 设计师的门槛
  10. jquery datatables+MVC+WCF