Fix DIV between the head and footer of the page

Asked

Viewed 210 times

0

Good people I have a div that gets fixed when scrolling the page, but I want it to scroll back with the page before it gets to the footer. The code below makes that when reaching the 300 of the scroll height the div is fixed. The problem is that when I roll the page to the radapé the div overlaps the footer, because it is fixed so I want the div to scroll again with the page before it reaches the footer for example 300px from the bottom.

My code is like this:

$(function(){

    var jElement = $('.element');

    $(window).scroll(function(){
        if ( $(this).scrollTop() > 300 ){
            jElement.css({
                'position':'fixed',
                'top':'200px'
            });
        }
        else{
            jElement.css({
                'position':'relative',
                'top':'auto'
            });
        }
    });
      
    });
 .head {windth:100%; height:100px;}

.conteudo{
  height:600px;float:left;width:50%;
}
.element{float:left;
  width: 30%;
  padding: 3em;
  background: #f9f9f9;
  border: 1px solid #ddd;
  border-top: #dc0000 3px solid;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="head">
head
</div>    
<div class="conteudo">
    conteudo
</div>
<div class="element">
    div a ser fixada 
</div>

1 answer

2

$(window).load(function() {
  function checkOffset() {
    if ($('#element').offset().top + $('#element').height() >=
      $('#footer').offset().top - 100)
      $('#element').css('position', 'absolute');
    if ($(document).scrollTop() + window.innerHeight < $('#footer').offset().top)
      $('#element').css('position', 'fixed');
  }
  $(document).scroll(function() {
    checkOffset();
  });
});
div.body {
  width: 100%;
  height: 2000px;
  background: #C0C0C0;
  position: relative;
}

div#element {
  float: left;
  position: fixed;
  left: 10px;
  bottom: 100px;
  width: 30%;
  padding: 3em;
  background: #f9f9f9;
  border: 1px solid #ddd;
  border-top: #dc0000 3px solid;
  text-align: center;
}

div#footer {
  width: 100%;
  height: 200px;
  background: #eee;
}

.head {
  windth: 100%;
  height: 100px;
}

.conteudo {
  height: 600px;
  float: left;
  width: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="body">
  <div class="head">
    head
  </div>
  <div class="conteudo">
    conteudo
  </div>
  <div id="element">
    div a ser fixada
  </div>
</div>

<div id="footer">
  Rodape
</div>

  • 3

    Leo, what’s wrong with using the snippets?

  • 2

    Here’s how a constructive critique :) https://i.stack.Imgur.com/bBHdP.png For js, html and css-only code, the recommendable [1] is that if you use the stack-snippets, so if your host ever stops working, the code remains active here on the network

  • @Marceloboni understood, I tried but was giving error message

  • What kind of error message? Use is very boring even at first, but after you take the practice, you can create some very elaborate things

  • { "message": "Script error." , "filename": "", "lineno": 0, "colno": 0 }

  • I discovered why: it was in version 3.2.1/jquery.min.js I switched to the author’s version of the question and so it worked without giving the error. It was worth the criticism and the editing too

  • has as Voce put it fixed only at 300 px height I did as Voce did more it gets fixed as soon as I enter and I want to enter the page to scroll the page div roll together and when it arrives at 300 px of scroll the div ai yes get fixed and when it arrives 1000px high the div back to scroll with the page

  • No, it does not appear, click on "Whole page"

  • 1

    yes, living and learning!!!

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.