Reputation: 2985
how do I re-size a div gradually to 0 if as window width is under x px or %, and back to max-width when window bigger than x px or % ?
Upvotes: 1
Views: 2186
Reputation: 3360
Run this example on full page.
Adding a resizeTimer
to provide a little bit of time lapse before firing the resize event.
var resizeTimer;
$(window).on('resize',function(){
clearTimeout(resizeTimer);
resizeTimer = setTimeout(function(){
$("#theDiv").height($(window).height()/5);//calculation for height here as required
}, 200);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div id="theDiv" style="background-color: yellow; border: 1px solid black; height: 100px"></div>
Upvotes: 0
Reputation: 259
This would normally be done with CSS, using a CSS transition property, and then using jQuery's addClass
CSS
#myElementToResize{
width: 100px;
-webkit-transition: width 2s;
transition: width 2s;
}
.minimized{
width: 0px;
}
JS
$(window).resize(function(){
if ( $(window).width() <= 768 ){
$("#myElementToResize").addClass("minimized");
} else {
$("#myElementToResize").removeClass("minimized");
}
})
Upvotes: 2