DIV move up when scrolling down

a 夏天 提交于 2021-01-28 14:22:43

问题


I've been trying to get a DIV to move up when using the scroll on browser to move down, but I can't find a solution which works.

More specifically, for example if I fill a DIV with images, I want these images to scroll upwards when I scroll the browser window scrollbar downwards. So as you move longer down the page, the DIV moves upwards and shows more images.

Could you give me some suggestions how to receive such an effect?


回答1:


Try something like this:

$(window).scroll(function(){            
    $("#scrollingDiv").stop().animate({ "marginTop": ($(window).scrollTop() + 30) + "px"}, "slow"); 
});



回答2:


I don't entirely follow what you are trying to do with the <div> content, but there is an easy way to detect page scrolling with jQuery:

$.scroll(function() {
    alert('Scroll position: ' + $('html').scrollTop());
});

From there, you can position whatever you want, however you want, using this value $('html').scrollTop().




回答3:


Maybe I'm misunderstanding but, are you trying to keep your DIV in a fixed position regardless of how far the user scrolls down the page?

If so there's a style for that:

position:fixed


来源:https://stackoverflow.com/questions/8140259/div-move-up-when-scrolling-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!