常用的代码片段

截取url地址栏的参数(单个参数)

getUrlParam(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
                var r = window.location.search.substr(1).match(reg);  //匹配目标参数
                if (r != null) return unescape(r[2]); return null; //返回参数值
            }

截取url多个参数

function GetRequest(url) {
      //url = url.replace('#/','');//去掉末尾的#/
      let theRequest = {};
      if (url.indexOf("?") != -1) {
          let str = url.substring(url.indexOf("?")+1);
          let strs = str.split("&");
          for(let i = 0; i < strs.length; i ++) {
              theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
          }
      }
      return theRequest;
    },
    var url = 'https://www.baidu.com/s?id=111&name=yourname';
    GetRequest(url)

vue中超出两行显示省略号

display: -webkit-box;
/* -webkit-box-orient: vertical; */
/*! autoprefixer: off */
-webkit-box-orient: vertical;
/* autoprefixer: on */
-webkit-line-clamp: 2;
overflow: hidden;

px转rem

document.documentElement.style.fontSize = document.documentElement.clientWidth/6.4+"px";//设置1rem=100px

原生Ajax封装

// 原生ajax的封装(params里面有三个属性type,url,data)
        function myAjax(params,fn) {
            let xhr = new XMLHttpRequest();
            if (params.type=='get'){
                xhr.open('GET', params.url, true);
                xhr.send(null);
            }else if(type=='post'){
                xhr.open("POST", params.url, true);
                xhr.send(params.data);
                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            }
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && (xhr.status == 200)) {
                    fn.call(this,xhr.responseText)
                }
            };
        }

纵向滚动不显示滚动条(css代码)


    /*text-align: left;*/   
    overflow: hidden;
    text-overflow: ellipsis;
    overflow-y: auto;

flex实现左右贴边,中间间隙自定义,左右盒子宽度自定义;


    
      
    
    
      
    
 

.threedBox{
  display: flex;
  flex-wrap: nowrap;
  justify-content:space-between;
}
.threedBox .leftBox{
  margin-right: 30rpx;
}
.threedBox .rightBox{
  width: 60%;
}
/*
下面是左右中间的等距离
*/
.threedBox{
  display: flex;
  flex-wrap: wrap;
  justify-content:center;
}
.leftBox,.rightBox{
  width: 50%;
  box-sizing: border-box;
  padding: 30rpx 0;
}
.leftBox{
  padding-left: 30rpx;
  padding-right: 15rpx;
}
.rightBox{
  padding-left: 15rpx;
  padding-right: 30rpx;
}
.between{
  background-color: #fff;
  border-radius: 8rpx;
}

判断当前时间是否再某个时间段

compareTime() {
    let date = new Date();
    let hour = parseInt(date.getHours());
    let minute = parseInt(date.getMinutes());
    if ((17 > hour && hour>=9) || (hour == 17 &&  minute< 30)){
      //在9-17:30时间段
    }else{
      //不在9-17:30时间段   
    }
  }

vue中引入图片

 { label: "最大风速", key: "ws_max", isCheck: false,unit:'m/s',icon:require('../assets/images/icon_fengsu.png')},

 

你可能感兴趣的:(组件使用)