/**
* Initialize bottomUpText shape processor and events
* @param {object} elem DOM object to shape
* @return {object}
*/
var bottomUpText = function(elem){
var _t = this;
// Set up element
_t.STAGE = elem;
_t.STAGE.style.whiteSpace = 'nowrap';
// Fetch text as array
_t.text = _t.STAGE.textContent.trim().split(' ');
// Run and bind
_t.processText();
window.addEventListener('resize', function(){ _t.processText(); });
return _t;
}
/**
* Process text shape
*/
bottomUpText.prototype.processText = function(){
// Set up vars
var _t = this,
maxWidth,
span,
prevX;
// Get max possible width
maxWidth = _t.STAGE.offsetWidth;
// Reset stage
_t.STAGE.innerHTML = '';
// Loop text array in reverse order
for(var i = _t.text.length - 1, n = 0, x = maxWidth; i >= 0; i--){
// Create new span if needed
if(!span){
span = _t.newSpan();
}
// Store current span width
prevX = span.offsetWidth;
// Prepend word to span
span.innerHTML = (_t.text[i].trim() + ' ' + span.innerHTML).trim();
// If additional word exceeds width cap
if(span.offsetWidth >= x){
// Prepend break tag before span
_t.STAGE.insertBefore(document.createElement('br'), span);
// Update max width for stepdown shape
// Disable this line if text block shape is no issue
x = prevX;
// Remove last added word from current span, create a new span and transfer word
span.innerHTML = span.innerHTML.replace(/^\w*\s/, '');
span = _t.newSpan();
span.innerHTML = _t.text[i].trim();
// Reset word count
n = 0;
}
// Incremement span word count
n++;
}
}
/**
* Prepend new span DOM element to stage
* @return {object} Created DOM object
*/
bottomUpText.prototype.newSpan = function(){
var _t = this,
span;
// Create element
span = document.createElement('span');
span.className =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.