Skip to content

Instantly share code, notes, and snippets.

@cjwinchester
Created July 23, 2013 03:54
Show Gist options
  • Save cjwinchester/6059739 to your computer and use it in GitHub Desktop.
Save cjwinchester/6059739 to your computer and use it in GitHub Desktop.
JQuery div resizer dongle
<!doctype html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<style>
.container {
font-family:'Helvetica Neue', Helvetica, Arial, sans-serif;
font-size:12px;
width:600px;
border-right:1px solid #ccc;
}
.bartext {
vertical-align:-40%;
margin-right:5px;
}
#bar1 {
height:30px;
background:#D0D1E6;
color:#fff;
font-weight:bold;
text-align:right;
}
#bar2 {
height:30px;
background:#74A9CF;
color:#fff;
font-weight:bold;
text-align:right;
}
#bar3 {
height:30px;
background:#034E7B;
color:#fff;
font-weight:bold;
text-align:right;
}
</style>
</head>
<body>
<input type="button" id="one" value="Scenario 1" />
<input type="button" id="two" value="Scenario 2" />
<input type="button" id="three" value="Scenario 3" />
<br/><br/>
<div class="container">
<div id="bar1"></div>
<div id="bar2"></div>
<div id="bar3"></div>
</div>
<script>
$("#one").click(function(){
$("#bar1").animate({
width: '35%',
}, 1000 );
$("#bar2").animate({
width: '15%',
}, 1000 );
$("#bar3").animate({
width: '50%',
}, 1000 );
// wait a tick before grabbing the div width - otherwise it'd return 100%
setTimeout(function(){
$('#bar1').html('<span class="bartext">' + (parseInt($('#bar1').css('width')) / 600) * 100 + '%</span>');
$('#bar2').html('<span class="bartext">' + (parseInt($('#bar2').css('width')) / 600) * 100 + '%</span>');
$('#bar3').html('<span class="bartext">' + (parseInt($('#bar3').css('width')) / 600) * 100 + '%</span>');
}, 1500)
});
$("#two").click(function(){
$("#bar1").animate({
width: '80%',
}, 1000 );
$("#bar2").animate({
width: '5%',
}, 1000 );
$("#bar3").animate({
width: '15%',
}, 1000 );
setTimeout(function(){
$('#bar1').html('<span class="bartext">' + (parseInt($('#bar1').css('width')) / 600) * 100 + '%</span>');
$('#bar2').html('<span class="bartext">' + (parseInt($('#bar2').css('width')) / 600) * 100 + '%</span>');
$('#bar3').html('<span class="bartext">' + (parseInt($('#bar3').css('width')) / 600) * 100 + '%</span>');
}, 1500)
});
$("#three").click(function(){
$("#bar1").animate({
width: '25%',
}, 1000 );
$("#bar2").animate({
width: '35%',
}, 1000 );
$("#bar3").animate({
width: '40%',
}, 1000 );
setTimeout(function(){
$('#bar1').html('<span class="bartext">' + (parseInt($('#bar1').css('width')) / 600) * 100 + '%</span>');
$('#bar2').html('<span class="bartext">' + (parseInt($('#bar2').css('width')) / 600) * 100 + '%</span>');
$('#bar3').html('<span class="bartext">' + (parseInt($('#bar3').css('width')) / 600) * 100 + '%</span>');
}, 1500)
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment