简体   繁体   English

当最小/最大宽度匹配特定宽度时,jQuery调整div的大小

[英]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,使用CSS Transition属性,然后使用jQuery的addClass来完成此操作

CSS CSS

#myElementToResize{
    width: 100px;
    -webkit-transition: width 2s;
    transition: width 2s;
}
.minimized{
    width: 0px;
}

JS 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. 添加一个resizeTimer可以在触发resize事件之前提供一点时间间隔。

 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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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