jQuery 中滚动条聚焦时如何禁用可拖动 div

发布于 2024-09-11 03:49:05 字数 441 浏览 5 评论 0原文

我有一个带有侧滚动条的 jQuery 可拖动容器 div,当我上下滚动时,该滚动条不应该是可拖动的。 .infotext 是包含文本的内部 div,包含在 #infobody 中,设置为 Overflow:auto。我需要一种方法来在选择滚动条时取消 div 上的可拖动功能。这是我的代码:

$(".lsidebar").draggable({"scroll":false});

 .lsidebar #infobody{
cursor:default;
position:absolute;
width:100%;
overflow:auto;
margin:23px 0 0 0;
z-index:14;
}

   #infobody .infotext{
cursor:default;
position:relative;
width:100%;
z-index:14;
color:#969696;
}

I have a jQuery draggable container div with a side scroll bar that should not be draggable when I am scrolling up and down. .infotext is the inner div that has the text, contained within #infobody which is set to overflow:auto. I need a way to negate the draggable function on the div when the scrollbar is selected. Here is my code:

$(".lsidebar").draggable({"scroll":false});

 .lsidebar #infobody{
cursor:default;
position:absolute;
width:100%;
overflow:auto;
margin:23px 0 0 0;
z-index:14;
}

   #infobody .infotext{
cursor:default;
position:relative;
width:100%;
z-index:14;
color:#969696;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一花一树开 2024-09-18 03:49:06

实际上你可以在draggable中使用“cancel”属性来防止滚动事件影响到draggable元素。

例如:

如果你的 html 是这样的...

<div class="draggable">
    <div class="scrollable"></div>
</div> 

对于 js:

$('.draggable').draggable({
   cancel: '.scrollable'
});

当你在该特定区域滚动时,外部可拖动元素将被暂时禁用。

Actually you can use "cancel" property in draggable to prevent the scrolling event affect to the draggable element.

For example:

if your html is like this...

<div class="draggable">
    <div class="scrollable"></div>
</div> 

for the js:

$('.draggable').draggable({
   cancel: '.scrollable'
});

When you scrolling in that specific area, the outer draggable element will be temporary disabled.

垂暮老矣 2024-09-18 03:49:06

这对我有用,我使用 jquery ui 1.8.2。


      $(".drag").draggable({
                        start: function(e, ui) {
                        if ($(e.originalEvent.target).is(".scroll"))
                            e.preventDefault();
                        }
                    })

this works for me, im using jquery ui 1.8.2.


      $(".drag").draggable({
                        start: function(e, ui) {
                        if ($(e.originalEvent.target).is(".scroll"))
                            e.preventDefault();
                        }
                    })
情深如许 2024-09-18 03:49:06

此问题的解决方法(因为由于某种原因,最近没有人回答我的问题)是用名为 jScrollPane 的 jQuery 滚动条替换默认浏览器滚动条。这样做允许我使用插入的滚动条 ID 作为选择器来禁用可拖动...如下所示:

 $('.jScrollPaneDrag').live('mousedown mouseup', function(e){
     if(e.type=='mousedown'){
         $('.lsidebar').draggable({disable: true});
     }else{
         $('.lsidebar').draggable({"scroll":false});
     };
 });

A workaround to this problem (because for some reason no one is answering my questions as of recently) was to substitute the default browser scrollbar with a jQuery scrollbar known as jScrollPane. Doing this allows me to use the inserted scrollbar ID as a selector to disable draggable...like so:

 $('.jScrollPaneDrag').live('mousedown mouseup', function(e){
     if(e.type=='mousedown'){
         $('.lsidebar').draggable({disable: true});
     }else{
         $('.lsidebar').draggable({"scroll":false});
     };
 });
别忘他 2024-09-18 03:49:06

我想这可能会很好用

$("div")
.mouseenter(function(){...bind drag;})
.mouseleave(function(){...unbind drag;});

I guess this might work fine

$("div")
.mouseenter(function(){...bind drag;})
.mouseleave(function(){...unbind drag;});
毁梦 2024-09-18 03:49:06

尝试在 page_wrapper 的 css 中添加 overflow:hidden;

Try to add overflow:hidden; in the css of the page_wrapper.

沧笙踏歌 2024-09-18 03:49:06

您可以绑定到当前元素及其所有子元素和父元素上的滚动事件,如下所示,滚动时将禁用拖动:

  • 当前可拖动元素
  • 所有可拖动元素的子元素 可拖动元素
  • 的所有可拖动父元素

检查此 JSFIDDLE

$(function () {
    $(".draggable").draggable({
        start: function () {
            // cancel draggin while scrolling
            if ($(this).data("scrolled")) {
                $(this).data("scrolled", false).trigger("mouseup");
                return false;
            }
        }
    }).find("*").andSelf().scroll(function () {
        // Bind to the scroll event on current element, and all its children.

        // Prevent all draggable parents from dragging while they are scrolling
        $(this).parents(".ui-draggable").data("scrolled", true);
    }); });

You can bind to the scroll event on the current element and all its children and parents as done below, the dragging will be disabled while scrolling on:

  • Current draggable element
  • All children of the draggable element
  • All draggable parents of the draggable element

check this JSFIDDLE.

$(function () {
    $(".draggable").draggable({
        start: function () {
            // cancel draggin while scrolling
            if ($(this).data("scrolled")) {
                $(this).data("scrolled", false).trigger("mouseup");
                return false;
            }
        }
    }).find("*").andSelf().scroll(function () {
        // Bind to the scroll event on current element, and all its children.

        // Prevent all draggable parents from dragging while they are scrolling
        $(this).parents(".ui-draggable").data("scrolled", true);
    }); });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文