admin管理员组文章数量:1023827
If I point the mouse within the div tag, the scrolling works, however I can't scroll the content if I point the mouse outside the box of div. Is it possible to target a specific div wherever the mouse pointer goes?
<div style="max-height: 100px;overflow-y: scroll;">
TEST<br><br><br><br><br><br><br><br><br><br>
</div>
If I point the mouse within the div tag, the scrolling works, however I can't scroll the content if I point the mouse outside the box of div. Is it possible to target a specific div wherever the mouse pointer goes?
<div style="max-height: 100px;overflow-y: scroll;">
TEST<br><br><br><br><br><br><br><br><br><br>
</div>
Share
Improve this question
edited Aug 21, 2018 at 6:32
Chaska
3,2151 gold badge13 silver badges17 bronze badges
asked Aug 21, 2018 at 6:27
Ninja Warrior 11Ninja Warrior 11
3723 silver badges17 bronze badges
3 Answers
Reset to default 5You can do something like this.
const target = document.getElementById("target");
document.addEventListener("wheel", function(e){
// prevent the default scrolling event
e.preventDefault();
// scroll the div
target.scrollBy(e.deltaX, e.deltaY);
})
#target{
height: 100px;
overflow-y: scroll;
}
<div id="target">
TEST<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</div>
You can register the event by yourselves. Such that you can scroll the div
you want outside the div
.
Those this will be tedious for you to handle if your page has multiple scroller.
By the way, to make it a bit more pretty, you need to add animation or it will move 'discretely' as following shown.
document.getElementById("scroll").addEventListener("wheel", e=>e.preventDefault());
document.getElementById("main").addEventListener("wheel", e=>myFunction(e));
function myFunction(e) {
document.getElementById("scroll").scrollTop += 0.2 * e.deltaY;
}
<div id="main" style="height:300px">
<div id="scroll" style="max-height: 100px;overflow-y: scroll;">
TEST<br><br><br><br><br><br><br><br><br><br>
</div>
</div>
Is it possible to target a specific div wherever the mouse pointer goes?
Not really... what you probably want to do instead is to set other parts of the page to position: fixed
or position: sticky
. This way the main body content will scroll but some parts will remain in the same place. Stack Overflow itself does this with the top header and the left sidebar.
If I point the mouse within the div tag, the scrolling works, however I can't scroll the content if I point the mouse outside the box of div. Is it possible to target a specific div wherever the mouse pointer goes?
<div style="max-height: 100px;overflow-y: scroll;">
TEST<br><br><br><br><br><br><br><br><br><br>
</div>
If I point the mouse within the div tag, the scrolling works, however I can't scroll the content if I point the mouse outside the box of div. Is it possible to target a specific div wherever the mouse pointer goes?
<div style="max-height: 100px;overflow-y: scroll;">
TEST<br><br><br><br><br><br><br><br><br><br>
</div>
Share
Improve this question
edited Aug 21, 2018 at 6:32
Chaska
3,2151 gold badge13 silver badges17 bronze badges
asked Aug 21, 2018 at 6:27
Ninja Warrior 11Ninja Warrior 11
3723 silver badges17 bronze badges
3 Answers
Reset to default 5You can do something like this.
const target = document.getElementById("target");
document.addEventListener("wheel", function(e){
// prevent the default scrolling event
e.preventDefault();
// scroll the div
target.scrollBy(e.deltaX, e.deltaY);
})
#target{
height: 100px;
overflow-y: scroll;
}
<div id="target">
TEST<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
</div>
You can register the event by yourselves. Such that you can scroll the div
you want outside the div
.
Those this will be tedious for you to handle if your page has multiple scroller.
By the way, to make it a bit more pretty, you need to add animation or it will move 'discretely' as following shown.
document.getElementById("scroll").addEventListener("wheel", e=>e.preventDefault());
document.getElementById("main").addEventListener("wheel", e=>myFunction(e));
function myFunction(e) {
document.getElementById("scroll").scrollTop += 0.2 * e.deltaY;
}
<div id="main" style="height:300px">
<div id="scroll" style="max-height: 100px;overflow-y: scroll;">
TEST<br><br><br><br><br><br><br><br><br><br>
</div>
</div>
Is it possible to target a specific div wherever the mouse pointer goes?
Not really... what you probably want to do instead is to set other parts of the page to position: fixed
or position: sticky
. This way the main body content will scroll but some parts will remain in the same place. Stack Overflow itself does this with the top header and the left sidebar.
本文标签: javascriptTarget a specific div to scroll even outside of itStack Overflow
版权声明:本文标题:javascript - Target a specific div to scroll even outside of it - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745600106a2158405.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论