使用可拖动禁用滚动 div

Disable scrolling with draggable div

我有一个可以拖动的 div。当我将它拖到网页的边缘之一附近时,整个网站都会滚动。我可以禁用此功能吗? (我基本上可以使用 div 在网页上移动,但我希望它仅限于我当前所在的位置) 我使用 jquery 可拖动脚本!

header:

link href="stilmallaudioplayer.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<style>
</style>
<script>
$(function() {
$( "#audioplayer" ).draggable();
});
</script>

body:

<div id="audioplayer" class="ui-widget-content">
<audio controls class="ui-widget-content">
<source src="horse.ogg" type="audio/ogg">
<source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
</div>

注意:这不是我的完整代码,只是可拖动的 div 部分!

css:

#audioplayer { 
    padding: 0.5em; 
    z-index: 3;
    position: absolute;
    top: 100px;
    left: 100px;
}

您可以在拖动 body 或容器元素上设置 overflow:hidden 或拖动 start 并在拖动 stop 时将其删除:

Js:

$("#audioplayer").draggable({
    start: function (event, ui) {
        $('body').addClass("overflow");
    },
    stop: function (event, ui) {
        $('body').removeClass("overflow");
    }
});

CSS:

overflow{overflow:hidden;} //Use inline style or  !important if other css overrides this

这似乎有效:fiddle

jQuery

$(function () {
    $("#audioplayer").draggable(
    { containment: "html", scroll: false }

    );
});

CSS

html, body {
    height:100%;
    overflow:hidden;
}

    #audioplayer {
        padding: 0.5em;
        z-index: 3;
        position: absolute;
        top: 100px;
        left: 100px;
        border: 1px solid black;
        height:20px;
        width: 100px;
    }