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

I have created two div's, one at the top and one at the bottom.I have created one button. I gave

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745121431a4612436.html

相关推荐

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

    I have created two div's, one at the top and one at the bottom.I have created one button. I gave

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信