No Library No Framework - ChoDragon9/posts GitHub Wiki
- jQuery λ°λλΌλ‘ ꡬν
- You Dont Need Lodash Underscore
- You Dont Need Momentjs
- Slider
- JS Animation
const arr = [0, 1, 2]
arr.splice(1, 2)
console.log(arr) // [0]
const price = 10000
price.toLocaleString() // 10,000
const arr = [0, 2, 3, 1]
arr.sort((a, b) => a - b)
console.log('μ€λ¦μ°¨μ', ...arr)
arr.sort((a, b) => b - a)
console.log('λ΄λ¦μ°¨μ', ...arr)
const parseSVG = (template) => {
var tmp = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
tmp.innerHTML = template
return tmp.children[0]
}
μ°μμ μΌλ‘ μ½λ°± μ€ν μμ²λμ΄λ, μ§μ λ μκ° μ£ΌκΈ°λ‘ μ½λ°±μ΄ μ€ννλ κΈ°λ² Throttle κΈ°λ²μ μ¬μ©νλ©΄ μΌμ ν μκ°μ μ£ΌκΈ°λ‘ μ½λ°±μ μ€ν ν μ μλ€.
/**
* Throttle
*
* @param callback {Function} Callback function
* @param _threshhold {Number} Throttle time
* @return {Function} Event Listener
*/
const throttle = (callback, ms = 100) => {
let timer = null
let last = 0
return function (...args) {
const self = this
const now = +new Date
if (last && now < last + ms){
clearTimeout(timer)
timer = setTimeout(() => {
last = now
callback.apply(self, args)
}, ms)
} else {
last = now
callback.apply(self, args)
}
}
}
const log = throttle(console.log, 1000)
setInterval(() => {
console.log('------')
log('1000')
console.log('------')
}, 500)
// ------
// 1000
// ------
// ------
// ------
// ------
// 1000
// ------
λ€μμ μ΄λ²€νΈ μ€νμ λ°©μ§νκΈ° μν΄ νλλ‘ κ·Έλ£Ήννμ¬ νΉμ μκ°μ΄ μ§λ ν μ€νλλ κΈ°λ² μ¬λ¬κ°μ μ΄λ²€νΈκ° κ³Όλ€νκ² μ¬μ©μ΄ λμμ λ λΆνλ₯Ό μ€μ¬μ£Όλ μν μ νλ€.
/**
* Debounce
*
* @param callback {Function} Callback function
* @param _delay {Number} Delay time
* @return {Function} Event Listener
*/
const debounce = (callback, ms) => {
let timer = null
return function(...args){
const self = this
clearTimeout(timer)
timer = setTimeout(() => {
callback.apply(self, args)
}, ms)
}
}
const log = debounce(console.log, 100)
log('1')
log('2')
log('3')
log('4')
// output: 4
.txt{
width: 200px;
max-height: 35px;
line-height: 20px;
overflow: hidden;
text-overflow: ellipsis;
white-space: normal;
word-wrap: break-word;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
.txt.show{display:block;max-height:none}
.txt.show + .more{display:none}
<div class="wrap">
<div class="txt">
κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§
κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§κ°λλ€λΌλ§
</div>
<a href="#" class="more" onclick="toggle()">More</a>
</div>
const toggle = () => {
const txt = document.querySelector('.txt').classList
if (txt.contains('show')) {
txt.remove('show')
} else {
txt.add('show')
}
}