【问题标题】:Mouse click and Drag Instead of Horizontal Scroll bar( To view full content of child Div)鼠标单击并拖动而不是水平滚动条(查看子 Div 的全部内容)
【发布时间】:2021-02-22 18:33:48
【问题描述】:

我需要鼠标点击和拖动而不是水平滚动条。

当我点击并拖动应该在拖动方向左右移动的子 div 时。

任何带有 css 或 jquery/JS 的解决方案

我的代码:

.parent{
  width:300px;
  border:5px sold red;
  overflow:hihdden; 
  float:left;
}
.child{
  width:1000px;
  float:left;
  font-size:15px;
  font-family:arial;
  padding:10px;
}
<div class="parent">
    <div class="child">    
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum        
        
    </div>
    
</div>

【问题讨论】:

  • 你可以使用 mouseDown 和 mouseUp 事件来改变父容器的 X 位置。
  • 错字错误,在border 中,solidoverflow 中缺少'i' 应该是hidden 而不是hihdden

标签: javascript jquery html css


【解决方案1】:

这可以用普通的 javascript 来完成。将鼠标 eventListeners 添加到要拖动的元素,捕获起点并计算鼠标移动的滚动 X 位置。拖动时,将此位置应用于您的子元素:

const slider = document.querySelector('.parent');
let mouseDown = false;
let startX, scrollLeft;

let startDragging = function (e) {
  mouseDown = true;
  startX = e.pageX - slider.offsetLeft;
  scrollLeft = slider.scrollLeft;
};
let stopDragging = function (event) {
  mouseDown = false;
};

slider.addEventListener('mousemove', (e) => {
  e.preventDefault();
  if(!mouseDown) { return; }
  const x = e.pageX - slider.offsetLeft;
  const scroll = x - startX;
  slider.scrollLeft = scrollLeft - scroll;
});

// Add the event listeners
slider.addEventListener('mousedown', startDragging, false);
slider.addEventListener('mouseup', stopDragging, false);
slider.addEventListener('mouseleave', stopDragging, false);
.parent{
  width:300px;
  border:5px solid red;
  overflow-x: hidden; 
  float:left;
}
.child{
  width:1000px;
  float:left;
  font-size:15px;
  font-family:arial;
  padding:10px;
  cursor: pointer;
}
<div class="parent">
    <div class="child">    
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum        
    </div>
</div>

【讨论】:

  • 假设子元素包含一个链接,如何在拖动时禁用该元素上的操作,并且仅在不执行拖动时使其可点击?
  • @Catalin 您可以拖动并单击子 div 内的任何按钮/链接。我对库尔特的回答做了一个小调:jsfiddle.net/5rvkum0p/2
  • @Máster 我认为 Catalin 提到了别的东西,我遇到的问题 - 当该容器内的所有内容都可以点击时(例如图片库)。我拖动并移动子元素,释放拖动时它“单击”下面的图像。
【解决方案2】:

您可以使用 jquery UI 中的 .draggable() 函数。这是我根据您的代码创建的示例的链接。 更新代码 http://jsfiddle.net/3mh2b7rk/4/

jQuery("#child").draggable({ 
    cursor: "move", 
    containment: "parent",
    stop: function() {
      if(jQuery("#child").position().left < 1)
          jQuery("#child").css("left", "720px");
    }
});
.parent{width:300px; border:5px solid red; overflow:hidden; left:20px}
.child-container{width:1730px;left:-710px;position:relative;}
#child{ width:1000px; float:left; font-size:15px; font-family:arial; padding:10px 5px 10px 0;left:720px; border-right:4px solid red}
.clear {clear:both;}
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
<div class="parent">
  <div class="child-container">
    <div id="child"> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum </div>
    <div class="clear"></div>
  </div>
</div>

【讨论】:

  • 感谢您的出色工作,但我想在“.ui-draggable”左值高于“0”时将子 div 恢复到原始位置。 我不想在子 div 处于原始位置时将其从左向右移动。
  • 您可以通过在可拖动调用中添加属性 revert: true 来做到这一点。像 jQuery( ".child" ).draggable({ cursor: "move", revert: true});
  • 是的,当左值更改为“0”以上时,这就是我想要的。你能分享任何演示吗?停止:函数(事件,ui){ var stop = ui.position.left; if (stop > 0) { ui.draggable.draggable('option','revert',true); } else{ $(this).draggable('option','revert','invalid'); } }
  • 我已经更新了演示代码并更新了我原来的评论..jsfiddle.net/3mh2b7rk/1
  • 为了搜索者的利益,这对我有用。 qnimate.com/javascript-scroll-by-dragging
猜你喜欢
  • 2012-04-08
  • 1970-01-01
  • 2019-04-05
  • 2022-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多