常用js代码
2018-09-01 本文已影响0人
zhanggongzi
/**
*鼠标滚动
*/
$(document).on("mousewheel DOMMouseScroll", function(event){
//mousewheel/wheelDelta-IE/Opera/Chrome
//DOMMouseScroll/detail-Firefox
var wheelVal = (event.originalEvent.wheelDelta && (event.originalEvent.wheelDelta > 0 ? 1 : -1)) || (event.originalEvent.detail && (event.originalEvent.detail > 0 ? 1 : -1));
if(wheelVal > 0){
console.log("is top");
}else {
console.log("is down");
}
});
/**
*鼠标位置
*/
var mousePosition = function(event){
if(event.pageX || event.pageY) return {x:event.pageX,y:event.pageY};
return {
x:event.clientX + document.body.scrollLeft - document.body.clientLeft,
y:event.clientY + document.body.scrollTop - document.body.clientTop
}
}
document.onmousemove = function(event){
var ev = event || window.event;
var position = mousePosition(ev);
//console.clear();
//console.log("x:"+position.x+",y:"+position.y);
}
/**
*禁止手机端页面滚动
*/
document.addEventListener("touchmove", function(event){
event.preventDefault();
});
/**
*阻止默认行为
*/
$(".xx").on("click", function(event){
event.preventDefault();
})
/**
*阻止冒泡
*/
$(".xx").on("click", function(event){
event.stopPropagation();
})
/**
*强制手机端横屏显示
*/
$(window).on("orientationchange", function(event){
if(event.orientation == 'portrait') $("body").css("transform","rotate(90deg)");
$("body").css("transform","rotate(0deg)");
})
/**
*电脑端页面全屏
*/
var fullScreen = function(ele){
if(ele.requestFullscreen){
ele.requestFullscreen();
}else if(ele.mozRequestFullScreen){
ele.mozRequestFullScreen();
}else if(ele.webkitRequestFullScreen){
ele.webkitRequestFullScreen();
}else if(ele.msRequestFullscreen){
ele.msRequestFullscreen();
}
}
//<button value="btn" onclick="fullScreen(document.documentElement)">btn</button>
/**
*倒计时验证码
*/
var sendCode =function (counter,ele,callback) {
var self = this;
self.ele = ele;
//var lastTime = new Date();
//var thisTime, diff;
self.counter = counter || 60;
self.timer = setInterval(function () {
//移动端考虑锁屏,用时间差
//锁屏之后,定时器会停止走动
//锁屏时间过久,可能会出现counter<0的情况
if (self.counter <= 0) {
self.counter = 0;
self.ele.disabled = false;
self.ele.innerHTML = "获取验证码";
self.timer && clearInterval(self.timer);
return;
}else{
self.ele.disabled = true;
self.ele.innerHTML = self.counter+"s";
}
//thisTime = new Date();
//diff = thisTime - lastTime;
//lastTime = thisTime;
//self.counter -= (diff / 1000) >> 0;
self.counter--;
}, 1000);
//发送验证码请求
callback && callback();
}
//sendCode(6,document.getElementById("send-code"),function(){console.log("done")});
/**
*判断是否在浏览器底部
*/
var isBrowserEnd = function(){
var _clientHeight = window.innerHeight || document.documentElement.clientHeight,
_winHeight = window.pageYOffset || document.documentElement.scrollTop,
_docHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
return _clientHeight + _winHeight == _docHeight;
}
//jq
var isBrowserEnd = function(){
return $(window).height() + $(window).scrollTop() == $(document).height();
}