

源码
<div class="box">
<div class="content">
<p style="background: #2e2b37" draggable="false">内容一<span>保持点击状态并向左滑动鼠标</span></p>
<p style="background: #6c5e86" draggable="false">内容二</p>
<p style="background: olive" draggable="false">内容三</p>
<p style="background: #de7172" draggable="false">内容四<span>保持点击状态并向右滑动鼠标</span></p>
</div>
</div>
let doc= document;
let html = doc.querySelector('html');
let content = doc.querySelector('.content');
doc.onselectstart = function(){return false;};
let startScroll = 0;
let startX = 0;
let previousX = 0;
let currentX = 0;
let velocity = 0;
let direction = 0;
let momentum = 0.875;
let momentumInterval = null;
let velocityInterval = null;
function f(event) {
currentX = event.clientX;
html.scrollLeft = startScroll + (startX - currentX);
}
content.onmouseleave = function(){
content.removeEventListener('mousemove', f);
};
content.onmousedown = function (event) {
startScroll = html.scrollLeft;
startX = event.clientX;
previousX = startX;
currentX = startX;
clearInterval(velocityInterval);
velocityInterval = setInterval(function () {
velocity = Math.abs(currentX - previousX);
direction = (currentX > previousX ? -1 : 1);
previousX = currentX;
}, 50);
this.addEventListener('mousemove', f)
};
content.onmouseup = function () {
let num = 0;
clearInterval(velocityInterval);
clearInterval(momentumInterval);
num = velocity;
momentumInterval = setInterval(function () {
html.scrollLeft = html.scrollLeft + (num * direction);
num *= momentum;
if (Math.abs(num) < 1){
clearInterval(momentumInterval);
}
}, 15);
content.removeEventListener('mousemove', f);
}
html,body{
height: 100%;
padding: 0;
margin: 0;
}
body {
overflow: hidden;
}
.box{
width: 10000px;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
.content {
width: 100%;
height: 80%;
display: flex;
margin-left: 50px;
margin-right: 50px;
box-shadow: 0 2rem 4rem 0.25rem rgba(46, 43, 55, 0.575);
cursor: grab;
}
.content:active{
cursor: grabbing;
}
p{
margin: 0;
display: flex;
justify-content: center;
align-items: center;
flex-direction:column ;
width: 100%;
height: 100%;
color: #ffffff;
font-size: 30px;
}