简体   繁体   中英

jquery resize div when min/max width matches certain width

如果窗口宽度小于x px或%,如何将div的大小逐渐调整为0,而当窗口大于x px或%时又回到max-width?

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");
    }
})

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM