简体   繁体   English

只在某个地方制作带有页面的div滚动?

[英]Make a div scroll with page only in a certain place?

How can it be made so that a div scrolls with the page but only in a certain area of the page? 怎样才能使div与页面一起滚动但只在页面的某个区域滚动?

I can't work out how to do this with CSS for only part of the page, I think javascript may be the only option. 我不知道如何使用CSS只为页面的一部分做这个,我认为javascript可能是唯一的选择。

For eg There's three sections of a page, Top , Middle and Bottom . 例如,页面有三个部分, TopMiddleBottom

There's a right floated div which should scroll with the user in the middle section and stop scrolling to be 'left in place' at the top of the middle section as well as the bottom of the middle section. 有一个正确的浮动DIV应该在中间部分用户滚动和停止滚动,在中间部分的顶部以及中间部分的底部被“留在原地”。

 #Top { background-color: grey; width: 100%; height: 400px; } #Middle { background-color: green; width: 100%; height: 800px; } #Bottom { background-color: blue; width: 100%; height: 400px; } #scrolling-section { background-color: yellow; width: 30%; height: 150px; float: right; } 
 <div id="Top"> </div> <div id="Middle"> <div id="scrolling-section"> This box should scroll along the green section but 'cut-off' and stop scrolling at the top and bottom of the green section </div> </div> <div id="Bottom"> </div> 

JSFiddle: fiddle JSFiddle: 小提琴

So here you have solution using jquery: 所以在这里你有使用jquery的解决方案:

  1. Listen to the scroll event and calculate how much the scrolling-section goes outside the Middle section while scrolling up / down. 收听scroll事件,并在向上/向下滚动时计算scrolling-sectionMiddle部分之外的位置。

  2. Added position: relative to the scrolling-section . 添加position: relative对于scrolling-section

  3. Adjust the position of the scrolling-section accordingly. 相应地调整scrolling-section的位置。

 $(document).scroll(function() { var wrapper = $('#Middle'); var box = $('#scrolling-section'); var offsetTop = - wrapper.offset().top + $(window).scrollTop(); var offsetBottom = wrapper.offset().top - $(window).scrollTop() + wrapper.outerHeight() - box.outerHeight(); if (offsetBottom > 0 && offsetTop < 0) { box.css({ 'top': 0 }); } else if (offsetBottom > 0 && offsetTop > 0) { box.css({ 'top': offsetTop + 'px' }); } else { box.offset({ 'top': $(window).scrollTop() + offsetBottom }); } }); 
 #Top { background-color: grey; width: 100%; height: 400px; } #Middle { background-color: green; width: 100%; height: 800px; } #Bottom { background-color: blue; width: 100%; height: 400px; } #scrolling-section { position: relative; background-color: yellow; width: 30%; height: 150px; float: right; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="Top"> </div> <div id="Middle"> <div id="scrolling-section"> This box should scroll along the green section but 'cut-off' and stop scrolling at the top and bottom of the green section </div> </div> <div id="Bottom"> </div> 

Let me know your feedback on this. 请告诉我您对此的反馈。 Thanks! 谢谢!

Some Javascript is needed in order to read the point where you want to change the state of the div you wish to address. 需要一些Javascript才能读取您想要更改要解决的div状态的位置。 You can do this with the getBoundingClientRect() method. 您可以使用getBoundingClientRect()方法执行此操作。 I have worked out a fiddle that will show you. 我找到了一个会告诉你的小提琴。 What happens is that you read the position of #Middle. 你会读到#Middle的位置。 I have added an input field that shows you the value. 我添加了一个输入字段,显示值。 The change will be when the position hits zero. 当仓位达到零时,变化将是。 You then change the CSS properties of the #scrolling-section. 然后,您可以更改#scrolling-section的CSS属性。

You will see some added readings of the element to ensure that it can be positioned in place and will keep its original width; 您将看到元素的一些附加读数,以确保它可以定位并保持其原始宽度;

 var scrollposition = document.getElementById("Middle"); var scrollsection = document.getElementById("scrolling-section"); var scrollsection_offsetLeft = scrollsection.offsetLeft; var scrollsection_width = scrollsection.offsetWidth; var valy = document.getElementById("posy"); window.addEventListener("scroll", function(event) { valy.value = scrollposition.getBoundingClientRect().y || scrollposition.getBoundingClientRect().top; if (valy.value <= 0) { scrollsection.style.position = "fixed"; scrollsection.style.top = "0px"; scrollsection.style.left = scrollsection_offsetLeft + "px"; scrollsection.style.width = scrollsection_width + "px"; } else { scrollsection.style.position = "static"; scrollsection.style.top = "auto"; scrollsection.style.left = "auto"; } }, false) 
 #posy { position: fixed; top: 0; } #Top { background-color: grey; width: 100%; height: 400px; } #Middle { background-color: green; width: 100%; height: 800px; } #Bottom { background-color: blue; width: 100%; height: 400px; } #scrolling-section { background-color: yellow; width: 30%; height: 150px; float: right; } 
 <input type="text" id="posy" /> <div id="Top"> </div> <div id="Middle"> <div id="scrolling-section"> This box should scroll along the green section but 'cut-off' and stop scrolling at the top and bottom of the green section </div> </div> <div id="Bottom"> </div> 

I'm on my mobile but if you add 我在我的手机上,但如果你添加

#scrolling-section {
  position: fixed;
  background-color: yellow;
  width: 30%;
  height: 150px;
  right: 8px;
}

This will scroll with the page but really there will need to be an event listener that will trigger when #scrolling-section appears on the screen possibly adding the attribute position:fixed; 这将滚动页面,但实际上需要一个事件监听器,当屏幕上出现#scrolling-section时可能会触发,可能会添加属性position:fixed; then another event listener when the #bottom appears calculates the size of #middle set margin-top & position:absolute; 当#bottom出现时,另一个事件监听器计算#middle set margin-top&position:absolute;的大小; hope this helps point in the right direction. 希望这有助于指出正确的方向。

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

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