【问题标题】:After second slide, next slide not scrolls第二张幻灯片后,下一张幻灯片不滚动
【发布时间】:2020-12-13 00:59:45
【问题描述】:

我正在尝试仅通过鼠标滚轮进行水平滚动,并且 div 将捕捉到边缘或中心,这意味着在一次滚动时,一张幻灯片会出现,下一张幻灯片会出现。所以我搜索并发现 - https://codepen.io/vasiluca/pen/xWEQdQ?editors=0010 -我使用并做了一些更改。第一张和第二张幻灯片滚动良好,但下一张幻灯片无法通过鼠标滚轮滚动。

jq

var scrolling = false;
    var scrolled = 0;
    const scrollDuration = 400;
    const scrollPause = 0; // Sets how much you must scroll before changing the slide
    // const scrollInterval = 5; // Time (seconds) before you can scroll again
    
    function scroll(direction) {
        if (scrolling == false) {
            scrolling = true;
            $('html, body').animate({
                scrollLeft: direction == 'left' ? $('html,body').scrollLeft() - $('.slide').innerWidth() : $('html,body').scrollLeft() + $('.slide').innerWidth()
            }, scrollDuration, function() {
                scrolling = false;
            });
        }
    }
    
    $(document).scrollTop(0);
    $(window).on('wheel', function(e) {
        if (scrolling == false) {
            scrolled++;
        }
        if (scrolled > scrollPause) {
            if (e.originalEvent.deltaY < 0) { // scrolling down
                scroll('left');
                scrolled = 0;
            }
            if (e.originalEvent.deltaY > 0) { // scrolling up
                scroll('right');
                scrolled = 0;
            }
        }
    });
    
    $('.slide').mouseover(function() {
        if (scrolling == false) {
            $('html, body').animate({
                scrollLeft: $(this).offset().left
            }, 100);
        }
    });
    
    $('.left').click(function() {
        scroll('left');
        scrolled = 0;
    });
    $('.right').click(function() {
        scroll('right');
        scrolled = 0;
    });
    
    $(document).scroll(function() {
        if ($(document).scrollLeft() == 0) {
            $('.left').css({
                'opacity': 0,
                'transform': 'translateX(-100%)'
            })
        } else {
            $('.left').css({
                'opacity': 1,
                'transform': 'translateX(0)'
            })
        }
        if ($(document).scrollLeft() == $('.slide-container').innerWidth() + $('.slide').innerWidth()) {
            $('.right').css({
                'opacity': 0,
                'transform': 'translateX(100%)'
            })
        } else {
            $('.right').css({
                'opacity': 1,
                'transform': 'translateX(0)'
            })
        }
    });
    
    $(document).keydown(function(e) {
        switch(e.keyCode) {
            case 39:
            case 40:
            scroll('right');
            break;
            case 37:
            case 38:
            scroll('left');
            break;
        }
    });

CSS

body {
        font-family: 'Roboto', sans-serif;
        overflow-y: hidden;
        border: 1px solid red;
    }
    
    *{margin: 0; padding: 0;}
    
    .container {border: 1px solid yello;}
    .horizontal{
        overflow-x: auto;
        overflow-y: hidden;
    }
    .header{
        position: fixed;
        width: 100vw;
        text-align: center;
        border: 1px solid yellow;
    }
    
    .left {
        position: fixed;
        top: 0;
        left: 0;
        height: 50px;
        width: 50px;
        line-height: 50px;
        text-align: center;
        background: rgba(255,255,255,1);
        opacity: 0;
        transition: background 0.15s ease;
        opacity 0.5s ease;
        transform 0.5s ease;
        }
    .left:hover{background: lightgrey;}
    
    .right{
        position: fixed;
        top: 0;
        right: 0;
        height: 50px;
        width: 50px;
        line-height: 50px;
        text-align: center;
        background: rgba(255,255,255,.5);
        transition: background 0.15s ease;
    }
    .right:hover{background: lightgrey}
    
    .slide-container{
        display: flex;
        height: 100vh;
        flex-wrap: nowrap;
        align-items: center;
    }
    .slide{
        flex: 0 0 100%;
        height: 100%;
        text-align: center;
        font-size: 50px;
        line-height: calc(100vh - 50px);
        overflow: hidden;
    }
    .slide:nth-child(odd){
        background: limegreen;
    }
    .slide:nth-child(even){
        background: lightblue;
    }

HTML

<div class="continer">
    <div class="header">
        <h1 class="title"> Horizontal Scrolling </h1>
        <span>(Use Mousewheel)</span>
    </div>
    
    <span class="left">left</span>
    <span class="right">right</span>
    
    <div class="slide-container">
        <div class="slide">A</div>
        <div class="slide">B</div>
        <div class="slide">C</div>
        <div class="slide">D</div>
        <div class="slide">E</div>
    </div>
</div><!--continer ends here-->

【问题讨论】:

  • 我在 Firefox 和 Chrome 中尝试了你的代码,它工作正常。
  • 感谢您的回复。请检查,有 5 张幻灯片,仅适用于两张幻灯片 A 和 B,鼠标滚轮滚动不适用于下一张幻灯片 C、D E。
  • 我已经用鼠标滚轮在 Windows10 上的 Edge、Chrome、Firefox 和 IE11 上进行了检查,它运行良好 - 前后滑动 5 次。您使用的是什么浏览器/操作系统?

标签: javascript html jquery css


【解决方案1】:

这不是问题的答案,因为鼠标滚轮在幻灯片之间移动至少在当前浏览器(Windows10 上的 Edge)上可以,但我将问题中的代码作为 sn-p 放在这里,以便其他人可以轻松尝试它在其他环境中。

var scrolling = false;
    var scrolled = 0;
    const scrollDuration = 400;
    const scrollPause = 0; // Sets how much you must scroll before changing the slide
    // const scrollInterval = 5; // Time (seconds) before you can scroll again
    
    function scroll(direction) {
        if (scrolling == false) {
            scrolling = true;
            $('html, body').animate({
                scrollLeft: direction == 'left' ? $('html,body').scrollLeft() - $('.slide').innerWidth() : $('html,body').scrollLeft() + $('.slide').innerWidth()
            }, scrollDuration, function() {
                scrolling = false;
            });
        }
    }
    
    $(document).scrollTop(0);
    $(window).on('wheel', function(e) {
        if (scrolling == false) {
            scrolled++;
        }
        if (scrolled > scrollPause) {
            if (e.originalEvent.deltaY < 0) { // scrolling down
                scroll('left');
                scrolled = 0;
            }
            if (e.originalEvent.deltaY > 0) { // scrolling up
                scroll('right');
                scrolled = 0;
            }
        }
    });
    
    $('.slide').mouseover(function() {
        if (scrolling == false) {
            $('html, body').animate({
                scrollLeft: $(this).offset().left
            }, 100);
        }
    });
    
    $('.left').click(function() {
        scroll('left');
        scrolled = 0;
    });
    $('.right').click(function() {
        scroll('right');
        scrolled = 0;
    });
    
    $(document).scroll(function() {
        if ($(document).scrollLeft() == 0) {
            $('.left').css({
                'opacity': 0,
                'transform': 'translateX(-100%)'
            })
        } else {
            $('.left').css({
                'opacity': 1,
                'transform': 'translateX(0)'
            })
        }
        if ($(document).scrollLeft() == $('.slide-container').innerWidth() + $('.slide').innerWidth()) {
            $('.right').css({
                'opacity': 0,
                'transform': 'translateX(100%)'
            })
        } else {
            $('.right').css({
                'opacity': 1,
                'transform': 'translateX(0)'
            })
        }
    });
    
    $(document).keydown(function(e) {
        switch(e.keyCode) {
            case 39:
            case 40:
            scroll('right');
            break;
            case 37:
            case 38:
            scroll('left');
            break;
        }
    });
body {
        font-family: 'Roboto', sans-serif;
        overflow-y: hidden;
        border: 1px solid red;
    }
    
    *{margin: 0; padding: 0;}
    
    .container {border: 1px solid yello;}
    .horizontal{
        overflow-x: auto;
        overflow-y: hidden;
    }
    .header{
        position: fixed;
        width: 100vw;
        text-align: center;
        border: 1px solid yellow;
    }
    
    .left {
        position: fixed;
        top: 0;
        left: 0;
        height: 50px;
        width: 50px;
        line-height: 50px;
        text-align: center;
        background: rgba(255,255,255,1);
        opacity: 0;
        transition: background 0.15s ease;
        opacity 0.5s ease;
        transform 0.5s ease;
        }
    .left:hover{background: lightgrey;}
    
    .right{
        position: fixed;
        top: 0;
        right: 0;
        height: 50px;
        width: 50px;
        line-height: 50px;
        text-align: center;
        background: rgba(255,255,255,.5);
        transition: background 0.15s ease;
    }
    .right:hover{background: lightgrey}
    
    .slide-container{
        display: flex;
        height: 100vh;
        flex-wrap: nowrap;
        align-items: center;
    }
    .slide{
        flex: 0 0 100%;
        height: 100%;
        text-align: center;
        font-size: 50px;
        line-height: calc(100vh - 50px);
        overflow: hidden;
    }
    .slide:nth-child(odd){
        background: limegreen;
    }
    .slide:nth-child(even){
        background: lightblue;
    }
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="continer">
    <div class="header">
        <h1 class="title"> Horizontal Scrolling </h1>
        <span>(Use Mousewheel)</span>
    </div>
    
    <span class="left">left</span>
    <span class="right">right</span>
    
    <div class="slide-container">
        <div class="slide">A</div>
        <div class="slide">B</div>
        <div class="slide">C</div>
        <div class="slide">D</div>
        <div class="slide">E</div>
    </div>
</div><!--continer ends here-->

【讨论】:

  • 您使用的是哪种浏览器和操作系统?
  • 完美。但是为什么它在我的浏览器中不起作用。我使用 Windows 10 和最新版本的 chrome、Firefox 和 edge。为了确认上面的代码,我也尝试了 codepan 但不起作用 - codepen.io/ramesh_a/pen/oNzBxdd?editors=1001 - 请参阅。
  • 你的 codepen 没有加载 jquery - 你能解决这个问题,我会再试一次。
  • 是的,我在我的代码盘中添加了 jquery,它可以工作,但仍然在我的桌面浏览器上,它不工作。我还在我的 html 文件中尝试了 jquery 的链接。
  • codepen 适合我。您使用的是什么桌面浏览器,您是说 codepen 不能在上面工作吗?您的意思不是浏览器而是您从桌面而不是网络提供的代码?
猜你喜欢
  • 1970-01-01
  • 2013-08-24
  • 2017-11-02
  • 2021-01-17
  • 2013-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多