简体   繁体   English

使用 CSS 和 jQuery 将上一个/下一个按钮添加到滚动容器

[英]Add prev/next buttons to scroll container with CSS and jQuery

After a long research I found a solution to add a custom scrollbar to a DIV element.经过长时间的研究,我找到了一种将自定义滚动条添加到 DIV 元素的解决方案。 It's called SimpleBar.它被称为 SimpleBar。 Docs can be found here .文档可以在这里找到。

The HTML structure and JS code is pretty simple: HTML 结构和 JS 代码非常简单:

Working demo工作演示

<div class="gallery" data-simplebar data-simplebar-auto-hide="false">
    <div class="item"><img src="https://via.placeholder.com/250x150/0000FF" /></div>
    <div class="item"><img src="https://via.placeholder.com/350x150/FF0000" /></div>
    [...]
</div>

With data-simplebar I can add a custom scrollbar to any DIV.使用data-simplebar我可以向任何 DIV 添加自定义滚动条。

There is just one thing I couldn't solve.只有一件事我无法解决。 I want to add prev/next arrows to the scroll element.我想向滚动元素添加上一个/下一个箭头。 The buttons should jump to the prev/next element in the DIV which is next to the left/right side of the div and not yet scrolled (partially) over.按钮应跳转到 DIV 中的上一个/下一个元素,该元素位于 div 的左/右侧旁边,并且尚未(部分)滚动。

And the JS should work for every slider instance on the site.并且 JS 应该适用于站点上的每个 slider 实例。 Like the SimpleBar itself.就像 SimpleBar 本身一样。 There is no need for extra code per scroll container.每个滚动容器不需要额外的代码。

Is there anything I could use in jQuery?我可以在 jQuery 中使用什么吗?

EDIT: I found this answer and fiddle .编辑:我找到了这个答案小提琴 I added the code to my example and changed it to left/right.我将代码添加到我的示例中并将其更改为左/右。 It's not exactly what I need but I thought it could be a starting point.这不完全是我需要的,但我认为这可能是一个起点。 Unfortunately it doesn't work properly.不幸的是,它不能正常工作。

You can use the following code.您可以使用以下代码。 Note that the scrolling depends on the viewport, so that once there's no more right width to go to, it won't have more room to move.请注意,滚动取决于视口,因此一旦 go 没有更多的正确宽度,它将没有更多的移动空间。

 const DIRECTION = { PREV: 1, NEXT: 2 }; $(document).ready(function () { $('.container').each(function (index, containerItem) { var $gallery = $(containerItem).find('.gallery'); const simpleBar = new SimpleBar($gallery[0], { autoHide: false, scrollbarMaxSize: 50 }); var $scroller = $(simpleBar.getScrollElement()); $(containerItem).find('.scrollLeft').on('click', function () { scroll(DIRECTION.PREV, $scroller); event.preventDefault(); // prevents anchor jump on click }); $(containerItem).find('.scrollRight').on('click', function () { scroll(DIRECTION.NEXT, $scroller); event.preventDefault(); }); $scroller.scroll(function () { setButtonsVisibility($scroller); }); }); }); function scroll(direction, $scroller) { var $active = $scroller.find('.active'); var $target = direction == DIRECTION.PREV? $active.prev(): $active.next(); if ($target.length) { $scroller.animate({ scrollLeft: $target[0].offsetLeft }, 2000); $active.removeClass('active'); $target.addClass('active'); } } function setButtonsVisibility($scroller) { var scrollLeft = $scroller.scrollLeft(); isScrollerStart($scroller, scrollLeft); isScrollerEnd($scroller, scrollLeft); } function isScrollerStart($scroller, scrollLeft, $button) { var $prevButton = $scroller.closest('.container').find('.scrollLeft'); if (scrollLeft == 0) { $prevButton.css('visibility', 'hidden'); } else { $prevButton.css('visibility', 'visible'); } } function isScrollerEnd($scroller, scrollLeft) { var $nextButton = $scroller.closest('.container').find('.scrollRight'); var scrollWidth = $scroller[0].scrollWidth; // entire width var scrollerWidth = $scroller.outerWidth() // visible width if (scrollLeft >= scrollWidth - scrollerWidth) { $nextButton.css('visibility', 'hidden'); } else { $nextButton.css('visibility', 'visible'); } }
 .container {margin: 0 auto 2rem; max-width: 960px;}.gallery {padding-bottom: 2rem; margin-bottom: 2rem;}.gallery.simplebar-content {display: flex;}.gallery.item {margin-right: 1rem;}.simplebar-scrollbar:before {background: red;important.}.simplebar-track:simplebar-horizontal {background; #eee;important.:};buttons {display: flex; justify-content: space-between; width: 100%; margin-bottom. 2rem:}.buttons a {padding; 0:5rem; background: #ddd; text-decoration: none; color. #000:};scrollLeft { visibility: hidden; }
 <script src="https://unpkg.com/simplebar@latest/dist/simplebar.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://unpkg.com/simplebar@latest/dist/simplebar.css"> <div class="container"> <h2>Slider</h2> <div class="gallery"> <div class="item active"><img src="https://via.placeholder.com/150x30/110000" /></div> <div class="item"><img src="https://via.placeholder.com/450x30/3300FF" /></div> <div class="item"><img src="https://via.placeholder.com/350x30/992244" /></div> <div class="item"><img src="https://via.placeholder.com/400x30/0000FF" /></div> </div> <div class="buttons"> <a class="scrollLeft" href="#"><strong>Prev</strong> (remove if first)</a> <a class="scrollRight" href="#"><strong>Next</strong> (remove if last)</a> </div> </div> <div class="container"> <h2>Slider</h2> <div class="gallery"> <div class="item active"><img src="https://via.placeholder.com/150x30/110000" /></div> <div class="item"><img src="https://via.placeholder.com/450x30/3300FF" /></div> <div class="item"><img src="https://via.placeholder.com/350x30/992244" /></div> <div class="item"><img src="https://via.placeholder.com/400x30/0000FF" /></div> </div> <div class="buttons"> <a class="scrollLeft" href="#"><strong>Prev</strong> (remove if first)</a> <a class="scrollRight" href="#"><strong>Next</strong> (remove if last)</a> </div> </div>

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

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