简体   繁体   English

使用jQuery计算窗口调整大小时的div高度

[英]Calculate div height on window resize using jQuery

I'm aiming to dynamically change the height of a div as the window is resized. 我的目标是在调整窗口大小时动态更改div的高度。

I'm successful when wrapping the jQuery in a function such as $(window).smartresize(function () { } , however, this breaks another area of the functionality. 当将jQuery包装在$(window).smartresize(function () { }类的函数中时,我很成功,但是,这破坏了功能的另一个领域。

HMTL HMTL

<section id="menu">

    <div class="menu-container">

        <ul class="menu__nav">
            <!-- menu nav  -->
        </ul>

        <div class="menu__main">
            <!-- content -->
        </div>

        <div class="menu__main">
            <!-- content -->
        </div>

        <div class="menu__main">
            <!-- content -->
        </div>

    </div>

</section>

jQuery jQuery的

( function($) {

    $(document).ready(function() {

        var $mmenu = $('.menu__main');

        if ($mmenu.length > 0) {

            var $mnav = $('.menu__nav'),
                $mnav_a = $mnav.find('a'),
                m = parseInt($mnav.outerHeight(true), 10),
                $contain = $mmenu.closest('.menu-container'),
                h = 0,
                l = 0;

            $mmenu.not(':eq(0)').hide();
            $mmenu.eq(0).addClass('active');
            $mnav_a.eq(0).addClass('active');

            $mmenu.each(function(z) {

                var $this = $(this);

                $this.css('height','auto');

                $this.css({
                    zIndex: z+1,
                    position: 'absolute',
                    top: m + "px",
                    left: l,
                    height: (parseInt($this.outerHeight(false), 10) + m) + "px"
                });

                l = l - $this.outerWidth();

            });

            $contain.height($mmenu.eq(0).height());

            $mnav_a.on('click', function(e) {

                e.preventDefault();
                var $this = $(this);

                if (!$this.hasClass('active')) {

                    $mmenu.stop(true, true);

                    $mnav_a.removeClass('active');
                    $this.addClass('active');

                    $mmenu.filter($('.active'))
                        .removeClass('active')
                        .fadeOut(250)
                        .animate({left:l+"px"}, 250);

                    var $target = $mmenu.filter($this.attr('href'));

                    $contain.animate({height:$target.height()}, 250);

                    $target.addClass('active')
                        .fadeIn(250)
                        .animate({left:0}, 250);
                }

                $this.blur();
            });
        }
    });

} ) ( jQuery );

This is the area I'm most interested in improving: 这是我最有兴趣改进的领域:

$mmenu.each(function(z) {

    var $this = $(this);

    $this.css('height','auto');

    $this.css({
        zIndex: z+1,
        position: 'absolute',
        top: m + "px",
        left: l,
        height: (parseInt($this.outerHeight(false), 10) + m) + "px"
    });

    l = l - $this.outerWidth();

});

$contain.height($mmenu.eq(0).height());

Please let me know if you require further information, hopefully there's an easy fix, thank you 如果您需要更多信息,请告诉我,希望有一个简单的解决方法,谢谢

I think with you can resize with CSS, using percent of window. 我认为可以使用CSS使用窗口百分比来调整大小。 You can define percent for some especifics size of devices. 您可以为设备的某些特定尺寸定义百分比。 I just create this css for example of funcionality. 我只是以功能为例创建此CSS。

@media (max-width: 480px) {
    #divMenu {
        width:40%;
        height:70%;
    }
}

@media (min-width: 480px) and (max-width: 1023px) {
    #divMenu {
        width:70%;
        height:80%;
    }
}

@media (min-width: 1024px) {
    #divMenu {
        width:50%;
        height:50%;
    }
}

Sorry, for my poor english. 对不起,我的英语不好。

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

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