使用鼠标滚轮水平滚动浏览器窗口

编程入门 行业动态 更新时间:2024-10-26 12:28:33
本文介绍了使用鼠标滚轮水平滚动浏览器窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个非常宽的网站,故意设计为没有垂直滚动但很多水平。

I have a very wide website, intentionally designed to have no vertical scrolling but a lot of horizontal.

水平滚动通常是一个痛苦的用户所以想知道是否有一些方法可以使用中间鼠标或其他滚动习惯(例如,向上/向下翻页,向上/向下箭头,鼠标中键/拖动)水平滚动而不是垂直滚动。

Scrolling horizontally is usually a pain to users so was wondering if there was some way of using the middle mouse or other scrolling habits (eg. page up/down, up/down arrows, middle mouse click/drag) to scroll horizontally instead of vertically.

编辑:要求水平滚动的主要原因是布局/方法是从左到右的图形/交互时间轴。我已经找到了一些例子;

The main reason for requiring horizontal scrolling is because the layout/approach is a left to right graphical/interactive timeline. I've since found some examples;

这个与MooTools: www.tinkainteractive.au/ 以及我在 naldzgraphics/inspirations/40-examples-of-horizo​​ntal-scrolling-websites/

This one with MooTools: www.tinkainteractive.au/ and a few other examples I found at naldzgraphics/inspirations/40-examples-of-horizontal-scrolling-websites/

推荐答案

您可以添加自己的事件监听器

You can add your own event listener

document.onmousewheel = myScrollFunction

滚动可以通过

window.scrollBy(x, y)

其中x是水平滚动offset和y垂直滚动偏移量。

Where x is the horizontal scrolling offset and y the vertical scrolling offset.

所以你可以在事件监听器中调用这个函数。您可能必须使用event.stopPropagation停止冒泡并使用event.preventDefault阻止浏览器默认行为,以便不再应用原始滚动行为。

So you might just call this function in your event listener. You may have to stop bubbling with event.stopPropagation and prevent browser default behaviour with event.preventDefault so that the original scrolling behaviour doesn't get applied anymore.

编辑:我对此感到好奇所以我实现了一些东西: - )

I was curious about this so I implemented something :-)

function onScroll(event) { // delta is +120 when scrolling up, -120 when scrolling down var delta = event.detail ? event.detail * (-120) : event.wheelDelta // set own scrolling offset, take inverted sign from delta (scroll down should scroll right, // not left and vice versa var scrollOffset = 10 * (delta / -120); // Scroll it window.scrollBy(scrollOffset, 0); // Not sure if the following two are necessary, you may have to evaluate this event.preventDefault; event.stopPropagation; } // The not so funny part... fin the right event for every browser var mousewheelevt=(/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel"; if (document.attachEvent) document.attachEvent("on"+mousewheelevt, onScroll); else if (document.addEventListener) document.addEventListener(mousewheelevt, onScroll, false);

这适用于Firefox 3.5和Opera 10,但不适用于IE8。但那将是你现在的部分...; - )

This works in Firefox 3.5 and Opera 10, however not in IE8. But that would be your part now... ;-)

更多推荐

使用鼠标滚轮水平滚动浏览器窗口

本文发布于:2023-10-08 08:59:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1472133.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:鼠标   滚轮   浏览器   窗口   水平

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!