Home >Web Front-end >HTML Tutorial >How to implement horizontal mouse wheel movement on a horizontal web page? _html/css_WEB-ITnose
How to move the mouse wheel horizontally on a horizontal webpage?
I am making a school food ordering webpage and want to make it horizontal. The webpage has already been made. How can I make the mouse wheel move the scroll bar horizontally?
Is it possible to add a button that when clicked will cause the scroll bar to move a certain number of pixels to the right? I only know static html web pages, please help me.
Adding a button is as simple as changing the position of the component that needs to be scrolled in the button function. Using libraries such as jQuery also brings Scroll effects.
Vertical and horizontal orientation is a matter of the client operating system, just configure the mouse properties, if any.
(function() { var stepSize = 200, //每滚动一格鼠标,移动多少距离 doc = document.documentElement, body = document.body, docWidth = doc.clientWidth, scrollLeft = 0;//添加mousewheel事件if (document.addEventListener) { document.addEventListener('mousewheel', scroll, false);} else { document.attachEvent('onmousewheel',scroll) //针对老ie浏览器}//处理mousewheel事件的信息function scroll (event) { var direction = event.wheelDelta; //保证滚动到头的时候不再调用update函数 if (scrollLeft <= 0 && direction > 0) { return; } if (scrollLeft >= docWidth && direction < 0) { return; } //根据鼠标滚动的方向确定是往左还是往右移动 var distance = direction > 0? -stepSize : stepSize; update(distance);}//滚动function update (distance) { scrollLeft += distance; doc.scrollLeft = scrollLeft; body.scrollLeft = scrollLeft; //针对webkit浏览器} })();
JavaScript code?123456789101112131415161718192021222324252627282930313233343536( function() { var stepSize = 200, // How much distance does the mouse move every time doc = document.documentElement, body = documen...
(function() {var stepSize = 200, //每滚动一格鼠标,移动多少距离 doc = document.documentElement, body = document.body, docWidth = doc.clientWidth, scrollLeft = -1, ready = false;//添加鼠标滚轮事件if (document.addEventListener) { document.addEventListener('mousewheel', scroll, false); document.addEventListener('DOMMouseScroll', scroll, false); //针对firefox} else { document.attachEvent('onmousewheel',scroll) //针对老ie浏览器}//处理mousewheel事件的信息function scroll (event) { //第一次滚动需要获取当前滚动位置 if (!ready) { scrollLeft = doc.scrollLeft + body.scrollLeft; ready = true; } //firefox用detail反映滚动方向,而且方向和其他浏览器相反。其他浏览器用wheelDelta var direction = event.wheelDelta || -event.detail; //保证滚动到头的时候不再调用update函数 if (scrollLeft <= 0 && direction > 0) { return; } if (scrollLeft >= docWidth && direction < 0) { return; } //根据鼠标滚动的方向确定是往左还是往右移动 var distance = direction > 0? -stepSize : stepSize; update(distance);}//滚动function update (distance) { scrollLeft += distance; doc.scrollLeft = scrollLeft; body.scrollLeft = scrollLeft; //针对webkit浏览器} })();
html, body { height: 100%;}
You can search for many solutions, you can refer to
(function() {var stepSize = 200, //每滚动一格鼠标,移动多少距离 doc = document.documentElement, body = document.body, docWidth = doc.clientWidth, scrollLeft = -1, ready = false;//添加鼠标滚轮事件if (document.addEventListener) { document.addEventListener('mousewheel', scroll, false); document.addEventListener('DOMMouseScroll', scroll, false); //针对firefox} else { document.attachEvent('onmousewheel',scroll) //针对老ie浏览器}//处理mousewheel事件的信息function scroll (event) { //第一次滚动需要获取当前滚动位置 if (!ready) { scrollLeft = doc.scrollLeft + body.scrollLeft; ready = true; } //firefox用detail反映滚动方向,而且方向和其他浏览器相反。其他浏览器用wheelDelta var direction = event.wheelDelta || -event.detail; //保证滚动到头的时候不再调用update函数 if (scrollLeft <= 0 && direction > 0) { return; } if (scrollLeft >= docWidth && direction < 0) { return; } //根据鼠标滚动的方向确定是往左还是往右移动 var distance = direction > 0? -stepSize : stepSize; update(distance);}//滚动function update (distance) { scrollLeft += distance; doc.scrollLeft = scrollLeft; body.scrollLeft = scrollLeft; //针对webkit浏览器} })();
html, body { height: 100%;}