vue-jquery
Version:
A Vue.js project
197 lines (192 loc) • 5.85 kB
JavaScript
let vueJquery = {};
vueJquery.install = function(Vue, options){
//DOM操作
//获取元素的宽高度
Vue.prototype.$getoffsetWidth = function(ele){
return ele.offsetWidth;
}
Vue.prototype.$getoffsetHeight = function(ele){
return ele.offsetHeight;
}
//获取屏幕可视区域宽高
Vue.prototype.$getClient = function(){
if(window.innerHeight !== undefined){
return {
"width": window.innerWidth,
"height": window.innerHeight
}
}else if(document.compatMode === "CSS1Compat"){
return {
"width": document.documentElement.clientWidth,
"height": document.documentElement.clientHeight
}
}else{
return {
"width": document.body.clientWidth,
"height": document.body.clientHeight
}
}
}
//设置style样式
Vue.prototype.$getStyle = function(obj, attr){
if(obj.currentStyle){ //行内样式
return obj.currentStyle[attr];
}else{ //嵌入样式,外联样式
return getComputedStyle(obj,false)[attr];
}
}
Vue.prototype.$setCss = function(ele, attr, value){
if(arguments.length == 3){//当参数个数为2时,使用设置css的方法
var i=0;
for(i=0;i<ele.length;i++){
ele[i].style[attr]=value;
}
}else if(arguments.length == 2){//只有一个参数时获取样式,或传入json
if(typeof attr=='string'){//attr为纯文字时,设置样式
return this.getStyle(ele[0],attr);
}else{//传入json,批量设置css样式
for(i=0;i<ele.length;i++){
var j='';
for(j in attr){
ele[i].style[j]=attr[j];
}
}
}
}
}
//设置attr属性
Vue.prototype.$setAttr = function(ele, attr, value){
if(arguments.length==3){//设置属性
var i=0;
for(i=0;i<ele.length;i++){
ele[i][attr]=value;
}
}else if(arguments.length==2){//获取属性
if(typeof attr=='string'){
return ele[0][attr];
}else{
for(i=0;i<ele.length;i++){
var j='';
for(j in attr){
ele[i][j]=attr[j];
}
}
}
}
return this;
}
//获取元素的offsetTop
Vue.prototype.$getTop = function(ele){ //ele为元素
var offset=ele.offsetTop,
me = this;
if(ele.offsetParent!=null) offset += me.$getTop(ele.offsetParent);
return offset;
}
//获取元素的offsetLeft
Vue.prototype.$getLeft = function(ele){ //ele为元素
var offset=ele.offsetLeft,
me = this;
if(ele.offsetParent!=null) offset += me.$getLeft(ele.offsetParent);
return offset;
}
//删除类样式
Vue.prototype.$removeClass = function(ele, str){ //ele为元素,str为类名
for(var j=0,lens = ele.length;j<lens;j++){
if (ele[j].className.indexOf(str) < 0) {
continue;
}else{
ele[j].className = ele[j].className.replace(str,'');
}
}
}
//添加类样式
Vue.prototype.$addClass = function(ele, str){
for(var j=0,lens=ele.length;j<lens;j++){
if(ele[j].className == '') {
ele[j].className += str
}else{
ele[j].className += ' '+str;
}
}
}
//BOM操作
//页面cookie
//创建cookie
Vue.prototype.$setCookie = function(name, value, exptime){ //name为cookie名字,value为cookie值,exptime为过期时间,设置5年后过期,相当于一直不过期
if (arguments.length == 3) { //当参数个数为3个时,加上过期时间
let exp = new Date();
exp.setTime(exptime);
//let exptime = new Date().getTime() + 1000*60*60*24*365*5; 5年时间
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}else {
document.cookie = name + "="+ escape (value);
}
}
//检查cookie
Vue.prototype.$getCookie = function(name){ //name为cookie名字
let arr,
reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg)){
return unescape(arr[2]);
}else{
return null;
}
}
//判断是否支持Storage
Vue.prototype.$issupportStorge = function(){
if(typeof(Storage) == "undefined"){
alert('当前浏览器不支持Web Storage');
return false;
}
}
//其它
//随机生成数字
Vue.prototype.$createRandom = function(num, from, to){ //num为随机生成多少个数,from从哪个数字开始,to到哪个数字结束
var arr=[];
var json={};
while(arr.length<num){
//产生单个随机数
var ranNum=Math.ceil(Math.random()*(to-from))+from;
//通过判断json对象的索引值是否存在 来标记 是否重复
if(!json[ranNum]){
json[ranNum]=1;
arr.push(ranNum);
}
}
return arr;
}
//把秒数转化成分秒形式
Vue.prototype.$formatSecond = function(time){ //time为时间秒数
var Time = parseInt(time),
Miuter = 0;
if(Time > 60) {
Miuter = '0' + parseInt(Time/60);
if(Miuter > 60) {
Miuter = parseInt(Time%60);
}
}
var result = time - Miuter*60;
if(result < 10) {
result = '0' + result;
}
if(Miuter > 0) {
result = Miuter+":"+result;
}
if(result <= 60) {
result = '00' + ":" +result;
}
return result;
}
//对象合并
Vue.prototype.$extend = function(target, source){
for (var obj in source) {
target[obj] = source[obj];
}
return target;
}
//点击返回return false函数
Vue.prototype.$back = function(){
return false;
}
}
export default vueJquery;