Skip to content

Instantly share code, notes, and snippets.

@jwdallas
Created August 6, 2013 22:57
Show Gist options
  • Save jwdallas/6169581 to your computer and use it in GitHub Desktop.
Save jwdallas/6169581 to your computer and use it in GitHub Desktop.
Working on a flexbox type thing
* { margin:0; padding:0 }
nav {
width: 220px;
height: 30px;
background-color: #bbb;
position: relative;
}
div {
position: absolute;
line-height: 30px;
background-color: hsl(200, 100%, 50%);
box-sizing: border-box;
}
div span {
display: inline-block;
padding: 0 5px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
max-width: 60px;
}
#left {
left: 0;
background: hsl(20, 100%, 50%);
}
#right {
right: 0;
background: hsl(50, 100%, 50%);
}
#title {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body>
<nav dir="ltr">
<div id="left"><span>gfffffffffffffff<span></div>
<div id="title"><span>jggvgvgvgvgggggggxrddddj</span></div>
<div id="right"><span>Rjgdgdgdj</span></div>
</nav>
</body>
</html>
$(function() {
var calc = {
totalWidth: function() { return $('nav').width(); },
leftWidth: function() { return $('#left').width(); },
rightWidth: function() { return $('#right').width(); },
titleWidth: function() { return $('#title').width(); },
emptySpace: function() {
console.log('totalWidth: ' + calc.totalWidth());
console.log('leftWidth: ' + calc.leftWidth());
console.log('rightWidth: ' + calc.rightWidth());
console.log('titleWidth: ' + calc.titleWidth());
var emptySpace = calc.totalWidth() - (calc.leftWidth() + calc.rightWidth() + calc.titleWidth());
console.log('emptySpace: ' + emptySpace);
return emptySpace;
},
titleOffset: function() {
console.log('titleWidth: ' + calc.titleWidth());
var centeredPosition = calc.totalWidth()/2 - calc.titleWidth()/2;
var currentPosition = $('#title').position().left;
console.log('currentPosition: ' + currentPosition);
console.log('centeredPosition: ' + centeredPosition);
var titleOffset = centeredPosition - currentPosition;
console.log('titleOffset: ' + titleOffset);
return titleOffset;
},
setTitleMaxWidth: function() {
var titleMaxWidth = calc.totalWidth() - (calc.leftWidth() + calc.rightWidth());
return titleMaxWidth;
}
};
function centerTitle() {
var leftOffset = calc.titleOffset() + calc.emptySpace()/2;
console.log('leftOffset: ' + leftOffset);
$('#title').offset({top:0, left:leftOffset});
}
function setLayout() {
$('#title')
.offset({top:0, left:calc.emptySpace()/2})
.find('span').css('max-width', calc.setTitleMaxWidth());
if (calc.titleOffset() !== 0) {
centerTitle();
}
}
setLayout();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment