Jquery 使用滚动变量增加元素的高度?
下面的函数通过计算窗口高度并将其除以窗口高度来计算用户滚动的距离。随着这个百分比的增加,我想增加div'arrow'的css'height'量。我做错了什么Jquery 使用滚动变量增加元素的高度?,jquery,css,Jquery,Css,下面的函数通过计算窗口高度并将其除以窗口高度来计算用户滚动的距离。随着这个百分比的增加,我想增加div'arrow'的css'height'量。我做错了什么 $(document).scroll(function(){ // grab the scroll amount and the window height var scrollAmount = $(window).scrollTop(); var documentHeight
$(document).scroll(function(){
// grab the scroll amount and the window height
var scrollAmount = $(window).scrollTop();
var documentHeight = $(document).height();
// calculate the percentage the user has scrolled down the page
var scrollPercent = (scrollAmount / documentHeight) * 100;
function increaseHeight() {
$(".arrow").css({
height: scrollPercent + 'px'
});
//do something when a user gets 50% of the way down my page
});
这应该是有效的-
或者(我不确定您在这里想做什么):
你为什么认为自己做错了什么?现在我们要猜猜你看到了什么?PS:“因为它不起作用”实际上并不是我第一个问题的答案,当用户向下滚动时,div.箭头的高度应该增加,我会使用
.height(scrollPercent)
@Blender-也许,但我只是尽可能地重用他的现有代码。非常感谢@HristoYankov-看起来我只是需要删除不必要的函数increaseHeight?@JoeIsaacson,是的,要么删除它,要么有条件地调用它,这取决于你需要什么。
$(document).scroll(function(){
// grab the scroll amount and the window height
var scrollAmount = $(window).scrollTop();
var documentHeight = $(document).height();
// calculate the percentage the user has scrolled down the page
var scrollPercent = (scrollAmount / documentHeight) * 100;
$(".arrow").css({
height: scrollPercent + 'px'
});
// do something when a user gets 50% of the way down my page
});
$(document).scroll(function(){
// grab the scroll amount and the window height
var scrollAmount = $(window).scrollTop();
var documentHeight = $(document).height();
// calculate the percentage the user has scrolled down the page
var scrollPercent = (scrollAmount / documentHeight) * 100;
var fnDoScroll = function() {
$(".arrow").css({
height: scrollPercent + 'px'
});
};
// do something when a user gets 50% of the way down my page
if (scrollPercent >= 50)
fnDoScroll();
});