固定位置-将宽度设置为等于容器
Position Fixed - Set width to be equal to container
我一直试图在用户滚动足够远时将div的位置设置为固定。我注意到,当div的位置设置为固定时,宽度与父元素的宽度不同。如何保持position:fixed的div的宽度与父元素的宽度相同?
在这个例子中,一旦设置了position:fixed,我希望底部div的宽度与黄色框的宽度相同。
http://jsfiddle.net/BYJPB/
这是我的HTML:
<header>
<div class="filler-space">
Filler Space
</div>
<div class="toolbar">
<div class="current-article">
<div class="progress-bar"></div>
My article
</div>
</div>
</header>
这是我的CSS:
body {
padding:0 10px;
height: 1000px;
}
.filler-space {
background-color: yellow;
border:1px solid #000000;
height: 140px;
}
.toolbar {
border: 1px solid black;
}
.current-article {
font-weight: 600;
height: 100%;
line-height: 40px;
overflow: hidden;
width: 100%;
z-index: -1;
}
.progress-bar {
position: absolute;
}
.prog .progress-bar {
background: none repeat scroll 0 0 #F2F4F7;
bottom: 0;
height: 100%;
left: 0;
margin: 0 -10px 0 -10px;
overflow: hidden;
right: 0;
top: 0;
transition: width 0.1s ease 0s;
z-index: -1;
width: 100%;
}
这是我的JavaScript:
var $toolbar = $('.toolbar');
var $window = $(window);
var $header = $('header');
$(document).scroll(function() {
var scrollTop = $window.scrollTop();
if ( scrollTop > 150) {
$toolbar.css({
'position' : 'fixed',
'top' : 0
});
$header.addClass('prog');
}
else {
$toolbar.css({
'position' : 'static',
'top' : 0
});
$header.removeClass('prog');
}
});
更新你的js,我不理解你的要求,但我认为这就是你想要的。
if ( scrollTop > 150) {
$toolbar.css({
'position' : 'fixed',
'top' : 0,
'width':$header.width()
});
$header.addClass('prog');
}
else {
$toolbar.css({
'position' : 'static',
'top' : 0
});
$header.removeClass('prog');
}
相关文章:
- 在BootStrap菜单栏中为所选项目设置背景,类似于BootStrap中的父导航选项
- 如何基于具有动态宽度的父容器设置比例宽度
- 敲除js变量设置类似于调用函数
- 如果元素在容器外部,是否可以相对于容器以绝对定位来定位该元素
- 当滚动条到达容器底部时设置动画
- Express&容器:Can't在发送标头后设置标头
- 我想根据容器中包含的图像设置容器的高度
- 在相对于用户当前 URL 路径的不同路径中设置 Cookie
- 对于 MEAN 堆栈 docker 容器来说,在 OS X 和 DigitalOcean 上具有相同配置的最简单设置是什
- 如何以类似于vivus.js的样式设置SVG填充和路径的动画
- 固定位置-将宽度设置为等于容器
- 获取元素相对于父容器的位置/偏移量
- 容器宽度远大于内容
- 设置相对于宽度的高度
- 在HTML5、CSS、JavaScript中添加选项选择器,类似于移动应用程序中的选项设置
- d3.js-当鼠标悬停在SVG容器上的这些元素上时,我如何设置光标
- Knockout.js绑定可以同时应用于容器标签和后代吗?
- 使用jquery设置父容器的宽度
- jQuery 查找元素相对于容器的位置
- 如何设置一个Dojo TitlePane的高度,相对于外部容器节点的大小