【问题标题】:Can't scroll page with mouse wheel while using html5 drag and drop使用 html5 拖放时无法使用鼠标滚轮滚动页面
【发布时间】:2018-06-14 13:40:28
【问题描述】:

使用html5拖放功能时,似乎无法使用鼠标滚轮滚动页面。我在任何 html5 拖放现场演示中都看到了这个问题。有什么办法可以解决这个问题?

【问题讨论】:

  • 嗨@Shelef,我遇到了同样的问题。我想知道您是否找到了任何解决方案?

标签: javascript html drag-and-drop


【解决方案1】:

这可能不是发生这种情况的唯一原因,但在我的情况下,有一个带有 pointer-events: none 的覆盖元素,它阻止了在 Chrome 中拖动时滚动

【讨论】:

    【解决方案2】:

    有一些可以滚动页面的演示。试试这个代码:

      <style type = "text/css">
         #boxA, #boxB {
            float:left;padding:10px;margin:10px;-moz-user-select:none;
         }
    
         #boxA { background-color: #6633FF; width:75px; height:75px;  }
         #boxB { background-color: #FF6699; width:150px; height:150px; }
      </style>
    
      <script type = "text/javascript">
    
         function dragStart(ev) {
            ev.dataTransfer.effectAllowed = 'move';
            ev.dataTransfer.setData("Text", ev.target.getAttribute('id'));
            ev.dataTransfer.setDragImage(ev.target,0,0);
            return true;
         }
    
         function dragEnter(ev) {
            event.preventDefault();
            return true;
         }
    
         function dragOver(ev) {
            return false;
         }
    
         function dragDrop(ev) {
            var src = ev.dataTransfer.getData("Text");
            ev.target.appendChild(document.getElementById(src));
            ev.stopPropagation();
            return false;
         }
      </script>
    

      <center>
         <h2>Drag and drop HTML5 demo</h2>
         <div>Try to move the purple box into the pink box.</div>
    
         <div id = "boxA" draggable = "true"
            ondragstart = "return dragStart(ev)">
            <p>Drag Me</p>
         </div>
    
         <div id = "boxB" ondragenter = "return dragEnter(ev)" 
            ondrop = "return dragDrop(ev)" 
            ondragover = "return dragOver(ev)">Dustbin
         </div>
      </center>
    

    调整窗口大小。

    【讨论】:

    • 有运行的例子吗?
    【解决方案3】:

    这是工作代码。附件可以显示工作页面的截图。我通过添加一些 div 来使页面滚动来更改源代码。 代码来源:https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop

    <!DOCTYPE HTML>
    <html>
    <head>
    <style>
    #div1 {
        width: 350px;
        height: 70px;
        padding: 10px;
        border: 1px solid #aaaaaa;
    }
    
    </style>
    <script>
        function allowDrop(ev) {
        ev.preventDefault();
        }
    
        function drag(ev) {
        ev.dataTransfer.setData("text", ev.target.id);
        }
    
        function drop(ev) {
        ev.preventDefault();
        var data = ev.dataTransfer.getData("text");
        ev.target.appendChild(document.getElementById(data));
        }
    </script>
    </head>
    <body>
    
        <p>Drag the W3Schools image into the rectangle:</p>
    
        <div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
        <br>
    
        <img id="drag1" src="images.jpg" alt="image can not be seen"draggable="true"       ondragstart="drag(event)" width="300" height="68">
    <div id="middle" style="height:50px;width:1200px;background-color:blue;margin-top:300px;"><p>This is a test div</p> </div>
        <div id="bottom" style="height:50px;width:1200px;background-color:yellow;margin-  top:400px;"><p>This is another test div</p></div>
    
    
    </body>
    </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      相关资源
      最近更新 更多