【发布时间】:2017-04-23 01:15:59
【问题描述】:
我的问题是关于增加 li 内 div 的宽度。
基本上,当用户滚动时,侧边栏(右侧)li 会将 span(红色背景色)从 0 增加到 100。换句话说,如果第一个对应的 div 在视图中,然后对应的 li 元素的 span 将随着用户向下或向上滚动而从 0 增加到 100%。这将指示用户滚动了多少 div。
我有一个示例代码笔,它显示了我正在尝试做的事情 https://codepen.io/hellouniverse/pen/xdVJQp
我的 CSS 看起来像
.sidebar {
position: fixed;
max-width: 300px;
&__list {
position: relative;
text-align: center;
padding: 10px;
margin: 10px auto;
border: 2px solid #f3f2f0;
list-style-type: none;
display: inline-flex;
flex-direction: column;
}
// The primary red bar at the bottom that increases in width with scroll
span {
background-color: red;
height: 5px;
}
}
问题出在js中 在 JS 中,我猜 1.需要计算对应div的高度 2.根据1计算的高度,按一定比例增加跨度。
我似乎根本无法弄清楚代码。有人可以帮忙吗?
// Checks whether an elem gets into view of the window
function isScrolledIntoView(elem) {
'use strict';
if (elem.length > 0) {
var docViewTop = jQuery(window).scrollTop();
var docViewBottom = docViewTop + jQuery(window).height();
var elemTop = jQuery(elem).offset().top;
return ((elemTop <= docViewBottom) && (elemTop >= docViewTop));
}
}
var lastScrollTop = 0;
$(window).scroll(function() {
// Increase & Decrease width of the span of li as you scroll
var height = $('section-just-in').height();
var incrementBy = 300 / height;
$('.sidebar__list span').animate({
// increase width from 0 till 100%
width : width + incrementBy
});
})
【问题讨论】:
标签: javascript jquery html css