简体   繁体   English

如何在ScrollDown上创建body scrollLeft,反之亦然

[英]How to make body scrollLeft on ScrollDown and vice-versa

I have put together a little code that I had hoped would allow the html to scroll to the left whenever a user scrolls down and scroll right when the user scrolls up 我已经整理了一些我曾希望允许html在用户向下滚动时向左滚动并在用户向上滚动时向右滚动的代码

I've put together an example of my code here JSFIDDLE 我在这里汇总了我的代码示例JSFIDDLE

$(document).ready(function() {
    $(window).bind('mousewheel', function(e) {
        e.preventDefault();
        if (e.originalEvent.wheelDelta >= 0) {
            $('html, body').scrollRight(1);
        }
        else {
            $('html, body').scrollLeft(1);
        }
    });
});

I need to prevent the user from scrolling vertically and want the vertical scrolls to cause horizontal scrolling instead. 我需要阻止用户垂直滚动,并希望垂直滚动导致水平滚动。

First of all, you miss te parenthesis at the end of the preventDefault() call. 首先,你在preventDefault()调用结束时错过了括号。 That's why the vertical scrollbar still works. 这就是垂直滚动条仍然有效的原因。

Second, there is no such jQuery method as scrollRight() . 其次,没有像scrollRight()这样的jQuery方法。 You should use the scrollLeft() method for both direction. 您应该对两个方向使用scrollLeft()方法。

See your updated fiddle ! 看到你的更新小提琴

The scrollRight method is not defined by jQuery so you have to use scrollLeft. scrollRight方法不是由jQuery定义的,因此您必须使用scrollLeft。

When you call scrollLeft without any arguments you get the current scroll position (starting from the left edge). 当您调用不带任何参数的scrollLeft时,您将获得当前滚动位置(从左边缘开始)。 When you call scrollLeft(value) you set the current scroll position to value (ref. https://api.jquery.com/scrollleft/ ). 调用scrollLeft(value)时,将当前滚动位置设置为value(参考https://api.jquery.com/scrollleft/ )。

Following snippet works. 以下代码段工作原理。

 $(document).ready(function() { var body = $('body'); $(window).bind('mousewheel', function(e) { e.preventDefault(); body.scrollLeft(body.scrollLeft() - e.originalEvent.wheelDelta); }); }); 
 section { width: 500vw; height: 100vh; /*unimportant */ background: rgba(76,76,76,1); background: -moz-linear-gradient(45deg, rgba(76,76,76,1) 0%, rgba(89,89,89,1) 12%, rgba(102,102,102,1) 25%, rgba(71,71,71,1) 39%, rgba(44,44,44,1) 50%, rgba(0,0,0,1) 51%, rgba(17,17,17,1) 60%, rgba(43,43,43,1) 76%, rgba(28,28,28,1) 91%, rgba(19,19,19,1) 100%); background: -webkit-gradient(left bottom, right top, color-stop(0%, rgba(76,76,76,1)), color-stop(12%, rgba(89,89,89,1)), color-stop(25%, rgba(102,102,102,1)), color-stop(39%, rgba(71,71,71,1)), color-stop(50%, rgba(44,44,44,1)), color-stop(51%, rgba(0,0,0,1)), color-stop(60%, rgba(17,17,17,1)), color-stop(76%, rgba(43,43,43,1)), color-stop(91%, rgba(28,28,28,1)), color-stop(100%, rgba(19,19,19,1))); background: -webkit-linear-gradient(45deg, rgba(76,76,76,1) 0%, rgba(89,89,89,1) 12%, rgba(102,102,102,1) 25%, rgba(71,71,71,1) 39%, rgba(44,44,44,1) 50%, rgba(0,0,0,1) 51%, rgba(17,17,17,1) 60%, rgba(43,43,43,1) 76%, rgba(28,28,28,1) 91%, rgba(19,19,19,1) 100%); background: -o-linear-gradient(45deg, rgba(76,76,76,1) 0%, rgba(89,89,89,1) 12%, rgba(102,102,102,1) 25%, rgba(71,71,71,1) 39%, rgba(44,44,44,1) 50%, rgba(0,0,0,1) 51%, rgba(17,17,17,1) 60%, rgba(43,43,43,1) 76%, rgba(28,28,28,1) 91%, rgba(19,19,19,1) 100%); background: -ms-linear-gradient(45deg, rgba(76,76,76,1) 0%, rgba(89,89,89,1) 12%, rgba(102,102,102,1) 25%, rgba(71,71,71,1) 39%, rgba(44,44,44,1) 50%, rgba(0,0,0,1) 51%, rgba(17,17,17,1) 60%, rgba(43,43,43,1) 76%, rgba(28,28,28,1) 91%, rgba(19,19,19,1) 100%); background: linear-gradient(45deg, rgba(76,76,76,1) 0%, rgba(89,89,89,1) 12%, rgba(102,102,102,1) 25%, rgba(71,71,71,1) 39%, rgba(44,44,44,1) 50%, rgba(0,0,0,1) 51%, rgba(17,17,17,1) 60%, rgba(43,43,43,1) 76%, rgba(28,28,28,1) 91%, rgba(19,19,19,1) 100%); filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#4c4c4c', endColorstr='#131313', GradientType=1 ); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <section></section> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM