0

$('#aaa').draggable();
* {
    margin: 0;
    padding: 0;
}

#aaa {
    width: 800px;
    height: 800px;
    position: absolute;
    background: red;
}

.wrap {
    height: 2000px;
    position: relative;
    ;
}
<div class="wrap">
    <div id='aaa'></div>
</div>
<link href="https://cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.js"></script>

when I drag the div overflow the page, the scrollbar will scroll. How do I prevent it from scrolling

ZhuoYan
  • 11
  • 1
  • 1
    Does this answer your question? [How to disable page scroll while dragging draggable in jquery?](https://stackoverflow.com/questions/32395936/how-to-disable-page-scroll-while-dragging-draggable-in-jquery) – Triby Dec 24 '19 at 02:56

3 Answers3

0

This is the default behavior and can be disabled by adding scroll: false option. https://api.jqueryui.com/draggable/#option-scroll

$('#aaa').draggable({
    scroll: false
});
* {
    margin: 0;
    padding: 0;
}

#aaa {
    width: 800px;
    height: 800px;
    position: absolute;
    background: red;
}

.wrap {
    height: 2000px;
    position: relative;
}
<div class="wrap">
    <div id='aaa'></div>
</div>
<link href="https://cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.js"></script>
Shanyu
  • 56
  • 3
0

You can add overflow: auto to the styling of the .wrap class, which will stop horizontal and vertical scrolling while dragging that element

$('#aaa').draggable();
* {
    margin: 0;
    padding: 0;
}

#aaa {
    width: 800px;
    height: 800px;
    position: absolute;
    background: red;
}

.wrap {
    height: 2000px;
    position: relative;
    overflow: auto;
}
<div class="wrap">
    <div id='aaa'></div>
</div>
<link href="https://cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet">
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/jqueryui/1.12.1/jquery-ui.js"></script>
Shiny
  • 4,945
  • 3
  • 17
  • 33
0

You can try like this

#wrap{width: 800px; height: 800px; position: absolute; overflow: scroll; background: red; } 

$( "#aaa" ).draggable({ containment: '#wrap',scroll: false })

Hitesh Kumar
  • 383
  • 2
  • 6