Last active
February 27, 2019 15:43
-
-
Save dagalti/c8fc86cb791a51fe24e5dc647507c4a3 to your computer and use it in GitHub Desktop.
Simple View more / Read more directive for vuejs based on number of lines. Add v-viewmore="MAXIMUM_LINES_T0_SHOW" to content div. Demo : https://codepen.io/dagalti/pen/vPOZaB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* How to Use? | |
1. Add v-viewmore to content div. | |
2.Add CSS to the component. | |
3. Javascript | |
import { viewmore } from 'vue-viewmore-directive.js' | |
export default { | |
name: 'Yourcomponent', | |
data () {}, | |
directives:{viewmore}, | |
} */ | |
/* JAVASCIRPT */ | |
export const viewmore = { | |
inserted: function (el, binding){ | |
let maxlines = binding.value | |
let lineheight = parseFloat(getComputedStyle(el).lineHeight) | |
let paddingtop = parseFloat(getComputedStyle(el).paddingTop) | |
let lines = (el.clientHeight) / lineheight ; | |
let maxheight = (lineheight * maxlines) + paddingtop | |
if(lines>maxlines){ | |
el.classList.add('more') | |
el.style.maxHeight = maxheight + 'px' | |
el.addEventListener('click', handler = ()=> { | |
el.style.maxHeight = "" | |
el.scrollIntoView({behavior: "smooth"}) | |
el.removeEventListener('click', handler) | |
el.classList.remove('more') | |
}) | |
} | |
}, | |
unbind: function (el, binding) { | |
el.removeEventListener('click', handler) | |
handler = "" | |
} | |
} | |
/* CSS */ | |
.more{ | |
position: relative; | |
overflow: hidden; | |
} | |
.more::after{ | |
content:"View more..."; | |
display: block; | |
cursor:pointer; | |
position: absolute; | |
bottom: 5px; | |
right: 3px; | |
font-size: 12px; | |
background: lightgreen; | |
padding: 3px 10px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment