如何防止固定按钮重叠页脚区域,并将按钮停止在页脚所在的顶部
How to prevent fixed button from overlapping footer area and stop the button on top of where the footer is located
我正在尝试创建一个固定在屏幕左下侧的按钮。我试着在JSFiddle中进行设置,以重新创建我想要做的事情。
这是我的HTML:
<div id="header">header
</div>
<div id="button">button
</div>
<div id="content">some content
</div>
<div id="footer">footer
</div>
和CSS:
#header,#footer{
background-color:red;
}
#content
{
height:2000px;
}
#footer
{
height:200px;
}
#button
{
background-color:gray;
width:100px;
height:100px;
position:fixed;
bottom:0;
left:0;
right:0;
}
我读过,我应该使用scrolltoFixed.js、lockfixed.js等插件但我的问题是,我不知道如何使用javascript,甚至不知道从哪里开始编辑javascript。这是一把小提琴
我希望按钮停止在页脚所在的位置,并使其看起来像是停靠的。
现在进行了更新,使其位于页脚上方。
希望这就是你的意思jQuery:
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() > $(document).height() - 200) {
$('#button').addClass('fixed_button');
}else{
$('#button').removeClass('fixed_button');
}
});
CSS:
.fixed_button{
position:absolute !important;
margin-top:1900px;
bottom: auto !important;
}
请改用绝对定位。另外,不要使用左:0和右:0。只使用其中一个。尝试
position:absolute;
bottom:0;
left:0;
编辑:抱歉,您的代码似乎可以工作。你到底想做什么?
我一直在寻找类似的东西,但找不到任何合适的答案。
var $fixed_element = $(".some_element")
if($fixed_element.length){
var $offset = $(".footer").position().top,
$wh = $(window).innerHeight(),
$diff = $offset - $wh,
$scrolled = $(window).scrollTop();
$fixed_element.css("bottom", Math.max(0, $scrolled-$diff));
}
所以现在固定元素会停在页脚之前。并且不会与之重叠。
相关文章:
- 输入类型按钮返回历史记录并返回顶部
- 如何在已经有图标的滚动到顶部按钮中添加文本
- 如何停止两个按钮执行相同的工作?在这种情况下,它们都会滚动到顶部
- 滚动到顶部按钮在 chrome 或 safari 中不起作用
- 转到顶部按钮在Firefox浏览器中不起作用
- 如何在volusion网站中添加滚动到顶部的浮动按钮
- Zurb 基金会顶部导航栏具有多个后退按钮
- 返回顶部按钮在加载时出现
- 如何用汉堡按钮替换固定的顶部导航
- 强制浏览器在单击后退按钮时将用户带到页面顶部
- 在 jQuery 中滚动到顶部按钮
- 当您离开页面底部 100 像素时,淡出“滚动到顶部”按钮
- 如何在单击按钮时滚动回页面顶部
- “返回顶部”按钮仅在窗口滚动时可见(但移动设备不起作用)
- 如何在单击按钮时滚动到顶部
- 滚动到顶部按钮,jQuery在Safari v.7.0.5中不起作用
- 滚动到顶部/底部按钮错误
- 如何使滚动到顶部按钮在页面的预定部分出现时出现
- 当按钮被停用时,它会显示在固定元素的顶部
- 单击“返回顶部”按钮(滚动顶部)多次发布