最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Button in fixed position in only one div - Stack Overflow

programmeradmin2浏览0评论

I have created two div's, one at the top and one at the bottom. I have created one button. I gave a property as fixed position at left side bottom page. If i scroll the page the button is fixed at that position only. basically the button is in fix position for the whole page. My Requirement: When I scroll the page the button should be fixed for some certain height only. When I am scrolling the page the button should be fixed at left button only till the first div bottom line touches the button bottom line. and wen I scroll the page that button should move along with the bottom line of first div.

Basically the button should be in fixed position till the first div bottom line only. when the first div bottom line collapse with the button bottom line, the button should be relative/absolute and moves along with it.

Hope you understood my requirement. Below is my code for which I am looking for requirement

 

   #top {
  border: 1px solid black;
  height: 900px;
  width: 80%;
  position: absolute;
}

.button {
  background-color: #4CAF50;
  border: none;
  color: white;
  bottom: 0px;
  font-size: 16px;
  margin-left: 0px;
  cursor: pointer;
  padding: 10px 24px;
  position: fixed;
}

#bottom {
  border: 1px solid black;
  height: 900px;
  width: 80%;
  top: 910px;
  position: relative;
}

#middle {
  bottom: 50px;
  position: fixed;
}
<html>

  <body>
    <div id="top">
      <div id="middle">
        <button class="button">Fixed Element</button>
      </div>
    </div>

    <br>
    <br>
    <div id="bottom">


    </div>
  </body>

</html>

I have created two div's, one at the top and one at the bottom. I have created one button. I gave a property as fixed position at left side bottom page. If i scroll the page the button is fixed at that position only. basically the button is in fix position for the whole page. My Requirement: When I scroll the page the button should be fixed for some certain height only. When I am scrolling the page the button should be fixed at left button only till the first div bottom line touches the button bottom line. and wen I scroll the page that button should move along with the bottom line of first div.

Basically the button should be in fixed position till the first div bottom line only. when the first div bottom line collapse with the button bottom line, the button should be relative/absolute and moves along with it.

Hope you understood my requirement. Below is my code for which I am looking for requirement

 

   #top {
  border: 1px solid black;
  height: 900px;
  width: 80%;
  position: absolute;
}

.button {
  background-color: #4CAF50;
  border: none;
  color: white;
  bottom: 0px;
  font-size: 16px;
  margin-left: 0px;
  cursor: pointer;
  padding: 10px 24px;
  position: fixed;
}

#bottom {
  border: 1px solid black;
  height: 900px;
  width: 80%;
  top: 910px;
  position: relative;
}

#middle {
  bottom: 50px;
  position: fixed;
}
<html>

  <body>
    <div id="top">
      <div id="middle">
        <button class="button">Fixed Element</button>
      </div>
    </div>

    <br>
    <br>
    <div id="bottom">


    </div>
  </body>

</html>

Share Improve this question edited Jul 26, 2016 at 10:03 brk 50.4k10 gold badges59 silver badges84 bronze badges asked Jul 26, 2016 at 9:55 AryanAryan 311 silver badge3 bronze badges 1
  • Position fixed is relative to the viewport, there are some useful tips on this quesiton: stackoverflow./questions/5209814/… which you might find useful. – connorb Commented Jul 26, 2016 at 10:07
Add a ment  | 

2 Answers 2

Reset to default 2

I think this is not possible with only css. You need javascript or jquery. You need jquery to run my example. You should measure the scrolled amount with the top or other element and change the bottom/top of the button accordingly like this:

Jquery

$(window).scroll(function () {
  if (($(this).scrollTop()+$(window).height())>$('#top').height()){
    $("#btn_fixed").css({ bottom: ($(this).scrollTop()+$(window).height()- $('#top').height())+"px" });
  }else{
    $("#btn_fixed").css({ bottom: '0px' });
  }
});

I have changed css and html of your code.

CSS

#top {
  border: 1px solid black;
  height: 900px;
  width: 80%;
  position: absolute;
}

.button {
  background-color: #4CAF50;
  border: none;
  color: white;
  bottom: 0px;
  font-size: 16px;
  margin-left: 0px;
  cursor: pointer;
  padding: 10px 24px;
  position: fixed;
}

#bottom {
  border: 1px solid black;
  height: 900px;
  width: 80%;
  top: 910px;
  position: relative;
}

HTML

<div id="top">
  <div id="middle">
     <button class="button" id="btn_fixed">Fixed Element</button>
  </div>
</div>

<br>
<br>
<div id="bottom">


</div>

Working fiddle : https://jsfiddle/khairulhasanmd/h9y0bf1g/

Use position: sticky; on your #middle div

发布评论

评论列表(0)

  1. 暂无评论