js

6/4/2019 vuejs

# 模拟 a 标签下载文件

function downloadUrlFile(path, filename) {
  let isSupportDownload = 'download' in document.createElement('a')
  if (isSupportDownload) {
    let a = document.createElement('a')
    a.href = path
    a.download = filename
    a.style.display = 'none'
    // 触发点击
    document.body.appendChild(a)
    a.click()
    document.body.removeChild(a)
  } else {
    window.open(path)
  }
}

# 获取 Ajax 头部信息

//获取响应头全部参数信息
console.log(xhr.getAllResponseHeaders())
//获取指定参数信息
console.log(xhr.getResponseHeader('Date'))

# 动态创建 iframe

//创建div
var div = document.createElement('div')
div.id = 'checkLayer'
//设置div的css样式
div.style.cssText =
  'display:none;width:100%;height:100%;position: absolute;top: 0;left:0;background-color: #fff;'
//在body内创建一个div
document.body.appendChild(div)
//创建iframe
var iframe = document.createElement('iframe')
//设置iframe属性
iframe.id = 'checkIframe'
iframe.name = 'checkIframe'
iframe.src = 'check.html'
iframe.height = '100%'
iframe.width = '100%'
iframe.frameBorder = '0'
//设置iframe样式
//iframe.style.cssText = 'background-color: #fff;';
//在div内创建iframe
div.appendChild(iframe)

# ul li 上下滚动

var marginTop = 0
setInterval(function() {
  $('.winMsgList li:first').animate({ marginTop: marginTop-- }, 10, function() {
    if (!$(this).is(':animated')) {
      //判断是否有一个动画节点
      if (-marginTop >= $(this).height()) {
        //判断移出位置是否超过高度
        $(this).css('margin', '0')
        $(this).appendTo($('.winMsgList')) //把第一个节点移到ul后面
        marginTop = 0 //重新设置移动数值
      }
    }
  })
}, 100)
function autoScroll() {
  var scrollDiv = $('.winMsg'),
    $ul = scrollDiv.find('ul'),
    $li = scrollDiv.find('li'),
    $length = $li.length,
    $liHeight = $li.height(),
    num = 0

  if (scrollDiv.length == 0) {
    return
  }

  if ($length > 1) {
    $ul.append($li.eq(0).clone())
    setInterval(function() {
      num++
      $ul
        .addClass('animate')
        .css('-webkit-transform', 'translateY(-' + $liHeight * num + 'px)')
      setTimeout(function() {
        if (num == $length) {
          $ul.removeClass('animate').css('-webkit-transform', 'translateY(0)')
          num = 0
        }
      }, 300)
    }, 3000)
  }
}
autoScroll()

# 数字每三位加个分隔符

var arr = 12324566
document.write(arr.toLocaleString())
//12,324,566
function seperate(num, n) {
  return num
    .toString()
    .replace(new RegExp('\\B(?=(\\d{' + n + '})+(?!\\d))', 'g'), ',')
}

# 手机号码 344 分隔

var num = '12345678901'
console.log(num.replace(/\s/g, '').replace(/(^\d{3}|\d{4}\B)/g, '$1 '))

# 保留 0-100 之内的数字,并保留 2 位小数

^(((\d|[1-9]\d)(.\d{1,2})?)|100|100.0|100.00)$

# 根据当前年月,获取最近的 12 个月

var last_year_month = function(day) {
  var d = new Date(day)
  var result = []
  for (var i = 0; i < 12; i++) {
    // 循环12次
    var m = d.getMonth() + 1 // 当前月份
    m = m < 10 ? '0' + m : m // 补全零
    result.push(d.getFullYear() + '-' + m) // 放值 todo:自定义格式
    d.setMonth(d.getMonth() - 1) // 下一个月
  }
  return result
}
last_year_month('2018-10-10')

# 字符串转换为数字

var a = '123'
console.log(+a) // 123
console.log(typeof +a) // number
// 同样可用于日期转换为数值:
var b = +new Date() // 1468545682168

# 数值向下取整

var a = ~~3.14 // 3
var b = 3.14 >> 0 // 3
var c = 3.14 | 0 // 3

# 字符串转换为数值并取整

var a = '3.14' | 0 // 3
var b = '3.14' ^ 0 // 3
Last Updated: 4/15/2020, 2:26:45 PM