在div上滚动时滚动页面 [英] scroll page when scroll on div
问题描述
我有一个div,当我在div上滚动时,页面应该滚动,如何实现呢? Div的z-index高于其他页面元素,它位于所有页面元素之上
I have a div, when I scroll on div, page should scroll, how can I achieve this? Div has z-index higher than other page elements, it's on top of all page elements
使用javascript或jQuery,什么?
Using javascript or jQuery, anything?
DIV是最上面的元素,其他元素的z-index较低.现在顶级DIV比视口更长,所以我需要滚动
DIV is the top most element, other elements have lower z-index. Now top DIV is longer than viewport, so I need to scroll
推荐答案
您可以使用jQuery在div上连接.Scroll()
事件: http://api.jquery.com/scroll/.然后,每次调用回调函数时,您都可以将scrollTop
位置设置为与div相似的位置.
You can hook up the .Scroll()
event on the div using jQuery: http://api.jquery.com/scroll/. Then each time the callback is called I belive you can set the scrollTop
position to something that corresponds to a similar position to the div.
$(function() {
$('#yourDiv').scroll(function() {
$(window).scrollTop($(window).height() * $(this).scrollTop() / $(this).height());
});
});
这只是一个粗略的猜测.但是,这样做的目的是找出div
向下滚动的距离,然后将窗口向下滚动相同的百分比.
This was just a rough guess. But the idea is to find how far down the div
has scrolled and scroll the window down the same percentage.
最后,我相信div
必须是position: absolute;
.
这篇关于在div上滚动时滚动页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!