function add ( arg1, arg2 ) {
var r1, r2, m;
try { r1 = arg1. toString ( ) . split ( "." ) [ 1 ] . length } catch ( e) { r1 = 0 }
try { r2 = arg2. toString ( ) . split ( "." ) [ 1 ] . length } catch ( e) { r2 = 0 }
m = Math. pow ( 10 , Math. max ( r1, r2) ) ;
return ( arg1 * m + arg2 * m) / m;
}
# 解决小数相减的误差
` ` `js
function accSub ( arg1, arg2 ) {
var r1, r2, m, n;
try { r1 = arg1. toString ( ) . split ( "." ) [ 1 ] . length } catch ( e) { r1 = 0 }
try { r2 = arg2. toString ( ) . split ( "." ) [ 1 ] . length } catch ( e) { r2 = 0 }
m = Math. pow ( 10 , Math. max ( r1, r2) ) ;
n = ( r1 >= r2) ? r1 : r2;
return ( ( arg1 * m - arg2 * m) / m) . toFixed ( n) ;
}
- javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。
function accDiv ( arg1, arg2 ) {
var t1 = 0 , t2 = 0 , r1, r2;
try { t1 = arg1. toString ( ) . split ( "." ) [ 1 ] . length } catch ( e) { }
try { t2 = arg2. toString ( ) . split ( "." ) [ 1 ] . length } catch ( e) { }
with ( Math) {
r1 = Number ( arg1. toString ( ) . replace ( "." , "" ) )
r2 = Number ( arg2. toString ( ) . replace ( "." , "" ) )
return ( r1 / r2) * pow ( 10 , t2 - t1) ;
}
}
- javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。
function accMul ( arg1, arg2 ) {
var m = 0 , s1 = arg1. toString ( ) , s2 = arg2. toString ( ) ;
try { m += s1. split ( "." ) [ 1 ] . length } catch ( e) { }
try { m += s2. split ( "." ) [ 1 ] . length } catch ( e) { }
return Number ( s1. replace ( "." , "" ) ) * Number ( s2. replace ( "." , "" ) ) / Math. pow ( 10 , m)
}
function debounce ( func, wait, immediate ) {
let timeout;
return function ( ) {
let context = this ;
let args = arguments;
if ( timeout) clearTimeout ( timeout) ;
if ( immediate) {
var callNow = ! timeout;
timeout = setTimeout ( ( ) => {
timeout = null ;
} , wait)
if ( callNow) func . apply ( context, args)
}
else {
timeout = setTimeout ( function ( ) {
func . apply ( context, args)
} , wait) ;
}
}
}
function throttle ( func, wait, type ) {
if ( type === 1 ) {
let previous = 0 ;
} else if ( type === 2 ) {
let timeout;
}
return function ( ) {
let context = this ;
let args = arguments;
if ( type === 1 ) {
let now = Date. now ( ) ;
if ( now - previous > wait) {
func . apply ( context, args) ;
previous = now;
}
} else if ( type === 2 ) {
if ( ! timeout) {
timeout = setTimeout ( ( ) => {
timeout = null ;
func . apply ( context, args)
} , wait)
}
}
}
}
upload ( file ) {
var a = file. file;
var b = window. URL ;
var c = b. createObjectURL ( a) ;
console. log ( c) ;
} ,
function dataType ( value ) {
if ( Object . prototype. toString . call ( value) === "[object Array]" ) {
console. log ( 'value是数组' ) ;
} else if ( Object . prototype. toString . call ( value) === '[object Object]' ) {
console. log ( 'value是对象' ) ;
}
}
function deepClone ( target ) {
let result;
if ( typeof target === 'object' ) {
if ( Array. isArray ( target) ) {
result = [ ] ;
for ( let i in target) {
result. push ( deepClone ( target[ i] ) )
}
} else if ( target=== null ) {
result = null ;
} else if ( target. constructor=== RegExp) {
result = target;
} else {
result = { } ;
for ( let i in target) {
result[ i] = deepClone ( target[ i] ) ;
}
}
} else {
result = target;
}
return result;
}
var beforePrint = function ( ) {
console. log ( 'Functionality to run before printing.' ) ;
} ;
var afterPrint = function ( ) {
console. log ( 'Functionality to run after printing' ) ;
} ;
if ( window. matchMedia) {
var mediaQueryList = window. matchMedia ( 'print' ) ;
mediaQueryList. addListener ( function ( mql ) {
if ( mql. matches) {
beforePrint ( ) ;
} else {
afterPrint ( ) ;
}
} ) ;
}
window. onbeforeprint = beforePrint;
window. onafterprint = afterPrint;
- 使用 xlsx. core. min. js 插件
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< meta name= "viewport" content= "width=device-width, initial-scale=1.0" >
< title> 导出excel中的数据< / title>
< / head>
< body>
< input type= "file" value= " " onchange= "importXlsx(this)" / >
< script src= "https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.14.0/xlsx.core.min.js" > < / script>
< script>
function importXlsx ( e ) {
const files = e. files;
let i, f;
for ( i = 0 , f = files[ i] ; i != files. length; ++ i) {
let reader = new FileReader ( ) ;
let name = f. name;
reader. onload = function ( e ) {
let data = e. target. result;
let workbook = XLSX . read ( data, {
type: typeof FileReader !== "undefined" && ( FileReader . prototype || { } ) . readAsBinaryString ?
'binary' : 'array'
} ) ;
for ( let sheet in workbook. Sheets) {
if ( workbook. Sheets. hasOwnProperty ( sheet) ) {
fromTo = workbook. Sheets[ sheet] [ '!ref' ] ;
let xlsxData = XLSX . utils. sheet_to_json ( workbook. Sheets[ sheet] ) ;
console. log ( xlsxData) ;
var arr = [ ] ;
console. log ( JSON . stringify ( arr) ) ;
} ;
} ;
}
reader. readAsBinaryString ( f) ;
}
}
< / script>
< / body>
< / html>
window. location. hash = "no-back" ;
window. location. hash = "Again-No-back-button" ;
window. onhashchange = function ( ) { window. location. hash = "no-back" ; }
function getUrlParms ( ) {
var args = new Object ( ) ;
var query = location. search. substring ( 1 ) ;
var pairs = query. split ( "&" ) ;
for ( var i = 0 ; i < pairs. length; i++ ) {
var pos = pairs[ i] . indexOf ( '=' ) ;
if ( pos == - 1 ) continue ;
var argname = pairs[ i] . substring ( 0 , pos) ;
var value = pairs[ i] . substring ( pos + 1 ) ;
args[ argname] = unescape ( value) ;
}
return args;
}
let a = document. createElement ( "a" ) ;
a. setAttribute ( "href" , url) ;
a. setAttribute ( "target" , "_blank" ) ;
let clickEvent = document. createEvent ( "MouseEvents" ) ;
clickEvent. initEvent ( "click" , true , true ) ;
a. dispatchEvent ( clickEvent) ;
export function getTimer ( type, timer ) {
if ( ! [ 'date' , 'time' , 'dateTime' , 'cdate' , 'ctime' , 'cdateTime' , 'y' , 'm' , 'd' ] . includes ( type) ) new Error ( 'getTimer type error' ) ;
const time = timer ? new Date ( timer) : new Date ( ) ;
var y = time. getFullYear ( ) ;
var m = ( time. getMonth ( ) + 1 ) . toString ( ) . padStart ( 2 , 0 ) ;
var d = time. getDate ( ) . toString ( ) . padStart ( 2 , 0 ) ;
var h = time. getHours ( ) . toString ( ) . padStart ( 2 , 0 ) ;
var mm = time. getMinutes ( ) . toString ( ) . padStart ( 2 , 0 ) ;
var s = time. getSeconds ( ) . toString ( ) . padStart ( 2 , 0 ) ;
const obj = {
'y' : y,
'm' : m,
'd' : d,
'date' : y + "-" + m + "-" + d,
'time' : h + "-" + mm + "-" + s,
'dateTime' : y + "-" + m + "-" + d + " " + h + ":" + mm + ":" + s,
'cdate' : y + '年' + m + '月' + d + '日' ,
'ctime' : h + '时' + mm + '分' + s + '秒' ,
'cdateTime' : y + '年' + m + '月' + d + '日' + " " + h + '时' + mm + '分' + s + '秒'
}
return obj[ type]
}
< input type= "file" name= "imgId" id= "imgId" value= "" / >
imgTypeSize ( 'imgId' , 0.5 )
function imgTypeSize ( FileId, maxsize ) {
var imgFile = document. getElementById ( FileId) . files[ 0 ] ;
if ( imgFile. name == "" ) {
alert ( "请上传图片" ) ;
return false ;
} else {
var imgStr = / \.(jpg|jpeg|png|bmp|BMP|JPG|PNG|JPEG)$ / ;
if ( ! imgStr. test ( imgFile. name) ) {
alert ( "文件不是图片类型" ) ;
return false ;
} else {
var imagSize = imgFile. size;
if ( imagSize < ( 1024 * 1024 * maxsize) ) {
return true ;
} else {
alert ( imgFile. name + "大小不能超过" + maxsize + "M" ) ;
return false ;
}
}
}
} ;
export function getTimed ( type, timestamp ) {
if ( ! [ 'd' , 'd-h' , 'd-h-m' , 'd-h-m-s' ] . includes ( type) ) new Error ( 'getTimer type error' ) ;
if ( ! isNumber ( Number ( timestamp) ) || timestamp == 0 ) return '--'
var seconds = Math. floor ( timestamp / 1000 % 60 ) ;
var minutes = Math. floor ( timestamp / 1000 / 60 % 60 ) ;
var hours = Math. floor ( timestamp / 1000 / 60 / 60 % 24 ) ;
var days = Math. floor ( timestamp / 1000 / 60 / 60 / 24 ) ;
const obj = {
'd' : days + '天' ,
'd-h' : days + '天' + hours + '小时' ,
'd-h-m' : days + '天' + hours + '小时' + minutes + '分钟' ,
'd-h-m-s' : days + '天' + hours + '小时' + minutes + '分钟' + seconds + '秒'
}
return obj[ type]
}
let map = new Map ( ) ;
for ( let item of res. data) {
map. set ( item. nodeId, item) ;
}
let list = [ ... map. values ( ) ]
export function exportExcel ( data, fileName, typeInfo ) {
const link = document. createElement ( 'a' )
var ext = / \.[^\.]+$ / . exec ( fileName) ;
const blob = new Blob ( [ data] , {
type: typeInfo ? getResponseType ( ext[ 0 ] . split ( '.' ) [ 1 ] ) : 'image/png'
} )
link. style. display = 'none'
link. href = URL . createObjectURL ( blob)
link. download = fileName
document. body. appendChild ( link)
link. click ( )
document. body. removeChild ( link)
}
function getResponseType ( ext ) {
var responseType = {
'abw' : 'application/x-abiword' ,
'arc' : 'application/x-freearc' ,
'avi' : 'video/x-msvideo' ,
'azw' : 'application/vnd.amazon.ebook' ,
'bin' : 'application/octet-stream' ,
'bmp' : 'image/bmp' ,
'bz' : 'application/x-bzip' ,
'bz2' : 'application/x-bzip2' ,
'csh' : 'application/x-csh' ,
'css' : 'text/css' ,
'csv' : 'text/csv' ,
'doc' : 'application/msword' ,
'docx' : 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ,
'eot' : 'application/vnd.ms-fontobject' ,
'epub' : 'application/epub+zip' ,
'gif' : 'image/gif' ,
'htm' : 'text/html' ,
'html' : 'text/html' ,
'ico' : 'image/vnd.microsoft.icon' ,
'ics' : 'text/calendar' ,
'jar' : 'application/java-archive' ,
'jpeg' : 'image/jpeg' ,
'jpg' : 'image/jpeg' ,
'js' : 'text/javascript' ,
'json' : 'application/json' ,
'jsonld' : 'application/ld+json' ,
'mid' : 'audio/midi audio/x-midi' ,
'midi' : 'audio/midi audio/x-midi' ,
'mjs' : 'text/javascript' ,
'mp3' : 'audio/mpeg' ,
'mpeg' : 'video/mpeg' ,
'mpkg' : 'application/vnd.apple.installer+xml' ,
'odp' : 'application/vnd.oasis.opendocument.presentation' ,
'ods' : 'application/vnd.oasis.opendocument.spreadsheet' ,
'odt' : 'application/vnd.oasis.opendocument.text' ,
'oga' : 'audio/ogg' ,
'ogv' : 'video/ogg' ,
'ogx' : 'application/ogg' ,
'otf' : 'font/otf' ,
'png' : 'image/png' ,
'pdf' : 'application/pdf' ,
'ppt' : 'application/vnd.ms-powerpoint' ,
'pptx' : 'application/vnd.openxmlformats-officedocument.presentationml.presentation' ,
'rar' : 'application/x-rar-compressed' ,
'rtf' : 'application/rtf' ,
'sh' : 'application/x-sh' ,
'svg' : 'image/svg+xml' ,
'swf' : 'application/x-shockwave-flash' ,
'tar' : 'application/x-tar' ,
'gz' : 'application/x-tar' ,
'tif' : 'image/tiff' ,
'tiff' : 'image/tiff' ,
'ttf' : 'font/ttf' ,
'txt' : 'text/plain' ,
'vsd' : 'application/vnd.visio' ,
'wav' : 'audio/wav' ,
'weba' : 'audio/webm' ,
'webm' : 'video/webm' ,
'webp' : 'image/webp' ,
'woff' : 'font/woff' ,
'woff2' : 'font/woff2' ,
'xhtml' : 'application/xhtml+xml' ,
'xls' : 'application/vnd.ms-excel' ,
'xlsx' : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ,
'xml' : 'text/xml' ,
'xul' : 'application/vnd.mozilla.xul+xml' ,
'zip' : 'application/zip' ,
'3gp' : 'video/3gpp' ,
'3g2' : 'video/3gpp2' ,
'7z' : 'application/x-7z-compressed' ,
} ;
return responseType[ ext] ;
}
. text {
margin: 5 px 10 px 15 px 20 px;
padding: 25 px 10 px 5 px 3 px;
}
< div class = "container" >
< p class = "text" > text< / p>
< / div>
var textNode = document. querySelector ( '.text' )
function getStyle ( obj, attr ) {
if ( obj. currentStyle) {
return obj. currentStyle[ attr] ;
}
else {
return document. defaultView. getComputedStyle ( obj, null ) [ attr] ;
}
}
var width = getStyle ( textNode, 'width' )
var marginTop = getStyle ( textNode, 'marginTop' )
function ajaxFun ( opt ) {
var type = "post" ;
var asyn = true ;
var withCredentials = false ;
var data = { } ;
var url = "" ;
var outtime = 10000 ;
var success = function ( ) { } ;
var error = function ( ) { } ;
var onouttime = function ( ) { } ;
if ( ! opt. url) {
console. log ( '必填访问地址' ) ;
return false ;
}
if ( opt. type) { type = opt. type; }
if ( opt. asyn) { asyn = opt. asyn; }
if ( opt. withCredentials) { withCredentials = opt. withCredentials; }
if ( opt. data) { data = opt. data; }
if ( opt. url) { url = opt. url; }
if ( opt. outtime) { outtime = opt. outtime; }
if ( opt. success) { success = opt. success; }
if ( opt. error) { error = opt. error; }
if ( opt. onouttime) { onouttime = opt. onouttime; }
var datastr = "" ;
for ( key in data) {
if ( datastr) { datastr += "&" ; }
datastr = datastr + ` ${ key} = ${ data[ key] } ` ;
}
var xmlHttp = new XMLHttpRequest ( ) ;
xmlHttp. open ( type, url, asyn) ;
if ( withCredentials) {
xmlHttp. withCredentials = withCredentials;
}
xmlHttp. setRequestHeader ( "Content-Type" , "application/x-www-form-urlencoded" ) ;
xmlHttp. send ( datastr) ;
xmlHttp. onreadystatechange = function ( ) {
if ( this . readyState == 4 && this . status == 200 ) {
success ( this . responseText) ;
}
} ;
xmlHttp. ontimeout = function ( ) {
onouttime ( ) ;
} ;
xmlHttp. onerror = function ( ) {
error ( ) ;
console. log ( arguments) ;
}
}
ajaxFun ( {
type : "post" ,
asyn : true ,
withCredentials : true ,
data : { "id" : 3 , "proid" : 5 } ,
url : "api.txt" ,
success : function ( re ) {
console. log ( re) ;
} ,
outtime : 2000 ,
onouttime : function ( ) {
console. log ( '超时了' ) ;
}
} )
< ! DOCTYPE html>
< html lang= "en" >
< head>
< meta charset= "UTF-8" >
< meta name= "viewport" content= "width=device-width, initial-scale=1.0" >
< title> 轮播图< / title>
< / head>
< style>
* {
margin: 0 ;
padding: 0 ;
list- style: none;
}
. clearfix: : after {
content: "" ;
display: block;
clear: both;
}
. focus {
width: 1200 px;
height: 800 px;
margin: 0 auto;
position: relative;
overflow: hidden;
box- sizing: border- box;
border: 5 px solid #ccc;
}
. focus- ul {
height: 800 px;
position: absolute;
top: 0 ;
left: 0 ;
}
. focus- ul li {
cursor: pointer;
width: 1200 px;
height: 800 px;
float: left;
}
. left {
display: block;
position: absolute;
top: 50 % ;
left: 0 ;
transform: translateY ( - 50 % ) ;
font- size: 24 px;
padding: 15 px 30 px;
background- color: rgba ( 0 , 0 , 0 , .3 ) ;
color: #fff;
z- index: 9 ;
display: none;
}
. right {
position: absolute;
top: 50 % ;
right: 0 ;
transform: translateY ( - 50 % ) ;
font- size: 24 px;
padding: 15 px 30 px;
background- color: rgba ( 0 , 0 , 0 , .3 ) ;
color: #fff;
z- index: 9 ;
display: none;
}
. focus- ol {
display: inline- block;
position: absolute;
bottom: 10 px;
left: 50 % ;
transform: translateX ( - 50 % ) ;
}
. focus- ol li {
cursor: pointer;
display: inline- block;
width: 10 px;
height: 10 px;
margin: 0 10 px;
border: 1 px solid #fff;
border- radius: 50 % ;
}
. current {
background- color: #fff;
}
< / style>
< body>
< div class = "focus" >
< ! -- 左右按钮 -- >
< a class = "left" href= "javascript:;" > & lt; < / a>
< a class = "right" href= "javascript:;" > & gt; < / a>
< ! -- 内容 -- >
< ul class = "clearfix focus-ul" >
< li> < img src= "./img/banner1.jpg" alt= "" > < / li>
< li> < img src= "./img/banner2.jpg" alt= "" > < / li>
< li> < img src= "./img/banner3.jpeg" alt= "" > < / li>
< li> < img src= "./img/banner4.jpg" alt= "" > < / li>
< / ul>
< ! -- 小圆圈 -- >
< ol class = "focus-ol" > < / ol>
< / div>
< script>
var focus = document. querySelector ( '.focus' ) ;
var left = document. querySelector ( '.left' ) ;
var right = document. querySelector ( '.right' ) ;
var focus_ul = document. querySelector ( '.focus-ul' ) ;
var focus_ul_li = document. querySelectorAll ( '.focus-ul li' )
var focus_ol = document. querySelector ( '.focus-ol' ) ;
var focus_ol_li;
var focusWidth = focus. offsetWidth;
var index = 0 ;
var circle = 0 ;
var timer;
var flag = true ;
ulLength ( ) ;
showBtn ( ) ;
circles ( ) ;
circlesClick ( ) ;
circlesClickMove ( ) ;
cloneImg ( ) ;
rightBtn ( ) ;
leftBtn ( ) ;
creationInterval ( ) ;
function ulLength ( ) {
focus_ul. style. width = focusWidth * focus_ul_li. length + focusWidth + 'px' ;
}
function showBtn ( ) {
focus. addEventListener ( 'mouseenter' , function ( ) {
left. style. display = 'block' ;
right. style. display = 'block' ;
clearInterval ( timer) ;
} ) ;
focus. addEventListener ( 'mouseleave' , function ( ) {
left. style. display = 'none' ;
right. style. display = 'none' ;
creationInterval ( ) ;
} ) ;
}
function circles ( ) {
for ( let i = 0 ; i < focus_ul. children. length; i++ ) {
let li = document. createElement ( 'li' ) ;
focus_ol. appendChild ( li) ;
}
focus_ol. children[ 0 ] . classList. add ( 'current' ) ;
focus_ol_li = focus_ol. children;
}
function circlesClick ( ) {
for ( let i = 0 ; i < focus_ol_li. length; i++ ) {
focus_ol_li[ i] . addEventListener ( 'click' , function ( ) {
for ( let j = 0 ; j < focus_ol_li. length; j++ ) {
focus_ol_li[ j] . classList. remove ( 'current' ) ;
}
this . classList. add ( 'current' ) ;
} )
}
}
function circlesClickMove ( ) {
for ( let i = 0 ; i < focus_ol_li. length; i++ ) {
focus_ol_li[ i] . addEventListener ( 'click' , function ( ) {
animate ( focus_ul, - i * focusWidth)
} )
}
}
function cloneImg ( ) {
let first_img = focus_ul_li[ 0 ] . cloneNode ( true ) ;
focus_ul. appendChild ( first_img) ;
}
function rightBtn ( ) {
right. addEventListener ( 'click' , function ( ) {
rightBtnMove ( ) ;
} )
}
function rightBtnMove ( ) {
if ( flag) {
flag = false ;
if ( index >= focus_ul_li. length) {
index = 0 ;
focus_ul. style. left = 0 ;
}
index++ ;
animate ( focus_ul, - index * focusWidth, function ( ) {
flag = true ;
} ) ;
rightBtnClickCirclesConversion ( ) ;
}
}
function leftBtn ( ) {
left. addEventListener ( 'click' , function ( ) {
leftBtnMove ( ) ;
} )
}
function leftBtnMove ( ) {
if ( flag) {
flag = false ;
if ( index == 0 ) {
index = focus_ul_li. length;
focus_ul. style. left = - index * focusWidth + 'px' ;
}
index-- ;
animate ( focus_ul, - index * focusWidth, function ( ) {
flag = true ;
} ) ;
leftBtnClickCirclesConversion ( ) ;
}
}
function rightBtnClickCirclesConversion ( ) {
circle++ ;
if ( circle >= focus_ol_li. length) {
circle = 0 ;
}
btnCircleStyle ( ) ;
}
function leftBtnClickCirclesConversion ( ) {
if ( circle == 0 ) {
circle = focus_ol_li. length;
}
circle-- ;
btnCircleStyle ( ) ;
}
function btnCircleStyle ( ) {
for ( let i = 0 ; i < focus_ol_li. length; i++ ) {
for ( let j = 0 ; j < focus_ol_li. length; j++ ) {
focus_ol_li[ j] . classList. remove ( 'current' ) ;
}
focus_ol_li[ circle] . classList. add ( 'current' ) ;
}
}
function animate ( obj, target, callback ) {
clearInterval ( obj. timer) ;
obj. timer = setInterval ( function ( ) {
var step = ( target - obj. offsetLeft) / 10 ;
step = step > 0 ? Math. ceil ( step) : Math. floor ( step) ;
if ( obj. offsetLeft == target) {
clearInterval ( obj. timer) ;
callback && callback ( ) ;
}
obj. style. left = obj. offsetLeft + step + 'px' ;
} , 15 ) ;
}
function creationInterval ( ) {
timer = setInterval ( function ( ) {
right. click ( ) ;
} , 2000 )
}
< / script>
< / body>
< / html>