Reputation: 57
I have a problem with jQuery. Can't figure out how to do this. I have multiple sections width different heights:
<section id="1" class="something" style="height:111px; background:red;">1</section>
<section id="2" class="something" style="height:222px; background:blue;">2</section>
<section id="3" class="something" style="height:333px; background:green;">3</section>
My jQuery scales the height of each section when the window is smaller than 500px:
var org_height = $('.something').height();
$(window).resize(function() {
$(".something").each(function() {
var win = $(window).width();
var height = $('.something').height();
if(win < 500) {
y = 500 - win;
$('.something').css("height",org_height-y/3+"px");
}
else {
$('.something').css("height",org_height+"px");
}
});
});
JSFiddle: https://jsfiddle.net/owtz31jj/
How can I store the original height of each section and scale them depending on each height and back to the original height again. I greatly appreciate any help.
Upvotes: 3
Views: 50
Reputation: 47099
You can use jQuery.fn.data:
// Store original height for each .something
$(".something").each(function() {
$(this).data('org_height', $(this).height());
});
$(window).on('resize', function() {
var win = $(window).width();
$(".something").each(function() {
// Get the value storred in org_height using jQuery.fn.data
var org_height = $(this).data('org_height');
if(win < 500) {
var y = 500 - win;
$(this).css("height", org_height - y/3);
}
else {
$(this).css("height", org_height);
}
});
});
Upvotes: 2