There is few issue in your code:
ele = $('html,body');
should be the element that you intends to scroll, not the document<body>
or<html>
, e.g in your case is<div id="scroll">...
- you have to use .scrollLeft() not
scrollTop()
since you are scrolling left and right, not top, down.
I believe this is what you want
var ele = $('#scroll');
var speed = 10,
scroll = 3,
scrolling;
$('#up').mouseenter(function() {
//scroll the element up
scrolling = window.setInterval(function() {
scroll += 0.5;
ele.scrollLeft(ele.scrollLeft() - scroll);
}, speed);
}).mouseleave(function(){
window.clearInterval(scrolling);
scroll = 3;
})
$('#down').mouseenter(function() {
//scroll the element down
scrolling = window.setInterval(function() {
scroll += 0.5;
ele.scrollLeft(ele.scrollLeft() + scroll);
}, speed);
})
.mouseleave(function(){
window.clearInterval(scrolling);
scroll = 3;
})
.control {
width: 100%;
position: fixed;
text-align: center
}
#up.control {
position: fixed;
top: 0
}
#down.control {
position: fixed;
top: 20
}
/* no needed: */
#scroll {
overflow-x: scroll;
width: 500px;
white-space: nowrap;
overflow: hidden!imprtant;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div id="scroll">
Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content
here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here
and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and
more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more
content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content here Content here and more content
here
</div>
<a href="#" id="up" class="control">left</a>
<a href="#" id="down" class="control">right</a>