简体   繁体   English

滚动页面上的固定位置

[英]fixed position on scrolling page

I have two div in site body.I want to have fixed position to the left div... but when scrolling to bottom it comes on my contact us div..this is the sample page: http://www.runsensible.com/legal/privacypolicy here is my code: 我的网站正文中有两个div。我想在左侧div上保持固定的位置...但是滚动到底部时,它会联系我div ..这是示例页面: http ://www.runsensible.com / legal / privacypolicy这是我的代码:

.sticky {
  position: fixed;
}

<div id="menu-privacy">
    <div class="fusion-text">
    <p>
        <a href="#info" class="_ps2id _mPS2id-h" data-ps2id-offset="">– What information we collect from you</a>
        <a href="#info-used" class="_ps2id _mPS2id-h" data-ps2id-offset="">– How your information is used</a>
        <a href="#info-shared" class="_ps2id _mPS2id-h" data-ps2id-offset="">– How your information is shared</a>
        <a href="#terms-services" class="_ps2id _mPS2id-h" data-ps2id-offset="">– Terms of service</a>
    </p>
    </div>
</div>

<script>
window.onscroll = function() {myFunction()};

var header = document.getElementById("menu-privacy").getElementsByClassName("fusion-text")[0];
var sticky = header.offsetTop;

function myFunction() {
    if (window.pageYOffset > sticky) {
        header.classList.add("sticky");
    } else {
        header.classList.remove("sticky");
    }
}
</script>

I suggest you use position:sticky instead so this effect can be achieved with pure CSS rather than adding javascript. 我建议您改用position:sticky,以便可以通过纯CSS而不是添加javascript来实现此效果。 Looking at your site it's rather possible to do this 查看您的网站,这样做很有可能

Here's what you need to do 这就是你需要做的

  1. Remove the javascript that add/remove the sticky class 删除添加/删除粘性类的JavaScript
  2. Add this style in your div with id="menu-privacy", something like this 使用id =“ menu-privacy”在您的div中添加此样式,类似这样

CSS 的CSS

#menu-privacy {
   position: sticky;
   top: 80px;
}

note that you can play around the top value to achieve the gap that you like. 请注意,您可以发挥最高价值来实现自己喜欢的差距。 Hope this helps! 希望这可以帮助!

1) give an id to footer or add a wrapper for footer section 2) let your function like this - you need to let condition by footer position 1)给页脚添加一个ID或为页脚添加一个包装器2)让您的函数像这样-您需要按页脚位置设置条件

function myFunction() {
  if (sticky < footerIdOffset) {// footerIdOffset is an offset for footer section
    header.classList.add("sticky");
  } else {
    header.classList.remove("sticky");
  }
}

3) you need to subtract margin/gabs between footer and upper section to work in your point truth. 3)您需要减去页脚和上半部分之间的边距/空白才能使用您的点真。

Good luck 祝好运

您可以尝试将属性“ z-index”添加到联系人div。

在“与我们联系” div设置(z-index:10)上​​。

What I suggest is instead of having a JavaScript function, we can use a CSS class for it. 我建议不要使用JavaScript函数,而可以使用CSS类。 NOTE: Based on the requirements of your specification, you can vary the size of width & height. 注意:根据规格要求,您可以更改宽度和高度的大小。 And you can put this as class="fixed" for the div tag containing id="menu-privacy" as you can see below. 您可以将它作为包含id="menu-privacy"的div标签的class="fixed"放置,如下所示。

   .fixed{ 
     position: fixed;
     top: 80px;
     right: 0px;
     width: 100px;
     height: 150px;
     }

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

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