首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在页脚顶部滚动div停止

在页脚顶部滚动div停止
EN

Stack Overflow用户
提问于 2015-10-01 15:15:53
回答 2查看 2.9K关注 0票数 0

我试着做一些类似http://jsfiddle.net/Kkv7X/的事情

目前,我的脚本运行良好,但是当我向下滚动页面并到达页脚部分时,滚动div (在我的例子中是.粘滞页脚)从屏幕上消失。

我想要做的是,当我到达页脚部分时,滚动的div应该保持在我的页脚的顶部。我该怎么做?

这是我的密码

代码语言:javascript
复制
$(document).ready(function () {
        
    // sticky footer scroll effect
    $(document).scroll(function() {
        if($('.sticky-footer').offset().top + $('.sticky-footer').height() >= $('#footer').offset().top - 10) {
            $('.sticky-footer').css('position', 'absolute');
        }
        			    
        if($(document).scrollTop() + window.innerHeight < $('#footer').offset().top) {
            $('.sticky-footer').css('position', 'fixed'); // restore when you scroll up
        }
    });
});

CSS

代码语言:javascript
复制
html {
    	position: relative;
    	min-height: 500px;
    }
    .sticky-footer {
    	position: fixed;
    	bottom: 0;
    	background-color: rgba(	255, 255, 255, 0.5);
    	text-align: center;
    	width: 100%;
    	height: 60px;
    	padding: 15px 0;
    }

HTML

代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sticky-footer">
    		<div class="container">
    			<div class="row">
    				<div class="col-md-12">
    					 // code here ...
    				</div>
    			</div>
    		</div>
    	</div>
    	
    	<footer id="footer">
    		<div class="container">
    			<div class="row">
    
                      // code here ...
                </div>
    		</div>
    	</footer>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-01 15:51:59

当滚动到达页脚时,它们(http://uk.marketo.com/software/)只会更改粘性按钮的样式。因此,您只需在滚动条到达页脚时侦听,并更改您的粘性元素作为响应。

要收听我推荐使用https://github.com/imakewebthings/waypoints的滚动条,我认为是处理滚动动画、侦听器等的更容易的库之一。

应用于粘性元素的样式应更改

代码语言:javascript
复制
.sticky{
  position  : fixed;
  bottom : 0;
}

至:

代码语言:javascript
复制
.sticky.reached{
   position : absolute;
   bottom : 400px; //or whatever.

.sticky是粘性元素(或元素或包装器等)的类。当滚动条到达页脚时,.reached就是添加到其中的类。当滚动条再次上升时,你几乎必须脱帽。即(使用路径点):

代码语言:javascript
复制
$("footer").waypoint(function(direction){
    if(direction == 'down')
       $('.sticky').addClass('reached');
    else
       $('.sticky.reached').removeClass('reached');   

});
票数 0
EN

Stack Overflow用户

发布于 2015-10-01 15:41:32

你的问题是,你的脚是高于你的其他粘性页脚。如果你能把它放在最底层,它就能工作:

代码语言:javascript
复制
$(document).ready(function () {
        
        			// sticky footer scroll effect
        			$(document).scroll(function() {
        				if($('.sticky-footer').offset().top + $('.sticky-footer').height() >= $('#footer').offset().top - 10) {
        					$('.sticky-footer').css('position', 'absolute');
        				}
        			    
        				if($(document).scrollTop() + window.innerHeight < $('#footer').offset().top) {
        					$('.sticky-footer').css('position', 'fixed'); // restore when you scroll up
        				}
        			});
        });
代码语言:javascript
复制
html {
    	position: relative;
    	min-height: 500px;
    }

footer {
  position: absolute;
  bottom: -500px;
  height: 500px;
  }

.sticky-footer {
    	position: fixed;
    	bottom: 0;
    	background-color: rgba(	255, 255, 255, 0.5);
    	text-align: center;
    	width: 100%;
    	height: 60px;
    	padding: 15px 0;
    }
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="sticky-footer">
    		<div class="container">
    			<div class="row">
    				<div class="col-md-12">
    					 // code here ...
    				</div>
    			</div>
    		</div>
    	</div>
    	
    	<footer id="footer">
    		<div class="container">
    			<div class="row">
    
                      // code here ...
                </div>
    		</div>
    	</footer>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32891136

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档