jde*_*ere 4 javascript css jquery scroll
我的网站左侧有一个应该留在用户手中的网站.因此,当用户滚动时,侧边栏会一直滚动,直到它从页面顶部开始说5px.从那时起,它应该被锁定在那里.
当然,视口可能比左栏小,因此左栏不能完全适合屏幕.虽然不是很戏剧化.但是我希望如果用户滚动到底部,侧边栏的底部"点击"页面的页脚,然后再次滚动页面.
这是我得到的代码:我的网站的基本设置和我的问题的第一部分的尝试(但你会看到它不起作用):jsfiddle.
我认为问题的第一部分相当清楚,但第二部分可能有点难以理解,所以这是一个模型:
您可以看到没有显示文本,因为文本位于视口上方.
这是我尝试第一部分的js:
$(document).ready(function () {
var theLoc = 5;
var links = $('#left');
$(window).scroll(function () {
console.log('scroll');
if (theLoc >= $(window).scrollTop()) {
if (links.hasClass('fixed')) {
links.removeClass('fixed');
}
} else {
if (!links.hasClass('fixed')) {
links.addClass('fixed');
}
}
});
});
Run Code Online (Sandbox Code Playgroud)
但可能更多的是css问题:
.fixed {
position:fixed;
}
Run Code Online (Sandbox Code Playgroud)
我尝试再次指定高度等(因为它显示得非常大),但没有提前.
我之前做过这个,这是我创建的代码:查看JSFiddle.
(你可能不得不稍微更改你的标记,我不确定这对你的表格布局有多好,我建议使用divs并浮动它们来布局你的内容.)或者,你可以使用代码/下面的逻辑和roll your own你自己的布局.
基本上,
- 获取我们的元素
- 获取页面加载时侧边栏的位置
- 获取#content.outerHeight()
一旦我们拥有这些变量,在window scroll测试中看看我们是否<=(小于或等于)我们的原件sidebarTop position或检查我们是否已经过去blogHeight,如果有2个是真的,删除粘性类,elseif我们的卷轴是>=我们原来的sidebar位置,然后添加.sticky类(具有position: fixed).
该Java脚本是像这样:
// Cache our vars for the fixed sidebar on scroll
var $sidebar = $('#sidebar-nav');
// Get & Store the original top of our #sidebar-nav so we can test against it
var sidebarTop = $sidebar.position().top;
// Edit the `- 10` to control when it should disappear when the footer is hit.
var blogHeight = $('#content').outerHeight() - 10;
// Add the function below to the scroll event
$(window).scroll(fixSidebarOnScroll);
// On window scroll, this fn is called (binded above)
function fixSidebarOnScroll(){
// Cache our scroll top position (our current scroll position)
var windowScrollTop = $(window).scrollTop();
// Add or remove our sticky class on these conditions
if (windowScrollTop >= blogHeight || windowScrollTop <= sidebarTop){
// Remove when the scroll is greater than our #content.OuterHeight()
// or when our sticky scroll is above the original position of the sidebar
$sidebar.removeClass('sticky');
}
// Scroll is past the original position of sidebar
else if (windowScrollTop >= sidebarTop){
// Otherwise add the sticky if $sidebar doesnt have it already!
if (!$sidebar.hasClass('sticky')){
$sidebar.addClass('sticky');
}
}
}
Run Code Online (Sandbox Code Playgroud)
该HTML:
<header>This is the header!</header>
<ul id="sidebar-nav" class="nav nav-list">
<li><a href="#">Home</a></li>
<li><a href="#">Blog</a></li>
</ul>
<div id="content">Content in here, scroll down to see the sticky in action!</div>
<div class="clear"></div>
<div id="footer">This is the #footer</div>
Run Code Online (Sandbox Code Playgroud)
的CSS:
/* Sticky our navbar on window scroll */
#sidebar-nav.sticky {position:fixed;top:5px;}
/* Other styling for the html markup */
header {
border:1px solid #aaa;
background-color:yellow;
margin-bottom:5px;
height:50px;
}
#sidebar-nav {
width:150px;
border:1px solid #ddd;
margin:0;
padding:0;
float:left;
}
#sidebar-nav li {
list-style:none;
border:1px solid #ddd;
margin:10px;
padding:2px;
}
#content {
height:2000px;
width:500px;
padding:10px;
border:1px solid #ddd;
margin:0 0 10px 5px;
float:right;
}
#footer {
height:800px;
border:1px solid green;
background-color:#ddd;
}
.clear {
clear:both;
}
Run Code Online (Sandbox Code Playgroud)
:)
| 归档时间: |
|
| 查看次数: |
8564 次 |
| 最近记录: |