简体   繁体   English

得到偏移然后使位置绝对

[英]get offset then make position absolute

Hello People what I need is get offset for any section then make position of all sections is absolute: the problem that all sections return with 0px of offset 你好人们我需要的是得到任何部分的偏移然后使所有部分的位置是绝对的:所有部分以0px的偏移返回的问题

 $(document).ready(function () { $('section').each(function (index, element) { var $this = $(this); $this.css('top', $this.offset().top + 'px'); $this.css('position', 'absolute'); }); }) 
 section { width: 100%; height: 400px; background-color:#ff5b74; } .section-2{ height:500px; background-color:#00ff6b; } .section-3{ background-color:#5066e9; } .section-4{ background-color:#ff6a00; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <section class="section-1"></section> <section class="section-2"></section> <section class="section-3"></section> <section class="section-4"></section> 

The problem is that for each section you set the position absolute which makes it out of the flow before you change the remaining sections. 问题是,对于每个部分,您设置绝对位置,使其在更改剩余部分之前使其脱离流程。 so the remaining sections will ignore the space taken by this section and fill it. 所以其余部分将忽略此部分占用的空间并填充它。

That's why they all take the same top position 这就是为什么他们都采取相同的顶级位置

You need to set the absolute position after you complete setting the top position for all sections, meaning outside the loop function. 完成所有部分的顶部位置设置后,需要设置绝对位置,这意味着在循环功能之外。

 $(document).ready(function() { $('section').each(function(index, element) { var $this = $(this); $this.css('top', $this.offset().top + 'px'); }); $('section').css('position', 'absolute'); }) 
 section { width: 100%; height: 400px; background-color: #ff5b74; } .section-2 { height: 500px; background-color: #00ff6b; } .section-3 { background-color: #5066e9; } .section-4 { background-color: #ff6a00; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <section class="section-1"></section> <section class="section-2"></section> <section class="section-3"></section> <section class="section-4"></section> 

The problem you are facing is as soon as you are in the each loop and set the first element to position: absolute; 您遇到的问题是,只要您在each循环中并将第一个元素设置为position: absolute; it will lose it's height cause it is position: absolute; 它将失去它的高度因为它是position: absolute; Or that's what position: absolute; 或者那是什么position: absolute; does. 确实。 It takes element out of the flow. 它需要元素流出。

Elements that are positioned relatively are still considered to be in the normal flow of elements in the document. 相对定位的元素仍然被认为是文档中正常的元素流。 In contrast, an element that is positioned absolutely is taken out of the flow and thus takes up no space when placing other elements. 相比之下,绝对定位的元件从流动中取出,因此在放置其他元件时不占用空间。 Information from here here 来自这里的信息

Sooo to accomplish you need to set the height by the height and the position of the previous element. Sooo要完成你需要通过高度和前一个元素的位置设置高度。

$(this).prev().offset().top + $(this).prev().outerHeight()

I left the console.log() there if you are interested to see the different values. 如果你有兴趣看到不同的值,我就离开了console.log()

 $(document).ready(function () { $('section').each(function (index, element) { // console.log( // $(this).offset().top, // $(this).outerHeight(), // ($(this).offset().top + $(this).outerHeight()), // ($(this).prev().offset().top + $(this).prev().outerHeight()) // ); $(this).css('top', $(this).prev().offset().top + $(this).prev().outerHeight() + 'px'); $(this).css('position', 'absolute'); }); }) 
 section { width: 100%; height: 400px; background-color:#ff5b74; } .section-2{ height:500px; background-color:#00ff6b; } .section-3{ background-color:#5066e9; } .section-4{ background-color:#ff6a00; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <section class="section-1"></section> <section class="section-2"></section> <section class="section-3"></section> <section class="section-4"></section> 

<style>
    section {
        width: 100%;
        height: 400px;
        background-color:#ff5b74;
    }
    .section-2{
        height:500px;
        background-color:#00ff6b;
    }
    .section-3{
        background-color:#5066e9;
    }
    .section-4{
        background-color:#ff6a00;
    }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<section class="section-1"></section>
<section class="section-2"></section>
<section class="section-3"></section>
<section class="section-4"></section>

<script>
    $(document).ready(function () {
        $('section').each(function (index, element) {
            var $this = $(this);

            if (index != 0) {

                $this.css('top', (Number($this.offset().top)+Number($('.section-'+(Number(index)+1)).height())) + 'px');
            }
            $this.css('position', 'absolute');
        });
    })

</script>

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

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