js实用方法记录-指不定哪天就会用到的js方法
2024-10-18 20:28:17
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){//有滚动条且滚动条距离顶部在四分之外
//显示回到顶部浮层什么的~~
}
最新文章
- C语言 第四章 分支结构练习
- python脚步管理工具supervisor=3.3.0的安装、使用。基于linux系统。
- HTML 5 音频
- Git——2
- FTP定时批量下载文件(SHELL脚本及使用方法&#160;)
- Hierarchical Softmax
- 回发或回调参数无效。在配置中使用 <;pages enableEventValidation=";true";/>; 或在页面中使用 <;%@ Page EnableEventValidation=";true"; %>; 启用了事件验证。
- [转]ionic Accordion list three levels
- (转)Thread.setDaemon设置说明
- 如何解决:新建Android程序的时候发生了找不到 \android-sdk-windows\tools\lib\proguard.cfg文件 的错误
- MEAN教程2-Nodejs安装
- 一. IntelliJ IDEA详细配置文档之初始环境搭建
- iOS项目之NSLog相关
- 剑指Offer 5. 用两个栈实现队列 (栈)
- Centos7 开放80,3306端口解决办法
- 简单说明一下Token ,Cookie,Session
- 006.FTP用户访问控制配置
- Android Build.VERSION.SDK_INT兼容介绍
- Bootstrap--响应式表格布局
- uva1416 dijkstra