【问题标题】:Element that is fixed at bottom of browser until it hits the bottom of a div固定在浏览器底部直到到达 div 底部的元素
【发布时间】:2017-04-13 04:56:00
【问题描述】:

我正在努力做出一些风格差异的网站是https://www.crunchydata.com/。有一个 ID 为 #bottom 的 div 当前固定在视口底部,直到您到达页面底部。

我想做的是将它固定在视口的底部,直到它到达 div #main-content 的底部(就在页脚之前),然后坐在那里 #main 的底部- 用户滚动到页脚时的内容,直到用户向上滚动。

我假设这将需要某种 JavaScript 解决方案,但无法找到与我正在寻找的足够接近的任何解决方案。任何指向正确方向的指针都将不胜感激。

【问题讨论】:

  • 你想要吗:#bottom 的位置将被固定,直到用户滚动到#content 部分的底部。那么,当用户在#content 下滚动时,#bottom 的位置将是相对的?
  • 寻找 position:sticky 和与之配套的 polyfills ...
  • @JituRaiyan,是的,这正是我要找的!
  • 好的,我会努力解决的

标签: javascript jquery html css


【解决方案1】:

您可以添加此 JavaScript 代码。它适用于所有设备(任何设备宽度或高度):

$(document).ready(function(){

	var scrollPoint;

	$(window).on('load resize scroll', function(){

	scrollPoint = $("#footer").position().top - $(window).height() + $("#bottom").outerHeight();

	if($(window).scrollTop() > scrollPoint){
		$('#bottom').css({
			'position' : 'relative',
			'top' : 0,
			'bottom' : ''
		});
	}else{
		$('#bottom').css({
			'position' : 'fixed',
			'top' : '',
			'bottom' : 0
		});
	}
	});
});

而且它工作得很好,没有硬代码。这是输出:

【讨论】:

  • 这很好用——有什么方法可以推荐在固定位置和相对位置之间添加平滑过渡?只添加一个常规的“transition all”看起来似乎不太好
  • 我刚刚更新了我的代码。您需要在事件中分配 bottomPos 和 scrollPoint 的值。否则,它无法正常调整大小。
  • 不幸的是,没有办法在位置之间设置动画:固定和相对。
  • 如果你放慢滚动速度,#bottom 元素会开始非常糟糕地跳跃像素。知道是什么原因造成的吗?
  • 是的,我注意到了。这种类型的错误没有逻辑错误。但是,不幸的是它发生了。所以,我需要找到解决这个问题的替代方法。
【解决方案2】:

您可能想考虑使用 position:sticky。如果您仍在寻找纯 JS 解决方案,您可以简单地检查用户向下滚动页面的距离,如果足够远,将元素“锁定”到页面。

例子:

document.body.onscroll=function(){
  var bottom=document.getElementById("bottom");
  if(scrollY+innerHeight>1050){ //position of where you want the element to go + it's height
    bottom.style.position="absolute";
    bottom.style.top="1000px";
    bottom.style.bottom="";
  }
  else{
    bottom.style.position="fixed";
    bottom.style.bottom="0px";
    bottom.style.top="";
  }
}
#bottom{
  position:fixed;
  bottom:0px;
  width:100%;
  height:50px;
  background-color:blue;
}
<div style="height:1000px;width:100%;background-color:red;"></div>
<div style="height:1000px;width:100%;background-color:yellow;"></div>
<div id="bottom"></div>

有点乱,但可以完成工作。

JSFiddle

【讨论】:

  • 谢谢!这是完美的!
  • Position:sticky 在不同的浏览器中也没有很好的支持(特别是 IE 根本没有)。所以感谢您帮助找到 JavaScript 答案
【解决方案3】:

这是有效的。请检查。

document.body.onscroll=function(){
    var bottom=document.getElementById("bottom");
    if(scrollY+innerHeight>1050){ //position of where you want the element to go + it's height
        bottom.style.position="relative";
        bottom.style.top="1000px";
        bottom.style.bottom="";
    }
    else{
        bottom.style.position="fixed";
        bottom.style.bottom="0px";
        bottom.style.top="";
    }
 }
#bottom{
    position:fixed;
    bottom:0px;
    width:100%;
    height:50px;
    background-color:blue;
}
<div style="height:1000px;width:100%;background-color:red;">
<div id="bottom" style="width:100%;height:50px;background-color:blue"></div>
</div>
<div style="height:1000px;width:100%;background-color:yellow;"></div>

【讨论】:

  • 以听起来很琐碎为代价,这似乎与我的回答完全相同...您能解释一下您的意图吗?
  • 我已经在js中编辑了位置并在html中更改了代码。我检查了你的 sn-p,但它不工作。
  • 哦……抱歉,我明白你在说什么。不过,它们在我这边的功能似乎或多或少是一样的?
  • 当我检查你的代码 sn-p 时它没有工作,所以通过编辑你的代码它工作了。
  • @dav 你做得很好,也支持 Nimish 进行更正。
猜你喜欢
  • 1970-01-01
  • 2017-06-27
  • 1970-01-01
  • 2013-12-25
  • 2011-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多