Created
December 8, 2012 01:00
-
-
Save jacktandrew/4237925 to your computer and use it in GitHub Desktop.
Read More or Less
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
var textHolder = document.querySelector('.textHolder'); | |
var fullText = textHolder.innerText; | |
var snippet = fullText.slice(0, 1000) + "..."; | |
function createLink(quantity) { | |
var link = document.createElement("a"); | |
link['href'] = "#"; | |
link.className = quantity | |
link.innerText = "Read " + quantity; | |
textHolder.appendChild(link); | |
return link | |
} | |
function makeSnippet() { | |
textHolder.innerText = snippet; | |
var link = createLink('more'); | |
link.addEventListener('click', makeFullText, false); | |
} | |
function makeFullText() { | |
textHolder.innerText = fullText; | |
var link = createLink('less'); | |
link.addEventListener('click', makeSnippet, false); | |
} | |
if(fullText.length > 1000) { | |
makeSnippet(); | |
} |
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
var snippetLength = 990; | |
var textWrapper = document.querySelector('.textWrapper'); | |
var fullText = textWrapper.innerText; | |
if(fullText.length > snippetLength) { | |
textWrapper.innerText = ""; | |
var hash = { | |
snippet : fullText.slice(0, snippetLength), | |
ellipsis : "...", | |
cutSection : fullText.slice(snippetLength, -1) | |
} | |
for(key in hash) { | |
var chunk = document.createElement("span"); | |
chunk.className = key; | |
chunk.innerText = hash[key]; | |
textWrapper.appendChild(chunk); | |
} | |
createLink('More'); | |
createLink('Less'); | |
toggleDisplay(['.readless','.cutSection']) | |
function createLink(quantity) { | |
var link = document.createElement("a"); | |
var readX = 'read' + quantity; | |
link['href'] = "#"; | |
link.className = readX; | |
link.innerText = "Read " + quantity; | |
textWrapper.appendChild(link); | |
link.addEventListener('click', toggleDisplay, false); | |
} | |
function toggleDisplay(array){ | |
if(array.type == 'click') { | |
array.preventDefault(); | |
array = ['.readMore','.ellipsis','.readless','.cutSection']; | |
} | |
array.forEach(function(el){ | |
if (document.querySelector(el).style.display == 'none') { | |
document.querySelector(el).style.display = 'inline'; | |
} else { | |
document.querySelector(el).style.display = 'none'; | |
} | |
}); | |
} | |
} |
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
var charLimit = 800; | |
var body = document.querySelector('body'); | |
var textHolder = document.querySelector('.textHolder'); | |
var fullText = textHolder.innerText; | |
if(fullText.length > charLimit) { | |
var snippet = fullText.slice(0, charLimit - 10) + "..."; | |
createLink('more'); | |
createLink('less'); | |
changeText(); | |
} | |
function createLink(quantity) { | |
var link = document.createElement("a"); | |
link['href'] = "#"; | |
link.className = quantity | |
link.innerText = "Read " + quantity; | |
link.addEventListener('click', changeText, false); | |
body.appendChild(link); | |
} | |
function changeText() { | |
if(textHolder.innerText.length > charLimit) { | |
textHolder.innerText = snippet; | |
document.querySelector('.less').style.display = 'none'; | |
document.querySelector('.more').style.display = 'block'; | |
} else { | |
textHolder.innerText = fullText; | |
document.querySelector('.more').style.display = 'none'; | |
document.querySelector('.less').style.display = 'block'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment