首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Infinity循环滑块概念

Infinity循环滑块概念
EN

Stack Overflow用户
提问于 2013-04-08 18:24:45
回答 3查看 45.6K关注 0票数 32

我想知道使用JavaScript/jQuery为网站构建Infinity-Image-Loop-Slider的最佳概念是什么(良好的可读性代码,pest实践代码,可重用性)?我不知道如何编码幻灯片放映,但什么蓝图符合上面提到的要求。我的问题的主要焦点是如何安排图片,以获得无限循环滑块的印象。

通过查看来自不同Sliders的代码,我发现了两种解决方案:

每次显示下一个/上一个图像时,-change所有图像的z索引。

-change图像在DOM中的位置。

但是检查和理解其他人的代码非常耗时-这就是为什么我问这个问题:-)

EN

回答 3

Stack Overflow用户

发布于 2016-02-24 20:40:09

我刚刚创建了项目滑块:查看它:https://github.com/lingtalfi/jItemSlider/blob/master/README.md

它有600行代码,也许你可以简单地浏览一下。

它背后的想法是受到netflix滑块的启发(截至2016-02-24)。

基本上,它使用css转换转换,因为它们是浏览器中最快/最流畅的转换。

http://eng.wealthfront.com/2015/05/19/performant-css-animations/

现在,滑动移动背后的基本概念是,您只显示当前可见切片,但在左侧还有一个不可见切片,在右侧还有另一个不可见切片。

而且,您还有两个额外的项目,两边各一个,因此您的项目看起来如下所示:

上一个项目-上一个额外项目-主要项目-下一个额外项目-下一个项目

只有主要项目可见。额外的项目部分可见。上一项和下一项不可见。

更多详细信息请访问:https://github.com/lingtalfi/jItemSlider/blob/master/doc/conception.md

现在,当您向右滑动时(例如),您基本上将更多的项目添加到右侧,然后从左侧删除这些项目。

这是我到目前为止遇到的最好的技术,因为你不需要处理很长的项目列表(使用克隆而不删除不可见的项目),这会使你的动画速度变慢。

注意:我第一次尝试这个滑块实际上是在不移除的情况下克隆,它是有效的,但我不喜欢它:https://github.com/lingtalfi/jInfiniteSlider

此外,它是基于项目的(而不是基于像素的),最终,这是用户所期望的,因为所有的东西都是对齐的,就像它应该的那样。

票数 3
EN

Stack Overflow用户

发布于 2015-06-19 23:43:31

非常感谢这篇文章!我已经更新并使用了上面的代码。我希望这对每个人都有帮助。可怜的开发者。

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Directive slider</title>
    <style>
        /* 四联切换焦点图 */
        .slides-wrapper{ position: relative;  width: 100%;  margin: 10px 0; }
        .gallery { position: relative;  width: 1200px;  height: 180px;  overflow:hidden;  }
        .gallery ul {  font-size: 0;  white-space: nowrap;  position: absolute;  top: 0;  left: -1200px;  margin: 0; padding: 0;  }
        .gallery li {  display: inline-block;  vertical-align: top;  width: 1200px;  height: 180px;  white-space: normal;  }
        .gallery li img{  width: 298px;  height:180px;  padding: 1px;  }
        .gallery .arrow { background: url(/shop/templates/default/images/home_bg.png) no-repeat; background-size: 150px 223px; width: 35px; height: 70px; position: absolute; z-index: 2; top: 50px; cursor: pointer; opacity: 0;}
        .gallery .prev { background-position: 1px -92px; left: 0;}
        .gallery .next { background-position: -30px -92px; right: 0px;}
    </style>
    <style type="text/css">
        .demo_wrapper{
            margin: 0 auto;
            width: 1200px;
        }
        .demo_wrapper .title{
            text-align: center;
        }
    </style>
</head>
<body>
<div class="demo_wrapper">
    <div class="title">
        <h1>Directive slider (Published by fenmingyu)</h1>
    </div>
    <!-- demo content -->
    <div class="slides-wrapper">
        <div class="gallery" id="top_sale_gallery">
            <ul>
                <li>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-1-1.jpg?234" alt=""></a>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-1-2.jpg?752" alt=""></a>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-1-3.jpg?320" alt=""></a>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-1-4.jpg?365" alt=""></a>
                </li>
                <li>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-2-1.jpg?852" alt=""></a>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-2-2.jpg?746" alt=""></a>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-2-3.jpg?525" alt=""></a>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-2-4.jpg?550" alt=""></a>
                </li>
            </ul>
            <div class='arrow prev'></div>
            <div class='arrow next'></div>
        </div>
        <div class="gallery" id="top_goods_gallery">
            <ul>
                <li>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-3-1.jpg?793" alt=""></a>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-3-2.jpg?180" alt=""></a>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-3-3.jpg?550" alt=""></a>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-3-4.jpg?851" alt=""></a>
                </li>
                <li>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-1-1.jpg?234" alt=""></a>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-1-2.jpg?752" alt=""></a>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-1-3.jpg?320" alt=""></a>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-1-4.jpg?365" alt=""></a>
                </li>
                <li>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-2-1.jpg?852" alt=""></a>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-2-2.jpg?746" alt=""></a>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-2-3.jpg?525" alt=""></a>
                    <a href="" target="_blank" title="" style="opacity: 1;"><img src="http://imgserv.5thmedia.cn/upload_test/shop/editor/web-102-104-2-4.jpg?550" alt=""></a>
                </li>
            </ul>
            <div class='arrow prev'></div>
            <div class='arrow next'></div>
        </div>
        <div style="clear: both;"></div>
    </div>
</div>

</body>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
    $(function() {
        $.fn.gallery = function(settings) {
            var defaults = {
                time: 3000,
                direction:1
            };
            var settings = $.extend(defaults, settings);
            var gallery_wrapper = $(this),
                gallery = gallery_wrapper.find('ul'),
                items   = gallery.find('li'),
                len     = items.length,
                current = 1,  /* the current item we're looking */
                first   = items.filter(':first'),
                last    = items.filter(':last'),
                w = gallery.find('li').width(),
                triggers = gallery_wrapper.find('.arrow');
            var show_slide = function(direction,w){
                gallery.animate({ left: "+=" + (-w * direction) }, function() {

                    current += direction;

                    /**
                     * we're cycling the slider when the the value of "current"
                     * variable (after increment/decrement) is 0 or when it exceeds
                     * the initial gallery length
                     */
                    cycle = !!(current === 0 || current > len);

                    if (cycle) {
                        /* we switched from image 1 to 4-cloned or
                         from image 4 to 1-cloned */
                        current = (current === 0)? len : 1;
                        gallery.css({left:  -w * current });
                    }
                });
            };
            var picTimer = setInterval(function() {
                        show_slide(settings.direction,w);
                    },
                    settings.time);
            return this.each(function(){

                /* 1. Cloning first and last item */
                first.before(last.clone(true));
                last.after(first.clone(true));
                /* 2. Set button handlers */
                triggers.on('click', function() {
                    if (gallery.is(':not(:animated)')) {

                        var cycle = false;
                        settings.direction = ($(this).hasClass('prev'))? -1 : 1;
                        /* in the example buttons have id "prev" or "next" */
                        show_slide(settings.direction,w);
                    }
                    clearInterval(picTimer);
                    picTimer = setInterval(function() {
                                show_slide(settings.direction,w);
                            },
                            settings.time);
                });
                /* hover show arrows*/
                show_slide(settings.direction,w);

                gallery_wrapper.hover(function() {
                    $(this).find(".arrow").css("opacity", 0.0).stop(true, false).animate({
                                "opacity": "0.3"
                            },
                            300);
                },function(){
                    $(this).find(".arrow").css("opacity", 0.3).stop(true, false).animate({
                                "opacity": "0"
                            },
                            300);
                });
            });
        };
        $('#top_goods_gallery.gallery').gallery();
        $('#top_sale_gallery.gallery').gallery({
            time: 5000,
            direction:-1
        });
    });
</script>
</html>

在我的项目中使用它。

票数 0
EN

Stack Overflow用户

发布于 2021-12-13 20:33:39

Vanila Javascript!!无克隆技术,拯救的getElementsByClassName

document.getElementsByClassName选择是一个实时集合;与querySelector方法不同,DOM中的任何更改都会在存储的变量中更新。

在这种技术中,如果我们在向右单击时到达最后一张幻灯片,则将第一张幻灯片移动到末尾,或者当我们在向左单击时到达第一张幻灯片时,将最后一张幻灯片移动到开头。这里不需要创建克隆。getElementsByClassName方法为我们提供了一个活动的HTML集合,当我们在DOM中进行更改时,可以使用该集合进行更新。(在本例中,更改了div的顺序)

这是我的GitHub Repository

代码语言:javascript
复制
// slider
const slides = document.getElementsByClassName("slide"); // this selection is a live collection; any changes in DOM is updated in the variable unlike querySelectors
const btnLeft = document.querySelector(`.btn-left`);
const btnRight = document.querySelector(`.btn-right`);

let currentSlideIndex = 0;
let lastSlideIndex = slides.length - 1;

// go to a slide;
function goToSlide(slideIndex) {
    [...slides].forEach((s, i) => {
        s.style.transform = `translateX(${100 * (i - slideIndex)}%)`
    })
    currentSlideIndex = slideIndex;
}
goToSlide(currentSlideIndex);

// make ready the next slide if current slide is the first or the last slide
function readyNextSlide() {
    // if currentSlide is the last slide, shift the first slide to the end
    if (currentSlideIndex === lastSlideIndex) {
        slides[lastSlideIndex].insertAdjacentElement("afterend", slides[0]);
        slides[lastSlideIndex].style.transform = `translateX(${100}%)`;
        currentSlideIndex--; //this is because current slide is now the second last slide
    }
    // if currentSlide is the first slide, shift the last slide to the beginning
    if (currentSlideIndex === 0) {
        slides[0].insertAdjacentElement("beforebegin", slides[lastSlideIndex]);
        slides[0].style.transform = `translateX(-${100}%)`;
        currentSlideIndex++; //this is because current slide is now the second slide
    }
}

// put the last slide in the beginning; ('if' condition is not necessary but providing if condition is future proof if user sets the initial slide to be shown as the last slide )
if (currentSlideIndex === lastSlideIndex || currentSlideIndex === 0) readyNextSlide();

// shift all slides left or right based on direction provided
function shiftSlides(direction) {
    direction ? currentSlideIndex++ : currentSlideIndex--
    if (currentSlideIndex === lastSlideIndex || currentSlideIndex === 0) readyNextSlide();
    goToSlide(currentSlideIndex);
}

//button click events
btnRight.addEventListener("click", shiftSlides.bind(null, 1));
btnLeft.addEventListener("click", shiftSlides.bind(null, 0));
代码语言:javascript
复制
body {
  display: grid;
  height: 100vh;
  width: 100vw;
  align-items: center;
  align-content: center;
  justify-content: center;
}

.slider {
  position: relative;
  width: 600px;
  height: 300px;
  transform: scale(0.8);
  overflow: hidden; /* remove overflow to see what's going on*/
}

.slide {
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 1s;
}

.slide b {
  position: absolute;
  font-size: 10em;
  color: black;
  opacity: 0.6;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.s1 {
  background-color: cornflowerblue;
}

.s2 {
  background-color: bisque;
}

.s3 {
  background-color: coral;
}

.s4 {
  background-color: thistle;
}

.btn {
  position: absolute;
  top: 50%;
  z-index: 10;
  border: none;
  background: crimson;
  font-family: inherit;
  color: white;
  height: 5.5rem;
  width: 5.5rem;
  font-size: 3.25rem;
  cursor: pointer;
}

.btn-left {
  left: 6%;
  transform: translate(-50%, -50%);
}

.btn-right {
  right: 6%;
  transform: translate(50%, -50%);
}
代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>Infinity Looping Rotating slider</title>
        <link rel="stylesheet" href="slider.css">
        <script src="slider.js" defer></script>
    </head>
    <body>
        <div class="slider">
            <div class="slide s1"><b>1</b></div>
            <div class="slide s2"><b>2</b></div>
            <div class="slide s3"><b>3</b></div>
            <div class="slide s4"><b>4</b></div>

            <button class="btn btn-left">&larr;</button>
            <button class="btn btn-right">&rarr;</button>
        </div>
        <p>
            <b>
                This is my response to a questing in <a href="https://stackoverflow.com/a/70340585/6216765">StackOverflow</a> about infinity loop slider.<br>
                My github repo is <a href="https://github.com/dibakash/infinity-looping-rotating-slider">Infinity loop Slider by Dibakash</a>
            </b>
        </p>

    </body>
</html>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15876754

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档