Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mouse click and Drag Instead of Horizontal Scroll bar( To view full content of child Div)

Tags:

I need Mouse click and Drag instead of Horizontal scroll bar.

Reference imageWhen i click and drag the child div that should move on left/right respect to dragging direction.

Any solution with css or jquery/JS

My code:

.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>
like image 575
ItzMe_Ezhil Avatar asked Feb 18 '15 05:02

ItzMe_Ezhil


People also ask

How do I make my mouse wheel scroll horizontally?

Scroll horizontally using a keyboard and mousePress and hold SHIFT. Scroll up or down using your mouse wheel (or another vertical scrolling function of your mouse).

How do I scroll horizontally in a div?

Set the overflow-y: hidden; and overflow-x: auto; that will automatically hide the vertical scroll bar and present only the horizontal scrollbar. The white-space: nowrap; property is used to wrap text in a single line. Here the scroll div will be horizontally scrollable.

How do I get rid of the horizontal scroll in overflow?

To hide the horizontal scrollbar and prevent horizontal scrolling, use overflow-x: hidden: HTML.


1 Answers

This can be done with plain javascript. Add mouse eventListeners to the element you want to drag, capture your starting point and calculate the scroll X position of your mouse movement. While dragging, apply this position to your child element:

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>
like image 153
Kurt Van den Branden Avatar answered Sep 20 '22 19:09

Kurt Van den Branden