简体   繁体   English

CSS窗口使用JavaScript的高度

[英]CSS window Height with JavaScript

I have some issue with javascript and css... 我有一些javascript和css的问题...

The problem is that i want to pass the value from javascript to css... 问题是我想将值从javascript传递给css ...

To be more specific i want to get from javascript the window height and pass it to css height in pixels... 更具体一点,我想从javascript获取窗口高度并将其传递给css高度(以像素为单位)...

Here is my CSS: 这是我的CSS:

.fulframe {
    position: relative;
    width: 100%;
    height: TheValueToPass;
    overflow: hidden
}

Is there any solution for that? 那有什么解决方案吗? I am not familiar with javascript and can you be more detailed in you answers :-) 我不熟悉javascript,你可以在答案中更详细:-)

You could do this with only CSS. 你只能用CSS做到这一点。

Pure css: 纯css:

.fulframe {
  position: relative;
  width: 100%;
  height: 100vh;
  overflow: hidden
}

The vh is the viewport height of the browser window. vh是浏览器窗口的视口高度。 This is not supported in older versions of IE, so be careful. 旧版本的IE不支持此功能,因此请小心。 It is out there though. 它在那里。

Update 10/26/2017: I'd like to add that viewport heights and widths are now widely supported in most all major browsers, with the exception being Opera. 更新10/26/2017:我想补充一点,视口高度和宽度现在在大多数主流浏览器中得到广泛支持,但Opera除外。 Can I Use Viewport Units? 我可以使用视口单元吗?

You cannot use Javascript variables directly in the CSS code! 您不能直接在CSS代码中使用Javascript变量!

If you are using JQuery there is css() function to modify style of elements. 如果您使用的是JQuery,则可以使用css()函数来修改元素的样式。 You should do 你应该做

    $( document ).ready(function() {
            $(".fulframe").css("height", TheValueToPass);
    });

Assuming that TheValueToPass is known before calling code above 假设在调用上面的代码之前已知TheValueToPass

I have also find the answer with jquery from "here" 我也从“这里”找到jquery的答案

just i modified it for my needs :-) 我根据自己的需要修改了它:-)

<script>

        function jqUpdateSize(){
            // Get the dimensions of the viewport
            var width = $(window).width();
            var height = $(window).height();

            $('#jqWidth').html(width);
            $('#jqHeight').html(height);

            $('.test').css({ position: "relative",
            width: $(window).width(),
            height:  $(window).height(),
            overflow:" hidden"});

        }
        $(document).ready(jqUpdateSize);    // When the page first loads
        $(window).resize(jqUpdateSize);     // When the browser changes size


    </script>

and then u can call class with html statement: 然后你可以用html语句调用类:

<div class="test" >heloo
    <p>
        <strong>jQuery resize:</strong>
        <span id="jqWidth"></span>,
        <span id="jqHeight"></span>
    </p></div>

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

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