简体   繁体   English

具有动态高度和绝对位置的子DIV-给父DIV赋予高度

[英]Child DIV with dynamic height and absolute position - Give height to parent DIV

(Please keep in mind I am not code savvy) (请记住,我不懂代码)

http://sebcastilho.com/ http://sebcastilho.com/

I want the image slider to stretch to the full width of the browser. 我希望图像滑块延伸到浏览器的整个宽度。 The website's theme uses some responsive code and so the width of parent DIV of the slider can't be set to 100%. 该网站的主题使用了一些响应代码,因此滑块的父级DIV的宽度不能设置为100%。

So the solution is to 'position: absolute' to get the slider to fit 100% width of the browser. 因此,解决方案是“位置:绝对”以使滑块适合浏览器100%的宽度。 However, when I do this, the browser doesn't know how high the content of the slider is and so the next sections of the website end up moving up and being covered by the slider. 但是,当我这样做时,浏览器不知道滑块的内容有多高,因此网站的下一部分最终会向上移动并被滑块覆盖。

How can I get the parent DIV of Royal Slider to change height dynamically based on the height of the slider (which itself is dynamic based on how many pixels wide it is)? 如何获得Royal Slider的父DIV,以根据滑块的高度动态更改高度(它本身是动态的,取决于它的像素宽度)?

EDIT (After ncardeli's answer) I got this piece of code I found working. 编辑 (在ncardeli的回答之后),我得到了这段代码,发现可以正常工作。 It gets the height of the slider div (the one that has the position: absolute) and gives the height to the containing div. 它获取滑块div的高度(该位置的位置:绝对值),并将高度赋予包含div的高度。

$(document).ready(function(){
    var x = $('#new-royalslider-1').height();
    $('.photographySlider').css('height', x);
})

My problem is that it doesn't refresh if the browser changes size. 我的问题是,如果浏览器更改大小,它不会刷新。 How can I achieve this? 我该如何实现?

A solution with pure CSS might be feasible, using vw unit and calc . 使用vw unitcalc ,使用纯CSS的解决方案可能是可行的。

You should set the width of the slider to 100vw, and position it relative. 您应该将滑块的宽度设置为100vw,并将其相对放置。

#new-royalslider-1 {
    background-color: black;
    margin: 0 auto;
    overflow: visible;
    padding: 0;
    position: relative;
    width: 100vw !important;
}

Then you should set a rule for each breakpoint, setting left to a calculated value. 然后,您应该为每个断点设置一个规则,将其设置为一个计算值。 The calculated value results from substracting the container width to the viewport width and the value of the left padding: 计算得出的值是通过将容器宽度减去视口宽度以及左填充的值得出的:

@media (min-width: 768px) {
    #new-royalslider-1 {
        left: calc((750px - 100vw) / 2 - 15px);
    }
}

@media (min-width: 992px) {
    #new-royalslider-1 {
        left: calc((970px - 100vw) / 2 - 15px);
    }
}

@media (min-width: 1200px) {
    #new-royalslider-1 {
        left: calc((1170px - 100vw) / 2 - 15px);
    }
}

Check here for browser support: 在此处查看浏览器支持:

If you prefer a Javascript solution, this script will do: 如果您更喜欢Javascript解决方案,则此脚本将执行以下操作:

$(window).on('resize', function (){
    $('.photographySlider').height($('#new-royalslider-1').height());
})

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

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